diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 0accf25c0..f5e332d71 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -2275,7 +2275,8 @@ void CheckMemoryLeakInFunction::checkReallocUsage() parameterVarIds.find(tok->varId()) == parameterVarIds.end()) { // Check that another copy of the pointer wasn't saved earlier in the function - if (!Token::findmatch(startOfFunction, "%var% = %varid% ;", tok->varId())) + if (!Token::findmatch(startOfFunction, "%var% = %varid% ;", tok->varId()) && + !Token::findmatch(startOfFunction, "[{};] %varid% = %var% [;=]", tok->varId())) memleakUponReallocFailureError(tok, tok->str()); } } diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index deab61f41..b2704e447 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -334,6 +334,7 @@ private: TEST_CASE(realloc7); TEST_CASE(realloc8); TEST_CASE(realloc9); + TEST_CASE(realloc10); TEST_CASE(assign); @@ -2077,6 +2078,17 @@ private: ASSERT_EQUALS("", errout.str()); } + void realloc10() + { + check("void foo() {\n" + " char *pa, *pb;\n" + " pa = pb = malloc(10);\n" + " pa = realloc(pa, 20);" + " exit();\n" + "}\n", false); + ASSERT_EQUALS("", errout.str()); + } + void assign() { check("void foo()\n"