diff --git a/lib/exprengine.cpp b/lib/exprengine.cpp index bc8136e91..20ca78621 100644 --- a/lib/exprengine.cpp +++ b/lib/exprengine.cpp @@ -929,6 +929,16 @@ void ExprEngine::ArrayValue::assign(ExprEngine::ValuePtr index, ExprEngine::Valu if (!index) data.clear(); 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}; data.push_back(indexAndValue); } diff --git a/test/testexprengine.cpp b/test/testexprengine.cpp index ecb88a37e..1cb1a5357 100644 --- a/test/testexprengine.cpp +++ b/test/testexprengine.cpp @@ -85,6 +85,7 @@ private: TEST_CASE(array4); TEST_CASE(array5); TEST_CASE(array6); + TEST_CASE(array7); TEST_CASE(arrayInit1); TEST_CASE(arrayInit2); TEST_CASE(arrayInit3); @@ -665,6 +666,19 @@ private: 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() { ASSERT_EQUALS("0", getRange("inf f() { char arr[10] = \"\"; return arr[4]; }", "arr[4]")); }