diff --git a/src/checkmemoryleak.cpp b/src/checkmemoryleak.cpp index ec149ed12..5d625dbd3 100644 --- a/src/checkmemoryleak.cpp +++ b/src/checkmemoryleak.cpp @@ -764,7 +764,14 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::liststr() == "if") { - if (Token::Match(tok, "if ( %varid% )", varid)) + if (alloctype == Fd && + (Token::Match(tok, "if ( %varid% >= 0 )", varid) || + Token::Match(tok, "if ( %varid% != -1 )", varid))) + { + addtoken("if(var)"); + tok = tok->next()->link(); + } + else if (Token::Match(tok, "if ( %varid% )", varid)) { addtoken("if(var)"); diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index dfc22c327..2c8f6ac0c 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -374,6 +374,9 @@ private: // exit.. ASSERT_EQUALS(";;exit;", getcode("char *s; exit(0);", "s")); ASSERT_EQUALS(";;if{exit;}", getcode("char *s; if (a) { exit(0); }", "s")); + + // open/close + ASSERT_EQUALS(";;alloc;if(var)dealloc;", getcode("int f; f=open(); if(f>=0)close(f);", "f")); }