Use ValueFlow in CheckIO::checkWrongPrintfScanfArguments() (#6563)

This commit is contained in:
PKEuS 2015-11-06 09:52:22 +01:00
parent 3177d73fb0
commit eefea507b9
2 changed files with 10 additions and 14 deletions

View File

@ -466,18 +466,8 @@ static bool findFormat(unsigned int arg, const Token *firstArg,
argTok->variable()->dimensionKnown(0) && argTok->variable()->dimensionKnown(0) &&
argTok->variable()->dimension(0) != 0))) { argTok->variable()->dimension(0) != 0))) {
*formatArgTok = argTok->nextArgument(); *formatArgTok = argTok->nextArgument();
*formatStringTok = nullptr; if (argTok->values.size() >= 1 && argTok->values.front().tokvalue && argTok->values.front().tokvalue->tokType() == Token::eString)
if (argTok->variable()) { *formatStringTok = argTok->values.front().tokvalue;
const Token *varTok = argTok->variable()->nameToken();
if (Token::Match(varTok, "%name% ; %name% = %str% ;") &&
varTok->str() == varTok->strAt(2) &&
Token::Match(varTok->tokAt(-4), "const char|wchar_t * const")) {
*formatStringTok = varTok->tokAt(4);
} else if (Token::Match(varTok, "%name% [ %num% ] = %str% ;") &&
Token::Match(varTok->tokAt(-2), "const char|wchar_t")) {
*formatStringTok = varTok->tokAt(5);
}
}
return true; return true;
} }
return false; return false;

View File

@ -3559,7 +3559,7 @@ private:
check("void foo() {\n" check("void foo() {\n"
" const char * const format1 = \"%15s%17s%17s%17s%17s\n\";\n" " const char * const format1 = \"%15s%17s%17s%17s%17s\n\";\n"
" const char format2[] = \"%15s%17s%17s%17s%17s\n\";\n" " const char format2[] = \"%15s%17s%17s%17s%17s\n\";\n"
" const char * const format3 = format1;\n" // we should warn about this someday " const char * const format3 = format1;\n"
" int i = 0;\n" " int i = 0;\n"
" sprintf_s(lineBuffer, format1, \"type\", \"sum\", \"avg\", \"min\", i, 0);\n" " sprintf_s(lineBuffer, format1, \"type\", \"sum\", \"avg\", \"min\", i, 0);\n"
" sprintf_s(lineBuffer, format2, \"type\", \"sum\", \"avg\", \"min\", i, 0);\n" " sprintf_s(lineBuffer, format2, \"type\", \"sum\", \"avg\", \"min\", i, 0);\n"
@ -3575,14 +3575,20 @@ private:
"[test.cpp:6]: (warning) sprintf_s format string requires 5 parameters but 6 are given.\n" "[test.cpp:6]: (warning) sprintf_s format string requires 5 parameters but 6 are given.\n"
"[test.cpp:7]: (warning) %s in format string (no. 5) requires 'char *' but the argument type is 'int'.\n" "[test.cpp:7]: (warning) %s in format string (no. 5) requires 'char *' but the argument type is 'int'.\n"
"[test.cpp:7]: (warning) sprintf_s format string requires 5 parameters but 6 are given.\n" "[test.cpp:7]: (warning) sprintf_s format string requires 5 parameters but 6 are given.\n"
"[test.cpp:8]: (warning) %s in format string (no. 5) requires 'char *' but the argument type is 'int'.\n"
"[test.cpp:8]: (warning) sprintf_s format string requires 5 parameters but 6 are given.\n"
"[test.cpp:9]: (warning) %s in format string (no. 5) requires 'char *' but the argument type is 'int'.\n" "[test.cpp:9]: (warning) %s in format string (no. 5) requires 'char *' but the argument type is 'int'.\n"
"[test.cpp:9]: (warning) sprintf format string requires 5 parameters but 6 are given.\n" "[test.cpp:9]: (warning) sprintf format string requires 5 parameters but 6 are given.\n"
"[test.cpp:10]: (warning) %s in format string (no. 5) requires 'char *' but the argument type is 'int'.\n" "[test.cpp:10]: (warning) %s in format string (no. 5) requires 'char *' but the argument type is 'int'.\n"
"[test.cpp:10]: (warning) sprintf format string requires 5 parameters but 6 are given.\n" "[test.cpp:10]: (warning) sprintf format string requires 5 parameters but 6 are given.\n"
"[test.cpp:11]: (warning) %s in format string (no. 5) requires 'char *' but the argument type is 'int'.\n"
"[test.cpp:11]: (warning) sprintf format string requires 5 parameters but 6 are given.\n"
"[test.cpp:12]: (warning) %s in format string (no. 5) requires 'char *' but the argument type is 'int'.\n" "[test.cpp:12]: (warning) %s in format string (no. 5) requires 'char *' but the argument type is 'int'.\n"
"[test.cpp:12]: (warning) printf format string requires 5 parameters but 6 are given.\n" "[test.cpp:12]: (warning) printf format string requires 5 parameters but 6 are given.\n"
"[test.cpp:13]: (warning) %s in format string (no. 5) requires 'char *' but the argument type is 'int'.\n" "[test.cpp:13]: (warning) %s in format string (no. 5) requires 'char *' but the argument type is 'int'.\n"
"[test.cpp:13]: (warning) printf format string requires 5 parameters but 6 are given.\n", errout.str()); "[test.cpp:13]: (warning) printf format string requires 5 parameters but 6 are given.\n"
"[test.cpp:14]: (warning) %s in format string (no. 5) requires 'char *' but the argument type is 'int'.\n"
"[test.cpp:14]: (warning) printf format string requires 5 parameters but 6 are given.\n", errout.str());
} }