From ada881ccdfc040c587f3519c5481a0f15b20594f Mon Sep 17 00:00:00 2001 From: Rikard Falkeborn Date: Sat, 18 May 2019 06:22:25 +0200 Subject: [PATCH] Fix #9130 (FP memory leak with NULL pointer cast) (#1839) --- lib/astutils.cpp | 2 +- test/testleakautovar.cpp | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index e84263c36..ed9c1e849 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -131,7 +131,7 @@ static bool match(const Token *tok, const std::string &rhs) { if (tok->str() == rhs) return true; - if (tok->isName() && !tok->varId() && tok->hasKnownIntValue() && MathLib::toString(tok->values().front().intvalue) == rhs) + if (!tok->varId() && tok->hasKnownIntValue() && MathLib::toString(tok->values().front().intvalue) == rhs) return true; return false; } diff --git a/test/testleakautovar.cpp b/test/testleakautovar.cpp index 1b37340dd..cb8ea5008 100644 --- a/test/testleakautovar.cpp +++ b/test/testleakautovar.cpp @@ -104,6 +104,7 @@ private: TEST_CASE(ifelse11); // #8365 - if (NULL == (p = malloc(4))) TEST_CASE(ifelse12); // #8340 - if ((*p = malloc(4)) == NULL) TEST_CASE(ifelse13); // #8392 + TEST_CASE(ifelse14); // #9130 - if (x == (char*)NULL) // switch TEST_CASE(switch1); @@ -1203,6 +1204,16 @@ private: TODO_ASSERT_EQUALS("[test.cpp:4] memory leak", "", errout.str()); } + void ifelse14() { // #9130 + check("char* f() {\n" + " char* buf = malloc(10);\n" + " if (buf == (char*)NULL)\n" + " return NULL;\n" + " return buf;\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } + void switch1() { check("void f() {\n" " char *p = 0;\n"