Fix #11039 Empty AST with delete new / #11327 FP leakReturnValNotUsed with new and offset (#4513)

* Fix internalAstError with new

* Format

* nullptr check

* Add test for #11039

* Fix #11039 Empty AST with delete new / #11327 FP leakReturnValNotUsed with new and offset
This commit is contained in:
chrchr-github 2022-09-27 18:12:58 +02:00 committed by GitHub
parent c8b96c3a20
commit cab4997b0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 1 deletions

View File

@ -543,6 +543,8 @@ static bool iscast(const Token *tok, bool cpp)
for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next()) {
if (tok2->varId() != 0)
return false;
if (cpp && !type && tok2->str() == "new")
return false;
while (tok2->link() && Token::Match(tok2, "(|[|<"))
tok2 = tok2->link()->next();
@ -1620,7 +1622,8 @@ static Token * createAstAtToken(Token *tok, bool cpp)
Token::Match(tok, "%name% %op%|(|[|.|::|<|?|;") ||
(cpp && Token::Match(tok, "%name% {") && iscpp11init(tok->next())) ||
Token::Match(tok->previous(), "[;{}] %cop%|++|--|( !!{") ||
Token::Match(tok->previous(), "[;{}] %num%|%str%|%char%")) {
Token::Match(tok->previous(), "[;{}] %num%|%str%|%char%") ||
Token::Match(tok->previous(), "[;{}] delete new")) {
if (cpp && (Token::Match(tok->tokAt(-2), "[;{}] new|delete %name%") || Token::Match(tok->tokAt(-3), "[;{}] :: new|delete %name%")))
tok = tok->previous();

View File

@ -2666,6 +2666,17 @@ private:
" Ref<StringBuffer> remove(new StringBuffer());\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void f() {\n" // #11039
" delete new int;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void f() {\n" // #11327
" int* p = (new int[3]) + 1;\n"
" delete[] &p[-1];\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void smartPointerFunctionParam() {

View File

@ -6074,6 +6074,9 @@ private:
ASSERT_EQUALS("a0[T{new=", testAst("a[0] = new T{};"));
ASSERT_EQUALS("a0[T::{new=", testAst("a[0] = new ::T{};"));
ASSERT_EQUALS("a0[ST::{new=", testAst("a[0] = new S::T{};"));
ASSERT_EQUALS("intnewdelete", testAst("delete new int;")); // #11039
ASSERT_EQUALS("intnewdelete", testAst("void f() { delete new int; }"));
ASSERT_EQUALS("pint3[new1+=", testAst("p = (new int[3]) + 1;")); // #11327
// placement new
ASSERT_EQUALS("X12,3,(new ab,c,", testAst("new (a,b,c) X(1,2,3);"));