From d5908f03b7efda982601dca25b7fd1ee02b2c53c Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Mon, 20 Oct 2014 06:33:36 +0200 Subject: [PATCH] Fixed #6226 (false negative: printf format check for user defined array type) --- lib/checkio.cpp | 14 ++++++++++++++ test/testio.cpp | 13 +++++++++++++ 2 files changed, 27 insertions(+) diff --git a/lib/checkio.cpp b/lib/checkio.cpp index 31797d738..8b0424f4a 100644 --- a/lib/checkio.cpp +++ b/lib/checkio.cpp @@ -1549,6 +1549,20 @@ bool CheckIO::ArgumentInfo::isStdVectorOrString() return true; } } + } else if (variableInfo->type()) { + const Scope * classScope = variableInfo->type()->classScope; + if (classScope) { + std::list::const_iterator functions; + for (functions = classScope->functionList.begin(); + functions != classScope->functionList.end(); ++functions) { + if (functions->name() == "operator[]") { + if (Token::Match(functions->retDef, "%type% &")) { + typeToken = functions->retDef; + return true; + } + } + } + } } return false; diff --git a/test/testio.cpp b/test/testio.cpp index 0c87ab4e9..1b2b767bb 100644 --- a/test/testio.cpp +++ b/test/testio.cpp @@ -3100,6 +3100,19 @@ private: ASSERT_EQUALS("[test.cpp:4]: (warning) %s in format string (no. 1) requires 'char *' but the argument type is 'std::string'.\n" "[test.cpp:4]: (warning) %s in format string (no. 2) requires 'char *' but the argument type is 'int'.\n", errout.str()); + check("template \n" + "struct Array {\n" + " T data[S];\n" + " T & operator [] (size_t i) { return data[i]; }\n" + "};\n" + "void foo() {\n" + " Array array1;\n" + " Array array2;\n" + " printf(\"%u %u\", array1[0], array2[0]);\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:9]: (warning) %u in format string (no. 1) requires 'unsigned int' but the argument type is 'int'.\n" + "[test.cpp:9]: (warning) %u in format string (no. 2) requires 'unsigned int' but the argument type is 'float'.\n", errout.str()); + } void testPosixPrintfScanfParameterPosition() { // #4900 - No support for parameters in format strings