#5639 String literal compared with char buffer in a struct.

This commit is contained in:
amai2012 2014-08-23 09:41:40 +02:00
parent f01d7543f6
commit 976966fe81
2 changed files with 19 additions and 1 deletions

View File

@ -2960,7 +2960,7 @@ void CheckOther::checkSuspiciousStringCompare()
const std::string varname = varTok->expressionString(); const std::string varname = varTok->expressionString();
if (litTok->type() == Token::eString) { if (litTok->type() == Token::eString) {
if (_tokenizer->isC() || (var && var->isPointer())) if (_tokenizer->isC() || (var && var->isArrayOrPointer()))
suspiciousStringCompareError(tok, varname); suspiciousStringCompareError(tok, varname);
} else if (litTok->originalName() == "'\\0'" && var && var->isPointer()) { } else if (litTok->originalName() == "'\\0'" && var && var->isPointer()) {
suspiciousStringCompareError_char(tok, varname); suspiciousStringCompareError_char(tok, varname);

View File

@ -5327,6 +5327,24 @@ private:
check("int foo(char c) {\n" check("int foo(char c) {\n"
"return c == \"42\"[0];}", "test.c", false, true, false, false); "return c == \"42\"[0];}", "test.c", false, true, false, false);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
// 5639 String literal compared with char buffer in a struct
check("struct Example {\n"
" char buffer[200];\n"
"};\n"
"void foo() {\n"
" struct Example example;\n"
" if (example.buffer == \"test\") ;\n"
"}\n", "test.cpp", false, true, false, false);
ASSERT_EQUALS("[test.cpp:6]: (warning) String literal compared with variable 'example.buffer'. Did you intend to use strcmp() instead?\n", errout.str());
check("struct Example {\n"
" char buffer[200];\n"
"};\n"
"void foo() {\n"
" struct Example example;\n"
" if (example.buffer == \"test\") ;\n"
"}\n", "test.c", false, true, false, false);
ASSERT_EQUALS("[test.c:6]: (warning) String literal compared with variable 'example.buffer'. Did you intend to use strcmp() instead?\n", errout.str());
} }
void suspiciousStringCompare_char() { void suspiciousStringCompare_char() {