From f75a841b8aae71069dd08cc58a915c87177af556 Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Sat, 1 Mar 2025 22:17:29 +0100 Subject: [PATCH 1/5] Fix #13676 FN nullPointerOutOfMemory with std::malloc --- lib/checknullpointer.cpp | 4 ++-- lib/library.cpp | 4 ++++ test/testnullpointer.cpp | 9 ++++++++- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index 1f9171a7ebb..7ef6b03d4ea 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -546,11 +546,11 @@ void CheckNullPointer::pointerArithmeticError(const Token* tok, const ValueFlow: std::string id = "nullPointerArithmetic"; if (value && value->unknownFunctionReturn == ValueFlow::Value::UnknownFunctionReturn::outOfMemory) { - errmsg = "If memory allocation fail: " + ((char)std::tolower(errmsg[0]) + errmsg.substr(1)); + errmsg = "If memory allocation fails: " + ((char)std::tolower(errmsg[0]) + errmsg.substr(1)); id += "OutOfMemory"; } else if (value && value->unknownFunctionReturn == ValueFlow::Value::UnknownFunctionReturn::outOfResources) { - errmsg = "If resource allocation fail: " + ((char)std::tolower(errmsg[0]) + errmsg.substr(1)); + errmsg = "If resource allocation fails: " + ((char)std::tolower(errmsg[0]) + errmsg.substr(1)); id += "OutOfResources"; } diff --git a/lib/library.cpp b/lib/library.cpp index 9b6c46f4410..948dd5e38e4 100644 --- a/lib/library.cpp +++ b/lib/library.cpp @@ -1597,6 +1597,10 @@ const std::string& Library::returnValue(const Token *ftok) const const std::string& Library::returnValueType(const Token *ftok) const { + while (Token::simpleMatch(ftok, "::")) + ftok = ftok->astOperand2() ? ftok->astOperand2() : ftok->astOperand1(); + if (!ftok) + return nullptr; if (isNotLibraryFunction(ftok)) { if (Token::simpleMatch(ftok->astParent(), ".") && ftok->astParent()->astOperand1()) { const Token* contTok = ftok->astParent()->astOperand1(); diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index 33352085208..31ac0abc492 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -4101,7 +4101,14 @@ class TestNullPointer : public TestFixture { " *(p+2) = 0;\n" " free(p);\n" "}"); - ASSERT_EQUALS("[test.cpp:3]: (error) If memory allocation fail: pointer addition with NULL pointer.\n", errout_str()); + ASSERT_EQUALS("[test.cpp:3]: (error) If memory allocation fails: pointer addition with NULL pointer.\n", errout_str()); + + check("void f() {\n" // #13676 + " int* q = static_cast(std::malloc(4));\n" + " *q = 0;\n" + " std::free(q);\n" + "}"); + ASSERT_EQUALS("[test.cpp:3]: (warning) If memory allocation fails, then there is a possible null pointer dereference: q\n", errout_str()); } void functioncall() { // #3443 - function calls From 66b287cbe46067063fed08e318e9e9ea9efe1e11 Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Sat, 1 Mar 2025 22:19:59 +0100 Subject: [PATCH 2/5] Fix --- lib/library.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/library.cpp b/lib/library.cpp index 948dd5e38e4..b6f801589d3 100644 --- a/lib/library.cpp +++ b/lib/library.cpp @@ -1600,7 +1600,7 @@ const std::string& Library::returnValueType(const Token *ftok) const while (Token::simpleMatch(ftok, "::")) ftok = ftok->astOperand2() ? ftok->astOperand2() : ftok->astOperand1(); if (!ftok) - return nullptr; + return mEmptyString; if (isNotLibraryFunction(ftok)) { if (Token::simpleMatch(ftok->astParent(), ".") && ftok->astParent()->astOperand1()) { const Token* contTok = ftok->astParent()->astOperand1(); From d560102d3974fc3c0128c7e6d31cce6bc9c4064d Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Sat, 1 Mar 2025 22:35:13 +0100 Subject: [PATCH 3/5] Fix test --- test/cfg/opencv2.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/cfg/opencv2.cpp b/test/cfg/opencv2.cpp index 56eacfd5210..a92b20a3729 100644 --- a/test/cfg/opencv2.cpp +++ b/test/cfg/opencv2.cpp @@ -43,7 +43,7 @@ void ignoredReturnValue() void memleak() { - // cppcheck-suppress cstyleCast + // cppcheck-suppress [cstyleCast, nullPointerOutOfMemory] const char * pBuf = (char *)cv::fastMalloc(1000); // cppcheck-suppress [uninitdata, valueFlowBailoutIncompleteVar] std::cout << pBuf; From abfeb8f369b014dd442ce377924072bb151941a6 Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Sat, 1 Mar 2025 22:54:11 +0100 Subject: [PATCH 4/5] Fix --- test/cfg/opencv2.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/cfg/opencv2.cpp b/test/cfg/opencv2.cpp index a92b20a3729..6e79caab8d0 100644 --- a/test/cfg/opencv2.cpp +++ b/test/cfg/opencv2.cpp @@ -43,9 +43,9 @@ void ignoredReturnValue() void memleak() { - // cppcheck-suppress [cstyleCast, nullPointerOutOfMemory] + // cppcheck-suppress cstyleCast const char * pBuf = (char *)cv::fastMalloc(1000); - // cppcheck-suppress [uninitdata, valueFlowBailoutIncompleteVar] + // cppcheck-suppress [uninitdata, valueFlowBailoutIncompleteVar, nullPointerOutOfMemory] std::cout << pBuf; // cppcheck-suppress memleak } From 6faa593a8f54fb254b98a93eebbf920d0ec3b980 Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Sat, 1 Mar 2025 22:59:10 +0100 Subject: [PATCH 5/5] Fix --- test/cfg/std.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/test/cfg/std.cpp b/test/cfg/std.cpp index 48c4efc4418..a3345425d41 100644 --- a/test/cfg/std.cpp +++ b/test/cfg/std.cpp @@ -1854,6 +1854,7 @@ void uninitar_fopen(void) const char *mode; // cppcheck-suppress uninitvar FILE * fp = std::fopen(filename, mode); + // cppcheck-suppress nullPointerOutOfResources fclose(fp); }