From 2d971894862476d23ee4b98d7b5db6f181d00e2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 16 Dec 2010 20:16:59 +0100 Subject: [PATCH] Fixed #2311 (False positive: Index out of bounds) --- lib/tokenize.cpp | 4 ++++ test/testtokenize.cpp | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 3ba3920d4..7a8408db0 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -6092,6 +6092,10 @@ bool Tokenizer::simplifyKnownVariables() if (pointeralias && Token::Match(tok3, ("!!= " + value).c_str())) break; + // Stop if do is found + if (tok3->str() == "do") + break; + // Stop if something like 'while (--var)' is found if (tok3->str() == "for" || tok3->str() == "while" || tok3->str() == "do") { diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 2edf9dd71..142214609 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -120,6 +120,7 @@ private: TEST_CASE(simplifyKnownVariables31); TEST_CASE(simplifyKnownVariables32); // const TEST_CASE(simplifyKnownVariables33); // struct variable + TEST_CASE(simplifyKnownVariables34); TEST_CASE(simplifyKnownVariablesBailOutAssign); TEST_CASE(simplifyKnownVariablesBailOutFor1); TEST_CASE(simplifyKnownVariablesBailOutFor2); @@ -1852,6 +1853,21 @@ private: ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); } + void simplifyKnownVariables34() + { + const char code[] = "void f() {\n" + " int x = 10;\n" + " do { cin >> x; } while (x > 5);\n" + " a[x] = 0;\n" + "}\n"; + const char expected[] = "void f ( ) {\n" + "int x ; x = 10 ;\n" + "do { cin >> x ; } while ( 5 < x ) ;\n" + "a [ x ] = 0 ;\n" + "}"; + ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); + } + void simplifyKnownVariablesBailOutAssign() { const char code[] = "int foo() {\n"