From a6ef3a224a697edc0166167753e8c56e972e953e Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Fri, 8 Nov 2013 12:44:05 +0100 Subject: [PATCH] Fixed #5151 (false negative: improper formatstring - vector::at()) --- lib/checkio.cpp | 14 ++++++++++++++ test/testio.cpp | 7 +++++++ 2 files changed, 21 insertions(+) diff --git a/lib/checkio.cpp b/lib/checkio.cpp index 50721987c..531391570 100644 --- a/lib/checkio.cpp +++ b/lib/checkio.cpp @@ -1398,6 +1398,20 @@ CheckIO::ArgumentInfo::ArgumentInfo(const Token * tok, const Settings *settings) tempToken->insertToken("wchar_t"); } typeToken = tempToken; + return; + } + + // check for std::vector::at() and std::string::at() + else if (Token::Match(tok1->previous(), "%var% . at (") && + Token::Match(tok1->linkAt(2), ") [,)]")) { + varTok = tok1->previous(); + variableInfo = varTok->variable(); + + if (!isStdVectorOrString()) { + variableInfo = 0; + typeToken = 0; + } + return; } else if (!(tok1->str() == "." || tok1->type() == Token::eVariable || tok1->type() == Token::eFunction)) return; diff --git a/test/testio.cpp b/test/testio.cpp index 13eac2374..5c60abb1a 100644 --- a/test/testio.cpp +++ b/test/testio.cpp @@ -2091,6 +2091,13 @@ private: "}\n"); ASSERT_EQUALS("", errout.str()); + check("std::vector v;\n" // #5151 + "void foo() {\n" + " printf(\"%c %u %f\", v.at(32), v.at(32), v.at(32));\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:3]: (warning) %u in format string (no. 2) requires 'unsigned int' but the argument type is 'char'.\n" + "[test.cpp:3]: (warning) %f in format string (no. 3) requires 'double' but the argument type is 'char'.\n", errout.str()); + } void testPosixPrintfScanfParameterPosition() { // #4900 - No support for parameters in format strings