From 344968413707f5b1b5fce02630af80c0b52b634e Mon Sep 17 00:00:00 2001 From: PKEuS Date: Mon, 10 Sep 2012 16:23:00 +0200 Subject: [PATCH] Fixed usage of _settigns->isEnabled("style") in CheckIO. Added forgotten test cases. --- lib/checkio.cpp | 10 ++++++---- test/testio.cpp | 10 ++++++++++ test/testsymboldatabase.cpp | 2 ++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/checkio.cpp b/lib/checkio.cpp index f7f60cbe3..0dd29f1cb 100644 --- a/lib/checkio.cpp +++ b/lib/checkio.cpp @@ -399,6 +399,7 @@ static bool isKnownType(const Variable* var, const Token* varTypeTok) void CheckIO::checkWrongPrintfScanfArguments() { const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase(); + bool warning = _settings->isEnabled("style"); for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { if (!tok->isName()) continue; @@ -492,14 +493,14 @@ void CheckIO::checkWrongPrintfScanfArguments() numFormat++; // Perform type checks - if (_settings->isEnabled("style") && argListTok && Token::Match(argListTok->next(), "[,)]")) { // We can currently only check the type of arguments matching this simple pattern. + if (argListTok && Token::Match(argListTok->next(), "[,)]")) { // We can currently only check the type of arguments matching this simple pattern. const Variable* variableInfo = symbolDatabase->getVariableFromVarId(argListTok->varId()); const Token* varTypeTok = variableInfo ? variableInfo->typeStartToken() : NULL; if (varTypeTok && varTypeTok->str() == "static") varTypeTok = varTypeTok->next(); if (scan && varTypeTok) { - if ((!variableInfo->isPointer() && !variableInfo->isArray()) || varTypeTok->strAt(-1) == "const") + if (warning && ((!variableInfo->isPointer() && !variableInfo->isArray()) || varTypeTok->strAt(-1) == "const")) invalidScanfArgTypeError(tok, tok->str(), numFormat); if (*i == 's' && variableInfo && isKnownType(variableInfo, varTypeTok) && variableInfo->isArray() && (variableInfo->dimensions().size() == 1) && variableInfo->dimensions()[0].known) { @@ -509,7 +510,7 @@ void CheckIO::checkWrongPrintfScanfArguments() invalidScanfFormatWidthError(tok, numFormat, numWidth, variableInfo); } } - } else if (!scan) { + } else if (!scan && warning) { switch (*i) { case 's': if (variableInfo && argListTok->type() != Token::eString && isKnownType(variableInfo, varTypeTok) && (!variableInfo->isPointer() && !variableInfo->isArray())) @@ -675,5 +676,6 @@ void CheckIO::invalidScanfFormatWidthError(const Token* tok, unsigned int numFor } else errmsg << "Width " << width << " given in format string (no. " << numFormat << ") doesn't match destination buffer."; - reportError(tok, severity, "invalidScanfFormatWidth", errmsg.str(), inconclusive); + if (severity == Severity::error || _settings->isEnabled("style")) + reportError(tok, severity, "invalidScanfFormatWidth", errmsg.str(), inconclusive); } diff --git a/test/testio.cpp b/test/testio.cpp index 1d7332b3b..d2e42f516 100644 --- a/test/testio.cpp +++ b/test/testio.cpp @@ -473,6 +473,16 @@ private: ASSERT_EQUALS("[test.cpp:5]: (warning, inconclusive) Width 3 given in format string (no. 1) is smaller than destination buffer 'output[5]'.\n" "[test.cpp:7]: (error) Width 5 given in format string (no. 1) is larger than destination buffer 'output[5]', use %4s to prevent overflowing it.\n" "[test.cpp:4]: (warning) scanf without field width limits can crash with huge input data.\n", errout.str()); + + check("void foo() {\n" + " const size_t BUFLENGTH(2048);\n" + " typedef char bufT[BUFLENGTH];\n" + " bufT line= {0};\n" + " bufT projectId= {0};\n" + " const int scanrc=sscanf(line, \"Project(\\\"{%36s}\\\")\", projectId);\n" + " sscanf(input, \"%5s\", output);\n" + "}", true); + ASSERT_EQUALS("", errout.str()); } void testPrintfArgument() { diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index cf185a053..4ef9b04aa 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -872,7 +872,9 @@ private: ASSERT_EQUALS("a", a->nameToken()->str()); ASSERT_EQUALS(2UL, a->dimensions().size()); ASSERT_EQUALS(0UL, a->dimension(0)); + ASSERT_EQUALS(false, a->dimensions()[0].known); ASSERT_EQUALS(4UL, a->dimension(1)); + ASSERT_EQUALS(true, a->dimensions()[1].known); } void functionArgs3() {