Passing string literals into printf as %p is fine

This commit is contained in:
Dmitry-Me 2017-10-08 11:14:10 +03:00
parent be2c65eb58
commit 9c0e6986da
2 changed files with 12 additions and 1 deletions

View File

@ -1275,7 +1275,7 @@ void CheckIO::checkFormatString(const Token * const tok,
break; break;
case 'p': case 'p':
if (argInfo.typeToken->tokType() == Token::eString) if (argInfo.typeToken->tokType() == Token::eString)
invalidPrintfArgTypeError_p(tok, numFormat, &argInfo); ;// string literals are passed as pointers to literal start, okay
else if (argInfo.isKnownType() && !argInfo.isArrayOrPointer()) else if (argInfo.isKnownType() && !argInfo.isArrayOrPointer())
invalidPrintfArgTypeError_p(tok, numFormat, &argInfo); invalidPrintfArgTypeError_p(tok, numFormat, &argInfo);
done = true; done = true;

View File

@ -1575,6 +1575,17 @@ private:
ASSERT_EQUALS("[test.cpp:3]: (warning) %p in format string (no. 1) requires an address but the argument type is 'foo'.\n" ASSERT_EQUALS("[test.cpp:3]: (warning) %p in format string (no. 1) requires an address but the argument type is 'foo'.\n"
"[test.cpp:4]: (warning) %p in format string (no. 1) requires an address but the argument type is 'char'.\n", errout.str()); "[test.cpp:4]: (warning) %p in format string (no. 1) requires an address but the argument type is 'char'.\n", errout.str());
check("class foo {};\n"
"void foo(char* pc, const char* cpc, wchar_t* pwc, const wchar_t* cpwc) {\n"
" printf(\"%p\", pc);\n"
" printf(\"%p\", cpc);\n"
" printf(\"%p\", pwc);\n"
" printf(\"%p\", cpwc);\n"
" printf(\"%p\", \"s4\");\n"
" printf(\"%p\", L\"s5W\");\n"
"}");
ASSERT_EQUALS("", errout.str());
check("class foo {};\n" check("class foo {};\n"
"void foo(const int* cpi, foo f, bar b, bar* bp, double d) {\n" "void foo(const int* cpi, foo f, bar b, bar* bp, double d) {\n"
" printf(\"%e\", f);\n" " printf(\"%e\", f);\n"