Bug hunting; Remove old value from array when it is overwritten

This commit is contained in:
Daniel Marjamäki 2020-12-16 19:06:20 +01:00
parent d4860f500a
commit 8619bfe957
2 changed files with 24 additions and 0 deletions

View File

@ -929,6 +929,16 @@ void ExprEngine::ArrayValue::assign(ExprEngine::ValuePtr index, ExprEngine::Valu
if (!index) if (!index)
data.clear(); data.clear();
if (value) { if (value) {
if (index) {
// Remove old item that will be "overwritten"
for (size_t i = 0; i < data.size(); ++i) {
if (data[i].index && data[i].index->name == index->name) {
data.erase(data.begin() + i);
break;
}
}
}
ExprEngine::ArrayValue::IndexAndValue indexAndValue = {index, value}; ExprEngine::ArrayValue::IndexAndValue indexAndValue = {index, value};
data.push_back(indexAndValue); data.push_back(indexAndValue);
} }

View File

@ -85,6 +85,7 @@ private:
TEST_CASE(array4); TEST_CASE(array4);
TEST_CASE(array5); TEST_CASE(array5);
TEST_CASE(array6); TEST_CASE(array6);
TEST_CASE(array7);
TEST_CASE(arrayInit1); TEST_CASE(arrayInit1);
TEST_CASE(arrayInit2); TEST_CASE(arrayInit2);
TEST_CASE(arrayInit3); TEST_CASE(arrayInit3);
@ -665,6 +666,19 @@ private:
expr(code, "==")); expr(code, "=="));
} }
void array7() {
const char code[] = "void foo(unsigned char *x) {\n"
" *x = 2;\n"
" *x = 1;\n"
"}";
ASSERT_EQUALS("1:28: $2=ArrayValue([$1],[:]=?,null)\n"
"1:28: $1=IntRange(1:ffffffffffffffff)\n"
"1:28: 0:memory:{x=($2,[$1],[:]=?)}\n"
"2:9: 0:memory:{x=($2,[$1],[:]=?,[0]=2)}\n"
"3:9: 0:memory:{x=($2,[$1],[:]=?,[0]=1)}\n",
trackExecution(code));
}
void arrayInit1() { void arrayInit1() {
ASSERT_EQUALS("0", getRange("inf f() { char arr[10] = \"\"; return arr[4]; }", "arr[4]")); ASSERT_EQUALS("0", getRange("inf f() { char arr[10] = \"\"; return arr[4]; }", "arr[4]"));
} }