diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 43c0664e2..4bcbc6625 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -550,6 +550,11 @@ void CheckMemoryLeakInFunction::checkReallocUsage() Token::findmatch(scope->bodyStart, "[{};] %varid% = *| %name% .| %name%| [;=]", tok, tok->varId())) continue; + // Check if the argument is known to be null, which means it is not a memory leak + if (arg->hasKnownIntValue() && arg->getKnownIntValue() == 0) { + continue; + } + const Token* tokEndRealloc = reallocTok->linkAt(1); // Check that the allocation isn't followed immediately by an 'if (!var) { error(); }' that might handle failure if (Token::simpleMatch(tokEndRealloc->next(), "; if (") && diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index b70b35921..1b598ef84 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -201,7 +201,7 @@ private: " free(a);\n" "}"); - TODO_ASSERT_EQUALS("", "[test.cpp:4]: (error) Common realloc mistake: 'a' nulled but not freed upon failure\n", errout.str()); + ASSERT_EQUALS("", errout.str()); } void realloc4() { @@ -296,7 +296,7 @@ private: " return;\n" " free(a);\n" "}"); - TODO_ASSERT_EQUALS("", "[test.cpp:4]: (error) Common realloc mistake: 'a' nulled but not freed upon failure\n", errout.str()); + ASSERT_EQUALS("", errout.str()); } void realloc13() {