From af7c97f9722a694593810db06b521dd271f5aedc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 7 May 2011 11:34:48 +0200 Subject: [PATCH] Fixed #1684 (false positive: buffer access out of bounds when using extern variable declaration) --- lib/checkbufferoverrun.cpp | 8 ++++++++ test/testbufferoverrun.cpp | 9 +++++++++ 2 files changed, 17 insertions(+) diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index 36254bded..527d2763e 100644 --- a/lib/checkbufferoverrun.cpp +++ b/lib/checkbufferoverrun.cpp @@ -1043,6 +1043,14 @@ void CheckBufferOverrun::checkScope(const Token *tok, const ArrayInfo &arrayInfo --indentlevel; } + // Skip array declarations + else if (Token::Match(tok, "[;{}] %type% *| %var% [") && + tok->strAt(1) != "return") + { + tok = tok->tokAt(3); + continue; + } + else if (Token::Match(tok, "%varid% [ %num% ]", arrayInfo.varid)) { std::vector indexes; diff --git a/test/testbufferoverrun.cpp b/test/testbufferoverrun.cpp index c29801952..438198b3a 100644 --- a/test/testbufferoverrun.cpp +++ b/test/testbufferoverrun.cpp @@ -117,6 +117,7 @@ private: TEST_CASE(array_index_for); // FN: for,if TEST_CASE(array_index_for_neq); // #2211: Using != in condition TEST_CASE(array_index_for_question); // #2561: for, ?: + TEST_CASE(array_index_extern); // FP when using 'extern'. #1684 TEST_CASE(buffer_overrun_1); TEST_CASE(buffer_overrun_2); @@ -1381,6 +1382,14 @@ private: ASSERT_EQUALS("", errout.str()); } + void array_index_extern() + { + // Ticket #1684. FP when using 'extern'. + check("extern char arr[15];\n" + "char arr[15] = \"abc\";"); + ASSERT_EQUALS("", errout.str()); + } + void buffer_overrun_1() { check("void f()\n"