diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 6aef7cab8..9ab1c2670 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -1087,24 +1087,20 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::listnext(); tok2; tok2 = tok2->next()) { if (tok2->str() == "(") ++innerParlevel; - if (tok2->str() == ")") { + else if (tok2->str() == ")") { --innerParlevel; if (innerParlevel <= 0) break; - } - if (Token::Match(tok2, "close|pclose|fclose|closedir ( %varid% )", varid)) { + } else if (Token::Match(tok2, "close|pclose|fclose|closedir ( %varid% )", varid)) { addtoken(&rettail, tok, "dealloc"); addtoken(&rettail, tok, ";"); dep = true; break; - } - if (alloctype == Fd && Token::Match(tok2, "%varid% !=|>=", varid)) { + } else if (alloctype == Fd && Token::Match(tok2, "%varid% !=|>=", varid)) { dep = true; - } - if (innerParlevel > 0 && Token::Match(tok2, "! %varid%", varid)) { + } else if (innerParlevel > 0 && Token::Match(tok2, "! %varid%", varid)) { dep = true; - } - if (innerParlevel > 0 && Token::Match(tok2, "%var% (") && !test_white_list(tok2->str())) { + } else if (innerParlevel > 0 && Token::Match(tok2, "%var% (") && !test_white_list(tok2->str())) { bool use = false; for (const Token *tok3 = tok2->tokAt(2); tok3; tok3 = tok3->next()) { if (tok3->str() == "(") @@ -1122,10 +1118,10 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::listvarId() && extravar.find(tok2->varId()) != extravar.end()) { + } else if (tok2->varId() && extravar.find(tok2->varId()) != extravar.end()) { + dep = true; + } else if (tok2->varId() == varid && tok2->next()->isOp()) dep = true; - } } if (Token::Match(tok, "if ( ! %varid% &&", varid)) { @@ -1673,7 +1669,7 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok) } // Delete "if { dealloc|assign|use ; return ; }" - else if (Token::Match(tok2, "[;{}] if { dealloc|assign|use ; return ; }") && + else if (Token::Match(tok2, "[;{}] if { dealloc|assign|use ; return ; }") && !Token::findmatch(tok, "if alloc ;")) { Token::eraseTokens(tok2, tok2->tokAt(8)); if (Token::simpleMatch(tok2->next(), "else")) diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index d635b9a5d..e50345700 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -440,6 +440,7 @@ private: ASSERT_EQUALS(";;use;if{}", getcode("char *s; if (x(s)) { }", "s")); ASSERT_EQUALS(";;use;if{}", getcode("char *s; if (x(&s)) { }", "s")); ASSERT_EQUALS(";;use;if{}", getcode("char *s; if (!s || x(&s)) { }", "s")); + ASSERT_EQUALS(";;ifv{}", getcode("int ffd; if (ffd<0 && (ffd=a)<0){}", "ffd")); // if (ticket #2442) ASSERT_EQUALS(";;;;if(!var){;}ifv{}", getcode("char *s; int x = 0; if (!s) { x = 2; } if (x) { }", "s"));