Fixed #4257 (False Positive: String literal compared with variable - for non-pointer variable)

This commit is contained in:
Alexander Mai 2012-10-07 16:26:03 +02:00 committed by Daniel Marjamäki
parent 0115bb8d24
commit 3656366c7e
2 changed files with 35 additions and 2 deletions

View File

@ -19,6 +19,7 @@
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
#include "checkother.h" #include "checkother.h"
#include "checknullpointer.h" // CheckNullPointer::isPointerDeRef()
#include "mathlib.h" #include "mathlib.h"
#include "symboldatabase.h" #include "symboldatabase.h"
@ -3107,8 +3108,12 @@ void CheckOther::checkSuspiciousStringCompare()
std::swap(varTok, litTok); std::swap(varTok, litTok);
const Variable* var = symbolDatabase->getVariableFromVarId(varTok->varId()); const Variable* var = symbolDatabase->getVariableFromVarId(varTok->varId());
if (var && var->isPointer()) if (var) {
suspiciousStringCompareError(tok, var->name()); bool unknown=false;
if (_tokenizer->isC() ||
(var->isPointer() && !CheckNullPointer::isPointerDeRef(tok, unknown, symbolDatabase)))
suspiciousStringCompareError(tok, var->name());
}
} }
} }
} }

View File

@ -5170,6 +5170,34 @@ private:
" return \"x\" == c;\n" " return \"x\" == c;\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
// Ticket #4257
check("bool foo() {\n"
"MyString *str=Getter();\n"
"return *str==\"bug\"; }\n", "test.c");
ASSERT_EQUALS("[test.c:3]: (warning) String literal compared with variable 'str'. Did you intend to use strcmp() instead?\n", errout.str());
// Ticket #4257
check("bool foo() {\n"
"MyString *str=Getter();\n"
"return *str==\"bug\"; }\n");
ASSERT_EQUALS("", errout.str());
// Ticket #4257
check("bool foo() {\n"
"MyString **str=OtherGetter();\n"
"return *str==\"bug\"; }\n");
TODO_ASSERT_EQUALS("[test.cpp:2]: (warning) String literal compared with variable 'c'. Did you intend to use strcmp() instead?\n",
"",
errout.str());
// Ticket #4257
check("bool foo() {\n"
"MyString str=OtherGetter2();\n"
"return &str==\"bug\"; }\n");
TODO_ASSERT_EQUALS("[test.cpp:2]: (warning) String literal compared with variable 'c'. Did you intend to use strcmp() instead?\n",
"",
errout.str());
} }
void checkPointerSizeof() { void checkPointerSizeof() {