memleakOnRealloc: Don't warn if pointer is NULL in condition

This commit is contained in:
Rikard Falkeborn 2020-09-12 18:46:01 +02:00
parent 3880412c46
commit da8ad9ce19
2 changed files with 7 additions and 2 deletions

View File

@ -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 (") &&

View File

@ -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() {