diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index 56d256b0b..b10522b3a 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -475,6 +475,9 @@ void CheckNullPointer::arithmetic() continue; if (numericOperand && numericOperand->valueType() && !numericOperand->valueType()->isIntegral()) continue; + const ValueFlow::Value* numValue = numericOperand ? numericOperand->getValue(0) : nullptr; + if (numValue && numValue->intvalue == 0) // don't warn for arithmetic with 0 + continue; const ValueFlow::Value* value = pointerOperand->getValue(0); if (!value) continue; diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index f2705009d..239be7e6e 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -4209,6 +4209,17 @@ private: "const char* get() const { return 0; }\n" "void f(foo x) { if (get()) x += get(); }"); ASSERT_EQUALS("", errout.str()); + + check("typedef struct { uint8_t* buf, *buf_end; } S;\n" // #11117 + "void f(S* s, uint8_t* buffer, int buffer_size) {\n" + " if (buffer_size < 0) {\n" + " buffer_size = 0;\n" + " buffer = NULL;\n" + " }\n" + " s->buf = buffer;\n" + " s->buf_end = s->buf + buffer_size;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void isPointerDeRefFunctionDecl() {