경고 C6284

'*function*'을 호출할 때 문자열이 필요한 경우 매개 변수로 전달되는 개체

설명

이 경고는 형식 지정자와 -style 함수에서 사용되는 형식이 일치하지 않음을 printf나타냅니다. 형식 지정자는 C 스타일 문자열 형식(예: %s or ) %ws이며 인수는 클래스/구조체/공용 구조체 형식입니다. 이 결함으로 인해 잠재적으로 잘못된 출력 외에도 충돌이 발생할 수 있습니다.

이러한 결함은 -style 함수에서 예상하는 C 스타일 printf문자열과 같은 bstr_tstd::stringCComBSTR 개체 문자열 형식을 변환하는 것을 잊어버리기 때문에 자주 발생합니다. 그렇다면 적절한 변환을 형식에 추가하는 것이 수정입니다. -style 함수에 대한 variadic 매개 변수 printf가 형식화되지 않았기 때문에 자동 변환이 발생하지 않으므로 변환이 필요합니다.

코드 분석 이름: OBJECT_AS_STRING_ARGUMENT_TO_FORMAT_FUNCTION

예시

#include <atlbase.h>
#include <string>

void f()
{
  char buff[50];
  CComBSTR bstrValue{"Hello"};
  std::string str{"World"};

  // Oops, %ws and %s require C-style strings but CComBSTR and std::strings are being passed instead
  sprintf(buff, "%ws %s", bstrValue, str);
}

적절한 변환을 추가하여 경고를 수정합니다.

#include <atlbase.h>
#include <string>

void f()
{
  char buff[50];
  CComBSTR bstrValue{"Hello"};
  std::string str{"World"};

  // Fixed by adding a static_cast to the CComBSTR and calling c_str() on the std::string
  sprintf(buff, "%ws %s", static_cast<wchar_t*>(bstrValue), str.c_str());
}

참고 항목

static_cast 연산자
sprintf_s, _sprintf_s_l, swprintf_s, _swprintf_s_l
C4477
C4840