Fixed false positive on C code introduced by last commit

This commit is contained in:
PKEuS 2014-05-21 20:36:19 +02:00
parent d19eabde42
commit 4cad064c3c
2 changed files with 38 additions and 6 deletions

View File

@ -2894,7 +2894,7 @@ void CheckOther::checkSuspiciousStringCompare()
const Token* varTok = tok;
const Token* litTok = tok->tokAt(2);
if (varTok->strAt(-1) == "+" || litTok->strAt(1) == "+")
if (!_tokenizer->isC() && (varTok->strAt(-1) == "+" || litTok->strAt(1) == "+"))
continue;
// rough filter for index access (#5734). Might cause false negatives in multidimensional structures
if (Token::simpleMatch(varTok->tokAt(1), "[") || Token::simpleMatch(litTok->tokAt(1), "["))
@ -2906,11 +2906,13 @@ void CheckOther::checkSuspiciousStringCompare()
if (!var)
continue;
if (_tokenizer->isC() ||
(var->isPointer() && varTok->strAt(-1) != "*" && !Token::Match(varTok->next(), "[.([]"))) {
if (litTok->type() == Token::eString)
if (litTok->type() == Token::eString) {
if (_tokenizer->isC() ||
(var->isPointer() && varTok->strAt(-1) != "*" && !Token::Match(varTok->next(), "[.([]")))
suspiciousStringCompareError(tok, var->name());
else if (litTok->type() == Token::eNumber && litTok->originalName() == "'\\0'")
} else if (litTok->type() == Token::eNumber && litTok->originalName() == "'\\0'") {
if (var->isPointer() && varTok->strAt(-1) != "*" && !Token::Match(varTok->next(), "[.([]"))
suspiciousStringCompareError_char(tok, var->name());
}
}

View File

@ -5176,9 +5176,24 @@ private:
check("bool foo(char* c) {\n"
" return \"x\" == c+foo;\n"
"}");
"}", "test.cpp");
ASSERT_EQUALS("", errout.str());
check("bool foo(char* c) {\n"
" return \"x\" == c+foo;\n"
"}", "test.c");
ASSERT_EQUALS("[test.c:2]: (warning) String literal compared with variable 'c'. Did you intend to use strcmp() instead?\n", errout.str());
check("bool foo(Foo c) {\n"
" return \"x\" == c.foo;\n"
"}", "test.cpp");
ASSERT_EQUALS("", errout.str());
check("bool foo(Foo c) {\n"
" return \"x\" == c.foo;\n"
"}", "test.c");
ASSERT_EQUALS("[test.c:2]: (warning) String literal compared with variable 'c'. Did you intend to use strcmp() instead?\n", errout.str());
check("bool foo(const std::string& c) {\n"
" return \"x\" == c;\n"
"}");
@ -5253,6 +5268,21 @@ private:
"}");
ASSERT_EQUALS("", errout.str());
check("bool foo(char* c) {\n"
" return *c == 0;\n"
"}", "test.c");
ASSERT_EQUALS("", errout.str());
check("bool foo(char* c) {\n"
" return *c == 0;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("bool foo(Foo* c) {\n"
" return 0 == c->x;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void foo(char* c) {\n"
" if(c == '\\0') bar();\n"
"}");