Skip to content
Browse files

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

  • Loading branch information...
1 parent d5f6cfc commit 706631f527e98cacf36ddce09de2a608e64f23a3 @danmar committed Apr 22, 2012
Showing with 22 additions and 4 deletions.
  1. +5 −2 lib/tokenize.cpp
  2. +17 −2 test/testtokenize.cpp
View
7 lib/tokenize.cpp
@@ -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
19 test/testtokenize.cpp
@@ -150,7 +150,8 @@ class TestTokenizer : public TestFixture {
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 @@ class TestTokenizer : public TestFixture {
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 @@ class TestTokenizer : public TestFixture {
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"

0 comments on commit 706631f

Please sign in to comment.
Something went wrong with that request. Please try again.