From eefea507b9330e57c55ab1ec0c0f74ba9498a702 Mon Sep 17 00:00:00 2001 From: PKEuS Date: Fri, 6 Nov 2015 09:52:22 +0100 Subject: [PATCH] Use ValueFlow in CheckIO::checkWrongPrintfScanfArguments() (#6563) --- lib/checkio.cpp | 14 ++------------ test/testio.cpp | 10 ++++++++-- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/lib/checkio.cpp b/lib/checkio.cpp index 98cdb53d6..a7f9dc2c6 100644 --- a/lib/checkio.cpp +++ b/lib/checkio.cpp @@ -466,18 +466,8 @@ static bool findFormat(unsigned int arg, const Token *firstArg, argTok->variable()->dimensionKnown(0) && argTok->variable()->dimension(0) != 0))) { *formatArgTok = argTok->nextArgument(); - *formatStringTok = nullptr; - if (argTok->variable()) { - 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); - } - } + if (argTok->values.size() >= 1 && argTok->values.front().tokvalue && argTok->values.front().tokvalue->tokType() == Token::eString) + *formatStringTok = argTok->values.front().tokvalue; return true; } return false; diff --git a/test/testio.cpp b/test/testio.cpp index 1e2e00c43..317f826b5 100644 --- a/test/testio.cpp +++ b/test/testio.cpp @@ -3559,7 +3559,7 @@ private: check("void foo() {\n" " const char * const format1 = \"%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" " sprintf_s(lineBuffer, format1, \"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: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: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) 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) 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) 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) 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()); }