From 612be2557b35b18fd6492f0e4def8ab9af9f8277 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 4 May 2010 20:02:47 +0200 Subject: [PATCH] Fixed #1646 (False positive: array access after return cannot have default loop value) --- lib/checkother.cpp | 2 +- lib/tokenize.cpp | 4 ++++ test/testtokenize.cpp | 30 ++++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 5ef623a81..84f2cf57c 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -3596,5 +3596,5 @@ void CheckOther::emptyStringTestError(const Token *tok, const std::string &var_n void CheckOther::fflushOnInputStreamError(const Token *tok, const std::string &varname) { reportError(tok, Severity::possibleError, - "fflushOnInputStream", "fflush() called on input stream \"" + varname + "\" may result in undefined behaviour"); + "fflushOnInputStream", "fflush() called on input stream \"" + varname + "\" may result in undefined behaviour"); } diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 0f7c3a937..ade40a9a9 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -4998,6 +4998,10 @@ bool Tokenizer::simplifyKnownVariables() } } + // Stop if label is found + if (Token::Match(tok3, "; %type% : ;")) + break; + if (pointeralias && Token::Match(tok3, ("!!= " + value).c_str())) break; diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 301974799..27d98eaf4 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -102,6 +102,7 @@ private: TEST_CASE(simplifyKnownVariables22); TEST_CASE(simplifyKnownVariables23); TEST_CASE(simplifyKnownVariables24); + TEST_CASE(simplifyKnownVariables25); TEST_CASE(match1); @@ -1167,6 +1168,35 @@ private: simplifyKnownVariables(code)); } + void simplifyKnownVariables25() + { + // This testcase is related to ticket #1646 + const char code[] = "void foo(char *str)\n" + "{\n" + " int i;\n" + " for (i=0;i<10;++i) {\n" + " if (*str == 0) goto label;\n" + " }\n" + " return;\n" + "label:\n" + " str[i] = 0;\n" + "}\n"; + + // Current result + ASSERT_EQUALS( + "void foo ( char * str ) " + "{" + " int i ;" + " for ( i = 0 ; i < 10 ; ++ i ) {" + " if ( * str == 0 ) { goto label ; }" + " }" + " return ;" + " label : ;" + " str [ i ] = 0 ; " + "}", + simplifyKnownVariables(code)); + } + void match1() {