Skip to content

Commit f2fdd96

Browse files
committed
Fixed danmar#5046 (False positive: Boolean value assigned to pointer)
1 parent 348f3fa commit f2fdd96

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

lib/checkbool.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -332,12 +332,17 @@ void CheckBool::checkAssignBoolToPointer()
332332
for (std::size_t i = 0; i < functions; ++i) {
333333
const Scope * scope = symbolDatabase->functionScopes[i];
334334
for (const Token* tok = scope->classStart; tok != scope->classEnd; tok = tok->next()) {
335-
if (Token::Match(tok, "!!* %var% = %bool% ;")) {
336-
const Variable *var1(tok->next()->variable());
335+
if (Token::Match(tok, "%var% = %bool% ;")) {
336+
// Todo: properly check if there is a deref
337+
// *x.p = true; // <- don't warn
338+
// x.p = true; // <- warn
339+
if (Token::Match(tok->previous(), "[*.)]"))
340+
continue;
337341

338342
// Is variable a pointer?
343+
const Variable *var1(tok->variable());
339344
if (var1 && var1->isPointer())
340-
assignBoolToPointerError(tok->next());
345+
assignBoolToPointerError(tok);
341346
}
342347
}
343348
}

test/testbool.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,19 @@ class TestBool : public TestFixture {
9999
" bool *p;\n"
100100
"};\n"
101101
"void f() {\n"
102-
" std::vector<S> sv;\n"
103-
" sv.push_back(S());\n"
104-
" S &s = sv[0];\n"
102+
" S s = {0};\n"
105103
" *s.p = true;\n"
106-
" *(s.p) = true;\n"
107104
"}\n");
108-
TODO_ASSERT_EQUALS("","[test.cpp:8]: (error) Boolean value assigned to pointer.\n"
109-
"[test.cpp:9]: (error) Boolean value assigned to pointer.\n", errout.str());
105+
ASSERT_EQUALS("", errout.str());
106+
107+
check("struct S {\n"
108+
" bool *p;\n"
109+
"};\n"
110+
"void f() {\n"
111+
" S s = {0};\n"
112+
" s.p = true;\n"
113+
"}\n");
114+
TODO_ASSERT_EQUALS("error", "", errout.str());
110115
}
111116

112117
void comparisonOfBoolExpressionWithInt1() {

0 commit comments

Comments
 (0)