From 0197343e0c89c6c134157558ba58a136bb89ff36 Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Wed, 9 May 2018 02:06:49 -0500 Subject: [PATCH] Fix false positive when using null arithmetic with class type (#1214) --- lib/checknullpointer.cpp | 11 +++++++++-- test/testnullpointer.cpp | 5 +++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index 19ee83679..11ee461cb 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -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 diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index 0982cc05e..8932fcf6b 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -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()); } };