From c7cd091a9384ec744f883395fb3888c4b1f5283e Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Fri, 5 Jan 2024 14:11:41 +0100 Subject: [PATCH] Fix #12141 debug: simplifyUsing: unmatched body end (#5620) --- lib/tokenize.cpp | 9 ++++++++- test/testleakautovar.cpp | 10 ++++++++++ test/testsimplifytypedef.cpp | 25 +++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 6654006e6..7cab82b95 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -821,7 +821,14 @@ namespace { } } - Token* const tok2 = insertTokens(tok, mRangeType); + // don't add class|struct|union in inheritance list + auto rangeType = mRangeType; + if (Token::Match(tok->previous(), "public|private|protected")) { + while (Token::Match(rangeType.first, "const|class|struct|union")) + rangeType.first = rangeType.first->next(); + } + + Token* const tok2 = insertTokens(tok, rangeType); Token* const tok3 = insertTokens(tok2, mRangeTypeQualifiers); Token *after = tok3; diff --git a/test/testleakautovar.cpp b/test/testleakautovar.cpp index e2ef3b585..e6de5e6e6 100644 --- a/test/testleakautovar.cpp +++ b/test/testleakautovar.cpp @@ -3044,6 +3044,7 @@ private: TEST_CASE(deallocuse2); TEST_CASE(fclose_false_positive); // #9575 TEST_CASE(strcpy_false_negative); + TEST_CASE(doubleFree); } void returnedValue() { // #9298 @@ -3089,6 +3090,15 @@ private: "}\n"); ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: buf\n", errout.str()); } + + void doubleFree() { + check("void f(char* p) {\n" + " free(p);\n" + " printf(\"%s\", p = strdup(\"abc\"));\n" + " free(p);\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } }; REGISTER_TEST(TestLeakAutoVarStrcpy) diff --git a/test/testsimplifytypedef.cpp b/test/testsimplifytypedef.cpp index 089d3474b..234a58a2d 100644 --- a/test/testsimplifytypedef.cpp +++ b/test/testsimplifytypedef.cpp @@ -1608,6 +1608,31 @@ private: ASSERT_EQUALS(expected, tok(code)); ASSERT_EQUALS("", errout.str()); } + + { + const char code[] = "struct B {};\n" // #12141 + "typedef struct B B;\n" + "namespace N {\n" + " struct D : public B {};\n" + "}\n"; + + const char expected[] = "struct B { } ; namespace N { struct D : public B { } ; }"; + ASSERT_EQUALS(expected, tok(code)); + ASSERT_EQUALS("", errout.str()); + } + + { + const char code[] = "struct B {};\n" + "typedef const struct B CB;\n" + "namespace N {\n" + " struct D : public CB {};\n" + "}\n" + "CB cb;\n"; + + const char expected[] = "struct B { } ; namespace N { struct D : public B { } ; } const struct B cb ;"; + ASSERT_EQUALS(expected, tok(code)); + ASSERT_EQUALS("", errout.str()); + } } void simplifyTypedef45() {