diff --git a/lib/checkleakautovar.cpp b/lib/checkleakautovar.cpp index 087c9209aef..2f3a26a58f5 100644 --- a/lib/checkleakautovar.cpp +++ b/lib/checkleakautovar.cpp @@ -401,7 +401,7 @@ bool CheckLeakAutoVar::checkScope(const Token * const startToken, if (Token::simpleMatch(tokAssignOp->astOperand1(), ".")) continue; // taking address of another variable.. - if (Token::Match(tokAssignOp, "= %var% [+;]")) { + if (Token::Match(tokAssignOp, "= %var% +|;|?|%comp%")) { if (varTok->tokAt(2)->varId() != varTok->varId()) { // If variable points at allocated memory => error leakIfAllocated(varTok, varInfo); diff --git a/test/testleakautovar.cpp b/test/testleakautovar.cpp index fe2cf01f244..1075774f51f 100644 --- a/test/testleakautovar.cpp +++ b/test/testleakautovar.cpp @@ -83,6 +83,7 @@ class TestLeakAutoVar : public TestFixture { TEST_CASE(assign23); TEST_CASE(assign24); // #7440 TEST_CASE(assign25); + TEST_CASE(assign26); TEST_CASE(isAutoDealloc); @@ -620,6 +621,20 @@ class TestLeakAutoVar : public TestFixture { ASSERT_EQUALS("", errout_str()); } + void assign26() { + check("void f(int*& x) {\n" // #8235 + " int* p = (int*)malloc(10);\n" + " x = p ? p : nullptr;\n" + "}", true); + ASSERT_EQUALS("", errout_str()); + + check("void f(int*& x) {\n" + " int* p = (int*)malloc(10);\n" + " x = p != nullptr ? p : nullptr;\n" + "}", true); + ASSERT_EQUALS("", errout_str()); + } + void isAutoDealloc() { check("void f() {\n" " char *p = new char[100];"