Fixed #3538 (false positive caused by bad tokenizer simplification)

This commit is contained in:
Daniel Marjamäki 2012-02-01 21:13:26 +01:00
parent 036b2a84bf
commit 2be85e9d37
3 changed files with 19 additions and 4 deletions

View File

@ -6494,7 +6494,7 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign
} }
// array usage // array usage
if (Token::Match(tok3, ("[(,] " + structname + " %varid% [|%op%").c_str(), varid)) { if (value[0] != '\"' && Token::Match(tok3, ("[(,] " + structname + " %varid% [|%op%").c_str(), varid)) {
if (!structname.empty()) { if (!structname.empty()) {
tok3->deleteNext(2); tok3->deleteNext(2);
} }

View File

@ -1773,14 +1773,14 @@ private:
" int i = 10;\n" " int i = 10;\n"
" bar(str[i]);\n" " bar(str[i]);\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:5]: (error) Buffer access out-of-bounds: \"abc\"\n", errout.str()); ASSERT_EQUALS("[test.cpp:5]: (error) Array 'str[4]' index 10 out of bounds\n", errout.str());
check("void f()\n" check("void f()\n"
"{\n" "{\n"
" const char *str = \"abc\";\n" " const char *str = \"abc\";\n"
" bar(str[4]);\n" " bar(str[4]);\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Buffer access out-of-bounds: \"abc\"\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (error) Array 'str[4]' index 4 out of bounds\n", errout.str());
check("void f()\n" check("void f()\n"
"{\n" "{\n"
@ -1794,7 +1794,7 @@ private:
" const char *str = \"a\tc\";\n" " const char *str = \"a\tc\";\n"
" bar(str[4]);\n" " bar(str[4]);\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Buffer access out-of-bounds: \"a\tc\"\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (error) Array 'str[4]' index 4 out of bounds\n", errout.str());
} }

View File

@ -2195,6 +2195,21 @@ private:
"}"; "}";
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); ASSERT_EQUALS(expected, tokenizeAndStringify(code, true));
} }
// 3538
{
const char code[] = "void f() {\n"
" char s[10];\n"
" strcpy(s, \"123\");\n"
" if (s[6] == ' ');\n"
"}";
const char expected[] = "void f ( ) {\n"
"char s [ 10 ] ;\n"
"strcpy ( s , \"123\" ) ;\n"
"if ( s [ 6 ] == ' ' ) { ; }\n"
"}";
ASSERT_EQUALS(expected, tokenizeAndStringify(code,true));
}
} }
void simplifyKnownVariables43() { void simplifyKnownVariables43() {