diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 064336a47..b8043a869 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -2879,6 +2879,11 @@ void CheckMemoryLeakNoVar::check() } } } + + // Handle the case were the user is calling a function returning something which is leaking + // and never assigns the returned value to a variable, which will lead to a leak. + else if (Token::Match(tok2, "[;{}] %var% (") && getAllocationType(tok2->next(), 0) != No) + missingAssignementLeak(tok2, tok2->next()->str()); } } } @@ -2888,4 +2893,9 @@ void CheckMemoryLeakNoVar::functionCallLeak(const Token *loc, const std::string reportError(loc, Severity::error, "leakNoVarFunctionCall", "Allocation with " + alloc + ", " + functionCall + " doesn't release it."); } +void CheckMemoryLeakNoVar::missingAssignementLeak(const Token *loc, const std::string &alloc) +{ + reportError(loc, Severity::error, "leakNoVar", "Allocation with " + alloc + " never assigned."); +} + diff --git a/lib/checkmemoryleak.h b/lib/checkmemoryleak.h index 4ad6977f7..8cceca537 100644 --- a/lib/checkmemoryleak.h +++ b/lib/checkmemoryleak.h @@ -456,6 +456,8 @@ private: void functionCallLeak(const Token *loc, const std::string &alloc, const std::string &functionCall); + void missingAssignementLeak(const Token *loc, const std::string &alloc); + void getErrorMessages(ErrorLogger * /*errorLogger*/, const Settings * /*settings*/) const { }