Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/checknullpointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ void CheckNullPointerImpl::nullPointerError(const Token *tok, const std::string
} else if (value->defaultArg) {
reportError(std::move(errorPath), Severity::warning, "nullPointerDefaultArg", errmsgdefarg, CWE_NULL_POINTER_DEREFERENCE, inconclusive || value->isInconclusive() ? Certainty::inconclusive : Certainty::normal);
} else {
std::string errmsg = std::string(value->isKnown() ? "Null" : "Possible null") + " pointer dereference";
std::string errmsg = std::string(value->isKnown() && !value->conditional ? "Null" : "Possible null") + " pointer dereference";

std::string id = "nullPointer";
if (value->unknownFunctionReturn == ValueFlow::Value::UnknownFunctionReturn::outOfMemory) {
Expand All @@ -470,7 +470,7 @@ void CheckNullPointerImpl::nullPointerError(const Token *tok, const std::string
errmsg = "$symbol:" + varname + '\n' + errmsg + ": $symbol";

reportError(std::move(errorPath),
value->isKnown() ? Severity::error : Severity::warning,
value->isKnown() && !value->conditional ? Severity::error : Severity::warning,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

conditional is only set by makeConditional() when the forward analyzer assumes an if condition. So the severity now depends on how the code is written, not on how certain the value is:

int f(bool b) { int* p = nullptr; if (b) { return *p; } return 0; }    // warning
int g(bool b) { int* p = nullptr; while (b) { return *p; } return 0; } // error
int h(bool b) { int* p = nullptr; return b ? *p : 0; }                 // error
int k(bool b) { int* p = nullptr; return b && *p; }                    // error

The message on line 457 is still chosen from isKnown() alone, so these warnings say "Null pointer dereference" instead of "Possible null pointer dereference". The same id and the same text now come at two severities.

In all of these cases p is null on every path that reaches the dereference. If the goal is consistent severity, it should probably not depend on the conditional flag.

id.c_str(),
errmsg,
CWE_NULL_POINTER_DEREFERENCE, inconclusive || value->isInconclusive() ? Certainty::inconclusive : Certainty::normal);
Expand Down
28 changes: 23 additions & 5 deletions test/testnullpointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ class TestNullPointer : public TestFixture {
TEST_CASE(nullpointerSmartPointer);
TEST_CASE(nullpointerOutOfMemory);
TEST_CASE(nullpointerOutOfResources);
TEST_CASE(nullpointerErrorPath);
TEST_CASE(functioncall);
TEST_CASE(functioncalllibrary); // use Library to parse function call
TEST_CASE(functioncallDefaultArguments);
Expand Down Expand Up @@ -2488,7 +2489,7 @@ class TestNullPointer : public TestFixture {
" *p = 0;\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4:10]: (error) Null pointer dereference: p [nullPointer]\n", errout_str());
ASSERT_EQUALS("[test.cpp:4:10]: (warning) Possible null pointer dereference: p [nullPointer]\n", errout_str());
}

void nullpointer78() // #7802
Expand Down Expand Up @@ -4100,10 +4101,10 @@ class TestNullPointer : public TestFixture {
" foo(p == s2);\n"
" foo(p == s3);\n"
"}\n", dinit(CheckOptions, $.inconclusive = true));
ASSERT_EQUALS("[test.cpp:4:15]: (error) Null pointer dereference: p [nullPointer]\n"
"[test.cpp:5:15]: (error) Null pointer dereference: p [nullPointer]\n"
"[test.cpp:7:9]: (error) Null pointer dereference: p [nullPointer]\n"
"[test.cpp:8:9]: (error) Null pointer dereference: p [nullPointer]\n", errout_str());
ASSERT_EQUALS("[test.cpp:4:15]: (warning) Possible null pointer dereference: p [nullPointer]\n"
"[test.cpp:5:15]: (warning) Possible null pointer dereference: p [nullPointer]\n"
"[test.cpp:7:9]: (warning) Possible null pointer dereference: p [nullPointer]\n"
"[test.cpp:8:9]: (warning) Possible null pointer dereference: p [nullPointer]\n", errout_str());

check("void f(std::string s1, const std::string& s2, const std::string* s3) {\n"
" void* p = 0;\n"
Expand Down Expand Up @@ -4586,6 +4587,23 @@ class TestNullPointer : public TestFixture {
errout_str());
}

void nullpointerErrorPath() {
setMultiline();

check("int f(bool b) {\n"
" int* p = nullptr;\n"
" if (b)\n"
" return 0;\n"
" return *p;\n"
"}\n");
ASSERT_EQUALS(
"[test.cpp:5:13]: warning: Possible null pointer dereference: p [nullPointer]\n"
"[test.cpp:2:14]: note: Assignment 'p=nullptr', assigned value is 0\n"
"[test.cpp:3:9]: note: Assuming condition is false\n"
"[test.cpp:5:13]: note: Null pointer dereference\n",
errout_str());
}

void functioncalllibrary() {
SimpleTokenizer tokenizer(settingsDefault,*this,false);
const char code[] = "void f() { int a,b,c; x(a,b,c); }\n";
Expand Down
Loading