From ed5532c2a718f9333a8229acae4a38d68482cea5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 28 Sep 2023 15:18:08 +0200 Subject: [PATCH] Fix #12019 (False positive: null pointer, array zero initialization) (#5495) --- lib/tokenize.cpp | 8 +++++++- test/testsimplifytypedef.cpp | 10 +++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index af2323484..84b105221 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -888,10 +888,16 @@ namespace { if (!after) throw InternalError(tok, "Failed to simplify typedef. Is the code valid?"); - const Token* const tok4 = useAfterVarRange ? insertTokens(after->previous(), mRangeAfterVar)->next() : tok3->next(); + Token* const tok4 = useAfterVarRange ? insertTokens(after->previous(), mRangeAfterVar)->next() : tok3->next(); tok->deleteThis(); + // Unsplit variable declarations + if (Token::Match(tok4->previous(), "] ; %name% = {") && tok4->isSplittedVarDeclEq()) { + tok4->deleteNext(); + tok4->deleteThis(); + } + // Set links std::stack brackets; for (; tok != tok4; tok = tok->next()) { diff --git a/test/testsimplifytypedef.cpp b/test/testsimplifytypedef.cpp index 2b4080eed..b568d59c2 100644 --- a/test/testsimplifytypedef.cpp +++ b/test/testsimplifytypedef.cpp @@ -67,6 +67,7 @@ private: TEST_CASE(carray1); TEST_CASE(carray2); TEST_CASE(carray3); + TEST_CASE(carray4); TEST_CASE(cdonotreplace1); TEST_CASE(cppfp1); TEST_CASE(Generic1); @@ -472,6 +473,13 @@ private: ASSERT_EQUALS("int ( * p ) [ 3 ] [ 2 ] [ 1 ] ;", simplifyTypedef(code)); } + void carray4() { + const char* code{}; + code = "typedef int arr[12];\n" // #12019 + "void foo() { arr temp = {0}; }\n"; + ASSERT_EQUALS("void foo ( ) { int temp [ 12 ] = { 0 } ; }", tok(code)); + } + void cdonotreplace1() { const char code[] = "typedef int t;\n" "int* t;"; @@ -1869,7 +1877,7 @@ private: "}"; // The expected tokens.. - const char expected2[] = "void f ( ) { char a [ 256 ] ; a = { 0 } ; char b [ 256 ] ; b = { 0 } ; }"; + const char expected2[] = "void f ( ) { char a [ 256 ] = { 0 } ; char b [ 256 ] = { 0 } ; }"; ASSERT_EQUALS(expected2, tok(code2, false, cppcheck::Platform::Type::Native, false)); ASSERT_EQUALS("", errout.str());