From 1566b72c5b068eccc91cb1ef3cfd191b58cce7be Mon Sep 17 00:00:00 2001 From: PKEuS Date: Wed, 18 Nov 2015 21:37:37 +0100 Subject: [PATCH] Fixed broken AST for simple expressions with delete operator. Fixed cppcheck internal warning --- lib/checkmemoryleak.cpp | 6 +++--- lib/tokenlist.cpp | 2 +- test/testtokenize.cpp | 1 + 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 8b30a850d..391bcd40c 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -232,8 +232,8 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getReallocationType(const Token *tok CheckMemoryLeak::AllocType CheckMemoryLeak::getDeallocationType(const Token *tok, unsigned int varid) const { - if (tokenizer->isCPP() && tok->str() == "delete") { - const Token* vartok = tok->astOperand1() ? tok->astOperand1() : tok->next(); + if (tokenizer->isCPP() && tok->str() == "delete" && tok->astOperand1()) { + const Token* vartok = tok->astOperand1(); if (Token::Match(vartok, ".|::")) vartok = vartok->astOperand2(); @@ -257,7 +257,7 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getDeallocationType(const Token *tok if (Token::Match(vartok, "%varid% )|,|-", varid)) { if (Token::Match(tok, "free|kfree") || - (tok->str() == "realloc" && Token::Match(vartok->next(), ", 0 )"))) + (tok->str() == "realloc" && Token::simpleMatch(vartok->next(), ", 0 )"))) return Malloc; if (tok->str() == "fclose") diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index ecc01446f..3ab4b1244 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -1013,7 +1013,7 @@ static Token * createAstAtToken(Token *tok, bool cpp) if (Token::Match(tok, "%type% <") && !Token::Match(tok->linkAt(1), "> [({]")) return tok->linkAt(1); - if (tok->str() == "return" || !tok->previous() || Token::Match(tok, "%name% %op%|(|[|.|::|<|?") || Token::Match(tok->previous(), "[;{}] %cop%|++|--|( !!{")) { + if (tok->str() == "return" || !tok->previous() || Token::Match(tok, "%name% %op%|(|[|.|::|<|?|;") || Token::Match(tok->previous(), "[;{}] %cop%|++|--|( !!{")) { if (cpp && Token::Match(tok->tokAt(-2), "[;{}] new|delete %name%")) tok = tok->previous(); diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 6414f8f82..dd3228970 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -8170,6 +8170,7 @@ private: ASSERT_EQUALS("a::new=", testAst("a = new (b) ::X;")); ASSERT_EQUALS("aA1(new(bB2(new(,", testAst("a(new A(1)), b(new B(2))")); ASSERT_EQUALS("Fred10[new", testAst(";new Fred[10];")); + ASSERT_EQUALS("f( adelete", testAst("void f() { delete a; }")); // invalid code (libreoffice), don't hang // #define SlideSorterViewShell