Fix false positive when using null arithmetic with class type (#1214)

This commit is contained in:
Paul Fultz II 2018-05-09 02:06:49 -05:00 committed by Daniel Marjamäki
parent f5dbfce8ff
commit 0197343e0c
2 changed files with 14 additions and 2 deletions

View File

@ -528,12 +528,19 @@ void CheckNullPointer::arithmetic()
if (!Token::Match(tok, "-|+|+=|-=|++|--"))
continue;
const Token *pointerOperand;
if (tok->astOperand1() && tok->astOperand1()->valueType() && tok->astOperand1()->valueType()->pointer != 0)
const Token *numericOperand;
if (tok->astOperand1() && tok->astOperand1()->valueType() && tok->astOperand1()->valueType()->pointer != 0) {
pointerOperand = tok->astOperand1();
else if (tok->astOperand2() && tok->astOperand2()->valueType() && tok->astOperand2()->valueType()->pointer != 0)
numericOperand = tok->astOperand2();
}
else if (tok->astOperand2() && tok->astOperand2()->valueType() && tok->astOperand2()->valueType()->pointer != 0) {
pointerOperand = tok->astOperand2();
numericOperand = tok->astOperand1();
}
else
continue;
if (numericOperand && numericOperand->valueType() && !numericOperand->valueType()->isIntegral())
continue;
MathLib::bigint checkValue = 0;
// When using an assign op, the value read from
// valueflow has already been updated, so instead of

View File

@ -2649,6 +2649,11 @@ private:
check("int* f10() { int *x = NULL; return x++; } ");
ASSERT_EQUALS("[test.cpp:1]: (error) Pointer addition with NULL pointer.\n", errout.str());
check("class foo {};\n"
"const char* get() const { return 0; }\n"
"void f(foo x) { if (get()) x += get(); }\n");
ASSERT_EQUALS("", errout.str());
}
};