Fixed #3748 (False positive out of bounds with postincrement)

This commit is contained in:
Daniel Marjamäki 2012-04-22 12:22:49 +02:00
parent d5f6cfcfa8
commit 706631f527
2 changed files with 22 additions and 4 deletions

View File

@ -6053,7 +6053,7 @@ bool Tokenizer::simplifyKnownVariables()
const bool valueIsPointer = false;
Token *scopeStart = tok2->tokAt(6);
ret |= simplifyKnownVariablesSimplify(&scopeStart, scopeStart, varid, structname, value, valueIsPointer, valueVarId, valueToken, 0);
ret |= simplifyKnownVariablesSimplify(&scopeStart, scopeStart, varid, structname, value, valueIsPointer, valueVarId, valueToken, -1);
}
else if (Token::Match(tok2, "strcpy ( %var% , %str% ) ;")) {
@ -6147,6 +6147,9 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign
bool ret = false;
// skip increments and decrements if the given indentlevel is -1
const bool skipincdec = (indentlevel == -1);
Token* bailOutFromLoop = 0;
int indentlevel3 = indentlevel;
bool ret3 = false;
@ -6473,7 +6476,7 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign
}
}
if (indentlevel == indentlevel3 && Token::Match(tok3->next(), "%varid% ++|--", varid) && MathLib::isInt(value)) {
if (!skipincdec && indentlevel == indentlevel3 && Token::Match(tok3->next(), "%varid% ++|--", varid) && MathLib::isInt(value)) {
const std::string op(tok3->strAt(2));
if (Token::Match(tok3, "[{};] %any% %any% ;")) {
tok3->deleteNext(3);

View File

@ -150,7 +150,8 @@ private:
TEST_CASE(simplifyKnownVariables45); // ticket #3281 - static constant variable not simplified
TEST_CASE(simplifyKnownVariables46); // ticket #3587 - >>
TEST_CASE(simplifyKnownVariables47); // ticket #3627 - >>
TEST_CASE(simplifyKnownVariablesIfEq); // if (a==5) => a is 5 in the block
TEST_CASE(simplifyKnownVariablesIfEq1); // if (a==5) => a is 5 in the block
TEST_CASE(simplifyKnownVariablesIfEq2); // if (a==5) { buf[a++] = 0; }
TEST_CASE(simplifyKnownVariablesBailOutAssign1);
TEST_CASE(simplifyKnownVariablesBailOutAssign2);
TEST_CASE(simplifyKnownVariablesBailOutFor1);
@ -2277,7 +2278,7 @@ private:
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true, true, Settings::Unspecified, "test.cpp"));
}
void simplifyKnownVariablesIfEq() {
void simplifyKnownVariablesIfEq1() {
const char code[] = "void f(int x) {\n"
" if (x==5) {\n"
" return x;\n"
@ -2291,6 +2292,20 @@ private:
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true, true, Settings::Unspecified, "test.c"));
}
void simplifyKnownVariablesIfEq2() {
const char code[] = "void f(int x) {\n"
" if (x==5) {\n"
" buf[x++] = 0;\n"
" }\n"
"}";
const char expected[] = "void f ( int x ) {\n"
"if ( x == 5 ) {\n"
"buf [ x ++ ] = 0 ;\n"
"}\n"
"}";
ASSERT_EQUALS(expected, tokenizeAndStringify(code, true, true, Settings::Unspecified, "test.c"));
}
void simplifyKnownVariablesBailOutAssign1() {
const char code[] = "int foo() {\n"
" int i; i = 0;\n"