From 3c3435dd10943668a9d323f5b5b33f995f9e9d4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 6 Jun 2021 08:13:40 +0200 Subject: [PATCH] Fix bug in Tokenizer::simplifyVarDecl --- lib/tokenize.cpp | 2 +- test/testtokenize.cpp | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index ee4e441e6..a5eb147c6 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -7438,7 +7438,7 @@ void Tokenizer::simplifyVarDecl(Token * tokBegin, const Token * const tokEnd, co else if (std::strchr(";,", tok2->str()[0])) { // "type var =" => "type var; var =" const Token *varTok = type0->tokAt(typelen); - while (Token::Match(varTok, "*|&|const|volatile")) + while (Token::Match(varTok, "%name%|*|& %name%|*|&")) varTok = varTok->next(); if (!varTok) syntaxError(tok2); // invalid code diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 7a7adb40a..07c0a5298 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -215,6 +215,7 @@ private: TEST_CASE(vardecl25); // #4799 - segmentation fault TEST_CASE(vardecl26); // #5907 - incorrect handling of extern declarations TEST_CASE(vardecl27); // #7850 - crash on valid C code + TEST_CASE(vardecl28); TEST_CASE(vardecl_stl_1); TEST_CASE(vardecl_stl_2); TEST_CASE(vardecl_template_1); @@ -2473,6 +2474,18 @@ private: tokenizeAndStringify(code, /*expand=*/true, Settings::Native, "test.c"); } + void vardecl28() { + const char code[] = "unsigned short f(void) {\n" + " unsigned short const int x = 1;\n" + " return x;\n" + "}"; + ASSERT_EQUALS("unsigned short f ( void ) {\n" + "const unsigned short x ; x = 1 ;\n" + "return x ;\n" + "}", + tokenizeAndStringify(code, /*expand=*/true, Settings::Native, "test.c")); + } + void volatile_variables() { const char code[] = "volatile int a=0;\n" "volatile int b=0;\n"