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)