From 4d44d0c07902350144acef7c483c45a5f3c76a8a Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Wed, 19 Jan 2022 21:39:45 +0100 Subject: [PATCH] Fix #9781 FP deallocuse after auto_ptr (#3724) --- lib/astutils.cpp | 7 ------- lib/checkleakautovar.cpp | 2 +- test/testleakautovar.cpp | 10 ++++++++++ 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index 8e260fb24..cb2876eef 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -2136,13 +2136,6 @@ bool isVariableChangedByFunctionCall(const Token *tok, int indirect, const Setti return false; } -static bool isVariableChangedByFunctionCall(const Token* tok, int indirect, const Settings* settings) -{ - bool inconclusive = false; - bool r = isVariableChangedByFunctionCall(tok, indirect, settings, &inconclusive); - return r || inconclusive; -} - bool isVariableChanged(const Token *tok, int indirect, const Settings *settings, bool cpp, int depth) { if (!tok) diff --git a/lib/checkleakautovar.cpp b/lib/checkleakautovar.cpp index 1169e1954..b7435ce6d 100644 --- a/lib/checkleakautovar.cpp +++ b/lib/checkleakautovar.cpp @@ -836,7 +836,7 @@ void CheckLeakAutoVar::changeAllocStatus(VarInfo *varInfo, const VarInfo::AllocI var->second.type = allocation.type; var->second.allocTok = allocation.allocTok; } - } else if (allocation.status != VarInfo::NOALLOC) { + } else if (allocation.status != VarInfo::NOALLOC && allocation.status != VarInfo::OWNED) { alloctype[arg->varId()].status = VarInfo::DEALLOC; alloctype[arg->varId()].allocTok = tok; } diff --git a/test/testleakautovar.cpp b/test/testleakautovar.cpp index a88b53e22..475743c34 100644 --- a/test/testleakautovar.cpp +++ b/test/testleakautovar.cpp @@ -101,6 +101,7 @@ private: TEST_CASE(deallocuse6); // #4034: FP. x = p = f(); TEST_CASE(deallocuse7); // #6467, #6469, #6473 TEST_CASE(deallocuse8); // #1765 + TEST_CASE(deallocuse9); // #9781 TEST_CASE(doublefree1); TEST_CASE(doublefree2); @@ -696,6 +697,15 @@ private: ASSERT_EQUALS("[test.cpp:4]: (error) Dereferencing 'ptr' after it is deallocated / released\n", errout.str()); } + void deallocuse9() { // #9781 + check("void f(Type* p) {\n" + " std::shared_ptr sp(p);\n" + " bool b = p->foo();\n" + " return b;\n" + "}\n", /*cpp*/ true); + ASSERT_EQUALS("", errout.str()); + } + void doublefree1() { // #3895 check("void f(char *p) {\n" " if (x)\n"