From 252a31c20b3f4292e5497d36a307ae78dce01d25 Mon Sep 17 00:00:00 2001 From: chrchr Date: Wed, 3 Jan 2024 15:36:49 +0100 Subject: [PATCH] Fix #12301 FP doubleFree with GTK functions --- lib/checkleakautovar.cpp | 6 +++--- lib/checkleakautovar.h | 2 +- test/cfg/gtk.c | 6 ++++++ 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/checkleakautovar.cpp b/lib/checkleakautovar.cpp index 86f1ab8164b..138916c11c3 100644 --- a/lib/checkleakautovar.cpp +++ b/lib/checkleakautovar.cpp @@ -815,7 +815,7 @@ bool CheckLeakAutoVar::checkScope(const Token * const startToken, } -const Token * CheckLeakAutoVar::checkTokenInsideExpression(const Token * const tok, VarInfo &varInfo) +const Token * CheckLeakAutoVar::checkTokenInsideExpression(const Token * const tok, VarInfo &varInfo, bool inFuncCall) { // Deallocation and then dereferencing pointer.. if (tok->varId() > 0) { @@ -862,7 +862,7 @@ const Token * CheckLeakAutoVar::checkTokenInsideExpression(const Token * const t } // check for function call - const Token * const openingPar = isFunctionCall(tok); + const Token * const openingPar = inFuncCall ? nullptr : isFunctionCall(tok); if (openingPar) { const Library::AllocFunc* allocFunc = mSettings->library.getDeallocFuncInfo(tok); VarInfo::AllocInfo alloc(allocFunc ? allocFunc->groupId : 0, VarInfo::DEALLOC, tok); @@ -1045,7 +1045,7 @@ void CheckLeakAutoVar::functionCall(const Token *tokName, const Token *tokOpenin const VarInfo::AllocInfo sp_allocation(sp_af ? sp_af->groupId : (arrayDelete ? NEW_ARRAY : NEW), VarInfo::OWNED, allocTok); changeAllocStatus(varInfo, sp_allocation, vtok, vtok); } else { - checkTokenInsideExpression(arg, varInfo); + checkTokenInsideExpression(arg, varInfo, /*inFuncCall*/ isLeakIgnore); } // TODO: check each token in argument expression (could contain multiple variables) argNr++; diff --git a/lib/checkleakautovar.h b/lib/checkleakautovar.h index 23af88ff11c..b91f6b5cc3f 100644 --- a/lib/checkleakautovar.h +++ b/lib/checkleakautovar.h @@ -135,7 +135,7 @@ class CPPCHECKLIB CheckLeakAutoVar : public Check { * @param varInfo Variable info * @return next token to process (if no other checks needed for this token). NULL if other checks could be performed. */ - const Token * checkTokenInsideExpression(const Token * const tok, VarInfo &varInfo); + const Token * checkTokenInsideExpression(const Token * const tok, VarInfo &varInfo, bool inFuncCall = false); /** parse function call */ void functionCall(const Token *tokName, const Token *tokOpeningPar, VarInfo &varInfo, const VarInfo::AllocInfo& allocation, const Library::AllocFunc* af); diff --git a/test/cfg/gtk.c b/test/cfg/gtk.c index b7e4f21485d..4557173b9d2 100644 --- a/test/cfg/gtk.c +++ b/test/cfg/gtk.c @@ -425,3 +425,9 @@ void g_abort_test() //cppcheck-suppress unreachableCode printf("Never reached"); } + +gchar* g_strchug_string_free_test(GString* t) // #12301 +{ + gchar* p = g_strchug(g_string_free(t, FALSE)); + return p; +}