Fix false positive when using null arithmetic with class type (#1214)
This commit is contained in:
parent
f5dbfce8ff
commit
0197343e0c
|
@ -528,12 +528,19 @@ void CheckNullPointer::arithmetic()
|
||||||
if (!Token::Match(tok, "-|+|+=|-=|++|--"))
|
if (!Token::Match(tok, "-|+|+=|-=|++|--"))
|
||||||
continue;
|
continue;
|
||||||
const Token *pointerOperand;
|
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();
|
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();
|
pointerOperand = tok->astOperand2();
|
||||||
|
numericOperand = tok->astOperand1();
|
||||||
|
}
|
||||||
else
|
else
|
||||||
continue;
|
continue;
|
||||||
|
if (numericOperand && numericOperand->valueType() && !numericOperand->valueType()->isIntegral())
|
||||||
|
continue;
|
||||||
MathLib::bigint checkValue = 0;
|
MathLib::bigint checkValue = 0;
|
||||||
// When using an assign op, the value read from
|
// When using an assign op, the value read from
|
||||||
// valueflow has already been updated, so instead of
|
// valueflow has already been updated, so instead of
|
||||||
|
|
|
@ -2649,6 +2649,11 @@ private:
|
||||||
|
|
||||||
check("int* f10() { int *x = NULL; return x++; } ");
|
check("int* f10() { int *x = NULL; return x++; } ");
|
||||||
ASSERT_EQUALS("[test.cpp:1]: (error) Pointer addition with NULL pointer.\n", errout.str());
|
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());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue