ExprEngine: Handling cases when for condition is always false (#2984)

This commit is contained in:
Georgy Komarov 2020-12-26 23:29:50 +03:00 committed by GitHub
parent bd22070df5
commit 1c12b4fd78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 0 deletions

View File

@ -2669,6 +2669,10 @@ static std::string execute(const Token *start, const Token *end, Data &data)
data.assignValue(tok, varid, loopValues); data.assignValue(tok, varid, loopValues);
tok = tok->linkAt(1); tok = tok->linkAt(1);
loopValues->loopScope = tok->next()->scope(); loopValues->loopScope = tok->next()->scope();
// Check whether the condition expression is always false
if (tok->next() && (initValue > lastValue)) {
tok = tok->next()->link();
}
continue; continue;
} }
} }

View File

@ -79,6 +79,7 @@ private:
TEST_CASE(switch2); TEST_CASE(switch2);
TEST_CASE(for1); TEST_CASE(for1);
TEST_CASE(forAlwaysFalse1);
TEST_CASE(while1); TEST_CASE(while1);
TEST_CASE(while2); TEST_CASE(while2);
@ -660,6 +661,22 @@ private:
expr(code, "==")); expr(code, "=="));
} }
void forAlwaysFalse1() {
const char code[] = "int f() {\n"
" int a = 19;\n"
" for (int i = 0; i < 0; i++)\n"
" a += 8;\n"
" for (int i = 0; i < 1; i++)\n"
" a += 23;\n"
" for (int i = 100; i >= 1; i--)\n"
" a += 23;\n"
" return a == 42;\n"
"}";
const char expected[] = "(and (>= $4 (- 2147483648)) (<= $4 2147483647))\n"
"(= (+ $4 23) 42)\n"
"z3::sat\n";
ASSERT_EQUALS(expected, expr(code, "=="));
}
void while1() { void while1() {
const char code[] = "void f(int y) {\n" const char code[] = "void f(int y) {\n"