Fixed #5046 (False positive: Boolean value assigned to pointer)

This commit is contained in:
Daniel Marjamäki 2013-10-05 08:53:37 +02:00
parent 348f3fa97f
commit f2fdd967f5
2 changed files with 19 additions and 9 deletions

View File

@ -332,12 +332,17 @@ void CheckBool::checkAssignBoolToPointer()
for (std::size_t i = 0; i < functions; ++i) { for (std::size_t i = 0; i < functions; ++i) {
const Scope * scope = symbolDatabase->functionScopes[i]; const Scope * scope = symbolDatabase->functionScopes[i];
for (const Token* tok = scope->classStart; tok != scope->classEnd; tok = tok->next()) { for (const Token* tok = scope->classStart; tok != scope->classEnd; tok = tok->next()) {
if (Token::Match(tok, "!!* %var% = %bool% ;")) { if (Token::Match(tok, "%var% = %bool% ;")) {
const Variable *var1(tok->next()->variable()); // Todo: properly check if there is a deref
// *x.p = true; // <- don't warn
// x.p = true; // <- warn
if (Token::Match(tok->previous(), "[*.)]"))
continue;
// Is variable a pointer? // Is variable a pointer?
const Variable *var1(tok->variable());
if (var1 && var1->isPointer()) if (var1 && var1->isPointer())
assignBoolToPointerError(tok->next()); assignBoolToPointerError(tok);
} }
} }
} }

View File

@ -99,14 +99,19 @@ private:
" bool *p;\n" " bool *p;\n"
"};\n" "};\n"
"void f() {\n" "void f() {\n"
" std::vector<S> sv;\n" " S s = {0};\n"
" sv.push_back(S());\n"
" S &s = sv[0];\n"
" *s.p = true;\n" " *s.p = true;\n"
" *(s.p) = true;\n"
"}\n"); "}\n");
TODO_ASSERT_EQUALS("","[test.cpp:8]: (error) Boolean value assigned to pointer.\n" ASSERT_EQUALS("", errout.str());
"[test.cpp:9]: (error) Boolean value assigned to pointer.\n", errout.str());
check("struct S {\n"
" bool *p;\n"
"};\n"
"void f() {\n"
" S s = {0};\n"
" s.p = true;\n"
"}\n");
TODO_ASSERT_EQUALS("error", "", errout.str());
} }
void comparisonOfBoolExpressionWithInt1() { void comparisonOfBoolExpressionWithInt1() {