Fixed usage of _settigns->isEnabled("style") in CheckIO. Added forgotten test cases.

This commit is contained in:
PKEuS 2012-09-10 16:23:00 +02:00
parent e9f13e1547
commit 3449684137
3 changed files with 18 additions and 4 deletions

View File

@ -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);
}

View File

@ -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() {

View File

@ -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() {