Properly detect binary & in CheckCondition::clarifyCondition() (fixes false positives when self-checking cppcheck)

This commit is contained in:
PKEuS 2016-05-06 17:39:41 +02:00
parent 896582ce56
commit 5d5886b464
2 changed files with 14 additions and 1 deletions

View File

@ -905,7 +905,10 @@ void CheckCondition::clarifyCondition()
} else if (!tok2->isName() && !tok2->isNumber() && tok2->str() != ".")
break;
}
} else if (tok->tokType() == Token::eBitOp) {
} else if (tok->tokType() == Token::eBitOp && (tok->str() != "&" || tok->astOperand2())) {
if (tok->astOperand2() && tok->astOperand2()->variable() && tok->astOperand2()->variable()->nameToken() == tok->astOperand2())
continue;
// using boolean result in bitwise operation ! x [&|^]
const ValueType* vt1 = tok->astOperand1() ? tok->astOperand1()->valueType() : nullptr;
const ValueType* vt2 = tok->astOperand2() ? tok->astOperand2()->valueType() : nullptr;

View File

@ -77,6 +77,7 @@ private:
TEST_CASE(clarifyCondition4); // ticket #3110
TEST_CASE(clarifyCondition5); // #3609 CWinTraits<WS_CHILD|WS_VISIBLE>..
TEST_CASE(clarifyCondition6); // #3818
TEST_CASE(clarifyCondition7);
TEST_CASE(alwaysTrue);
@ -1568,6 +1569,15 @@ private:
ASSERT_EQUALS("", errout.str());
}
void clarifyCondition7() {
// Ensure that binary and unary &, and & in declarations are distinguished properly
check("void f(bool error) {\n"
" bool & withoutSideEffects=found.first->second;\n" // Declaring a reference to a boolean; & is no operator at all
" execute(secondExpression, &programMemory, &result, &error);\n" // Unary &
"}");
ASSERT_EQUALS("", errout.str());
}
void testBug5895() {
check("void png_parse(uint64_t init, int buf_size) {\n"
" if (init == 0x89504e470d0a1a0a || init == 0x8a4d4e470d0a1a0a)\n"