From e7f7c77eab954c723e3911295b21981c0e2858fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 24 Aug 2010 22:03:18 +0200 Subject: [PATCH] Fixed #1948 (C++ class scoping not followed) --- lib/checkbufferoverrun.cpp | 13 +++++++++---- test/testbufferoverrun.cpp | 17 +++++++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index 187925fa4..3647056f0 100644 --- a/lib/checkbufferoverrun.cpp +++ b/lib/checkbufferoverrun.cpp @@ -1221,10 +1221,16 @@ void CheckBufferOverrun::checkGlobalAndLocalVariable() void CheckBufferOverrun::checkStructVariable() { - const char declstruct[] = "struct|class %var% {|:"; - for (const Token *tok = Token::findmatch(_tokenizer->tokens(), declstruct); - tok; tok = Token::findmatch(tok->next(), declstruct)) + for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { + if (tok->str() == "{") + { + tok = tok->link(); + } + + if (!Token::Match(tok, "struct|class %var% {|:")) + continue; + const std::string &structname = tok->next()->str(); const Token *tok2 = tok; @@ -1295,7 +1301,6 @@ void CheckBufferOverrun::checkStructVariable() else continue; - // Goto end of statement. const Token *CheckTok = NULL; while (tok3) diff --git a/test/testbufferoverrun.cpp b/test/testbufferoverrun.cpp index 99658a31b..9b5e83ffe 100644 --- a/test/testbufferoverrun.cpp +++ b/test/testbufferoverrun.cpp @@ -175,6 +175,8 @@ private: TEST_CASE(executionPaths2); TEST_CASE(cmdLineArgs1); + + TEST_CASE(scope); // handling different scopes } @@ -2485,6 +2487,21 @@ private: ASSERT_EQUALS("", errout.str()); } + + void scope() + { + check("class A {\n" + "private:\n" + " struct X { char buf[10]; };\n" + "}\n" + "\n" + "void f()\n" + "{\n" + " X x;\n" + " x.buf[10] = 0;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } }; REGISTER_TEST(TestBufferOverrun)