From 07fe361964087d81c18aa1c4d763f15727acee6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 13 Mar 2011 17:52:45 +0100 Subject: [PATCH] Fixed #2638 (Tokenizer::setVarId : varid is wrongly given when unknown macro is used) --- lib/checkbufferoverrun.cpp | 3 +++ lib/tokenize.cpp | 13 +++++++++++++ test/testbufferoverrun.cpp | 12 ++++++++++++ test/testtokenize.cpp | 18 ++++++++++++++++++ 4 files changed, 46 insertions(+) diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index c6f440c79..918958f8a 100644 --- a/lib/checkbufferoverrun.cpp +++ b/lib/checkbufferoverrun.cpp @@ -2032,6 +2032,9 @@ bool CheckBufferOverrun::ArrayInfo::declare(const Token *tok, const Tokenizer &t atok = atok->tokAt(2); } + if (Token::Match(atok, "] = !!{")) + return false; + return (!_num.empty() && Token::Match(atok, "] ;|=")); } diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 48616cb06..734a5bcdd 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -3504,6 +3504,19 @@ void Tokenizer::setVarId() } } + // Don't set variable id for 'AAA a[0] = 0;' declaration (#2638) + if (tok2->previous()->varId() && tok2->str() == "[") + { + const Token *tok3 = tok2; + while (tok3 && tok3->str() == "[") + { + tok3 = tok3->link(); + tok3 = tok3 ? tok3->next() : NULL; + } + if (Token::Match(tok3, "= !!{")) + continue; + } + // Variable declaration found => Set variable ids if (Token::Match(tok2, "[,();[=]") && !varname.empty()) { diff --git a/test/testbufferoverrun.cpp b/test/testbufferoverrun.cpp index 053139bea..19706f8cf 100644 --- a/test/testbufferoverrun.cpp +++ b/test/testbufferoverrun.cpp @@ -211,6 +211,8 @@ private: TEST_CASE(scope); // handling different scopes TEST_CASE(getErrorMessages); + + TEST_CASE(unknownMacroNoDecl); // #2638 - not variable declaration: 'AAA a[0] = 0;' } @@ -2941,6 +2943,16 @@ private: CheckBufferOverrun c; c.getErrorMessages(this, 0); } + + void unknownMacroNoDecl() + { + check("void f() {\n" + " int a[10];\n" + " AAA a[0] = 0;\n" // <- not a valid array declaration + " a[1] = 1;\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } }; REGISTER_TEST(TestBufferOverrun) diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index fabac2d59..622cfbc88 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -181,6 +181,7 @@ private: TEST_CASE(varid_in_class2); TEST_CASE(varid_operator); TEST_CASE(varid_throw); + TEST_CASE(varid_unknown_macro); // #2638 - unknown macro is not type TEST_CASE(varidclass1); TEST_CASE(varidclass2); @@ -3133,6 +3134,23 @@ private: ASSERT_EQUALS(expected, actual); } + void varid_unknown_macro() + { + // #2638 - unknown macro + const char code[] = "void f() {\n" + " int a[10];\n" + " AAA\n" + " a[0] = 0;\n" + "}"; + const char expected[] = "\n\n##file 0\n" + "1: void f ( ) {\n" + "2: int a@1 [ 10 ] ;\n" + "3: AAA\n" + "4: a@1 [ 0 ] = 0 ;\n" + "5: }\n"; + ASSERT_EQUALS(expected, tokenizeDebugListing(code)); + } + void varidclass1() { const std::string actual = tokenizeDebugListing(