Fixed #1646 (False positive: array access after return cannot have default loop value)

This commit is contained in:
Daniel Marjamäki 2010-05-04 20:02:47 +02:00
parent 7ce70777a4
commit 612be2557b
3 changed files with 35 additions and 1 deletions

View File

@ -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");
}

View File

@ -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;

View File

@ -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()
{