Skip to content

Replace deprecated APIs in rt_printf_impl.cpp#9833

Closed
hyunjiki-amd wants to merge 1 commit into
Xilinx:masterfrom
hyunjiki-amd:banned-api-2
Closed

Replace deprecated APIs in rt_printf_impl.cpp#9833
hyunjiki-amd wants to merge 1 commit into
Xilinx:masterfrom
hyunjiki-amd:banned-api-2

Conversation

@hyunjiki-amd
Copy link
Copy Markdown
Collaborator

Problem solved by the commit

To meet the driver compliance, banned APIs must be replaced according to https://learn.microsoft.com/en-us/windows-hardware/drivers/devtest/28719-banned-api-usage-use-updated-function-replacement

Bug / issue (if any) fixed, which PR introduced the bug, how it was discovered

Issue fixed: driver compliance

How problem was solved, alternative solutions (if any) and why they were rejected

Replace deprecated APIs in core/common (e.g., strncpy to memcpy)

Risks (if any) associated the changes in the commit

None

What has been tested and how, request additional testing if necessary

Confirmed that the build is successful with the changes locally

Documentation impact (if any)

None

Signed-off-by: Hyunji Kim Hyunji.Kim@amd.com

@hyunjiki-amd
Copy link
Copy Markdown
Collaborator Author

Will implemented in terms of std::string instead of c-style arrays

Copy link
Copy Markdown
Contributor

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

char *buf = formatStr + strlen(formatStr);
sprintf(buf, "%d", conversion.m_fieldWidthValue);
char *buf = &formatStr[pos];
pos += sprintf(buf, "%d", conversion.m_fieldWidthValue);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not call c-style vararg functions [cppcoreguidelines-pro-type-vararg]

    pos += sprintf(buf, "%d", conversion.m_fieldWidthValue);
           ^

char *buf = formatStr + strlen(formatStr);
sprintf(buf, ".%d", conversion.m_precisionValue);
char *buf = &formatStr[pos];
pos += sprintf(buf, ".%d", conversion.m_precisionValue);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not call c-style vararg functions [cppcoreguidelines-pro-type-vararg]

    pos += sprintf(buf, ".%d", conversion.m_precisionValue);
           ^

@hyunjiki-amd hyunjiki-amd changed the title Replace deprecated APIs in core/common rt_printf_impl.cpp Replace deprecated APIs in rt_printf_impl.cpp May 25, 2026
Copy link
Copy Markdown
Collaborator

@stsoe stsoe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't really change the brain dead code. Please prompt AI with what I quote below.
Then review if response is any good. Perplexity's response looked pretty good, but I didn't have the time to review.

This code is completely brain-dead, please rewrite to use std::string and no unsafe strXXX functions.

std::string convertArg(const PrintfArg& arg, const ConversionSpec& conversion)
{
  std::string retval = "";
  char formatStr[32];
  strcpy(formatStr, "%");
  if (conversion.m_leftJustify)
    strcat(formatStr, "-");
  if (conversion.m_signPlus)
    strcat(formatStr, "+");
  if (conversion.m_prefixSpace)
    strcat(formatStr, " ");
  if (conversion.m_alternative)
    strcat(formatStr, "#");
  if (conversion.m_padZero)
    strcat(formatStr, "0");
  if (conversion.m_fieldWidth) {
    char *buf = formatStr + strlen(formatStr);
    sprintf(buf, "%d", conversion.m_fieldWidthValue);
  }
  if (conversion.m_precision) {
    char *buf = formatStr + strlen(formatStr);
    sprintf(buf, ".%d", conversion.m_precisionValue);
  }
  switch ( conversion.m_lengthModifier ) {
    case ConversionSpec::CS_CHAR: {
      strcat(formatStr, "hh");
      break;
    }
    case ConversionSpec::CS_SHORT: {
      strcat(formatStr, "h");
      break;
    }
    case ConversionSpec::CS_INT_FLOAT: {
      // TODO: Vec Only...
      //strcat(formatStr, "hl");
      break;
    }
    case ConversionSpec::CS_LONG: {
      // HACK: LONG only supported for non vectors now...
      if ( conversion.m_vectorSize == 1 ) {
        strcat(formatStr, "l");
      }
      break;
    }
    default:
      break;
  }

  strcat(formatStr, " ");
  formatStr[strlen(formatStr)-1] = conversion.m_specifier;
  // TODO: later make this dynamically size... for now 1024 should be sufficient
  int bufLen = 1024;
  char *printBuf = new char[bufLen];
  switch ( arg.m_typeInfo ) {
    case PrintfArg::AT_PTR: {
      snprintf(printBuf, bufLen, formatStr, arg.ptr);
      retval = printBuf;
      break;
    }
    case PrintfArg::AT_STR: {
      snprintf(printBuf, bufLen, formatStr, arg.str.c_str());
      retval = printBuf;
      break;
    }
    case PrintfArg::AT_INT: {
      snprintf(printBuf, bufLen, formatStr, arg.int_arg);
      retval = printBuf;
      break;
    }
    case PrintfArg::AT_UINT: {
      snprintf(printBuf, bufLen, formatStr, arg.uint_arg);
      retval = printBuf;
      break;
    }
    case PrintfArg::AT_FLOAT: {
      snprintf(printBuf, bufLen, formatStr, arg.float_arg);
      retval = printBuf;
      break;
    }
    case PrintfArg::AT_INTVEC: {
      size_t comma = 0;
      for (auto val : arg.intVec) {
        if (comma++) retval += ",";
        snprintf(printBuf, bufLen, formatStr, val);
        retval += printBuf;
      }
      break;
    }
    case PrintfArg::AT_UINTVEC: {
      size_t comma = 0;
      for (auto val : arg.uintVec) {
        if (comma++) retval += ",";
        snprintf(printBuf, bufLen, formatStr, val);
        retval += printBuf;
      }
      break;
    }
    case PrintfArg::AT_FLOATVEC: {
      size_t comma = 0;
      for (auto val : arg.floatVec) {
        if (comma++) retval += ",";
        snprintf(printBuf, bufLen, formatStr, val);
        retval += printBuf;
      }
      break;
    }
  }
  delete[] printBuf;
  return retval;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants