Skip to content

Commit 72c775a

Browse files
authored
Fixup danmar#4626: tweak error message 'If memory allocation fails, then there is a possible null pointer dereference' (danmar#7087)
1 parent 9d64da9 commit 72c775a

File tree

2 files changed

+4
-10
lines changed

2 files changed

+4
-10
lines changed

lib/checknullpointer.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -466,17 +466,11 @@ void CheckNullPointer::nullPointerError(const Token *tok, const std::string &var
466466

467467
std::string id = "nullPointer";
468468
if (value->unknownFunctionReturn == ValueFlow::Value::UnknownFunctionReturn::outOfMemory) {
469-
if (errmsg.compare(0, 9, "Possible ") == 0)
470-
errmsg = "If memory allocation fail: " + errmsg.substr(9);
471-
else
472-
errmsg = "If memory allocation fail: " + errmsg;
469+
errmsg = "If memory allocation fails, then there is a " + ((char)std::tolower(errmsg[0]) + errmsg.substr(1));
473470
id += "OutOfMemory";
474471
}
475472
else if (value->unknownFunctionReturn == ValueFlow::Value::UnknownFunctionReturn::outOfResources) {
476-
if (errmsg.compare(0, 9, "Possible ") == 0)
477-
errmsg = "If resource allocation fail: " + errmsg.substr(9);
478-
else
479-
errmsg = "If resource allocation fail: " + errmsg;
473+
errmsg = "If resource allocation fails, then there is a " + ((char)std::tolower(errmsg[0]) + errmsg.substr(1));
480474
id += "OutOfResources";
481475
}
482476

test/testnullpointer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2769,7 +2769,7 @@ class TestNullPointer : public TestFixture {
27692769
" (*kep)->next = 0;\n"
27702770
" (*kep)->len = slen;\n"
27712771
"}\n");
2772-
ASSERT_EQUALS("[test.cpp:6]: (warning) If memory allocation fail: null pointer dereference: *kep\n", errout_str());
2772+
ASSERT_EQUALS("[test.cpp:6]: (warning) If memory allocation fails, then there is a possible null pointer dereference: *kep\n", errout_str());
27732773
}
27742774

27752775
void nullpointer95() // #11142
@@ -4087,7 +4087,7 @@ class TestNullPointer : public TestFixture {
40874087
" *p = 0;\n"
40884088
" free(p);\n"
40894089
"}");
4090-
ASSERT_EQUALS("[test.cpp:3]: (warning) If memory allocation fail: null pointer dereference: p\n", errout_str());
4090+
ASSERT_EQUALS("[test.cpp:3]: (warning) If memory allocation fails, then there is a possible null pointer dereference: p\n", errout_str());
40914091

40924092
check("void f() {\n"
40934093
" int *p = malloc(10);\n"

0 commit comments

Comments
 (0)