Skip to content

Commit 745a9b9

Browse files
author
Daniel Kroening
authored
Merge pull request #1068 from tautschnig/fix-array-replace
Expansion of array_replace/array_copy must not drop offsets
2 parents e0b4515 + 03347eb commit 745a9b9

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

regression/cbmc/memset2/main.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <string.h>
2+
#include <assert.h>
3+
4+
typedef struct {
5+
int a;
6+
int b;
7+
} TEST;
8+
9+
static TEST test;
10+
11+
main()
12+
{
13+
test.a = 1;
14+
test.b = 2;
15+
memset(&test.b, 0, sizeof(test.b));
16+
assert(test.a);
17+
assert(!test.b);
18+
test.b = 2;
19+
memset(&test.a, 0, sizeof(test.a));
20+
assert(!test.a);
21+
assert(test.b);
22+
}

regression/cbmc/memset2/test.desc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
main.c
3+
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring

src/goto-symex/symex_other.cpp

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,21 @@ void goto_symext::symex_other(
9494
clean_code.op1()=d1;
9595

9696
clean_expr(clean_code.op0(), state, true);
97+
exprt op0_offset=from_integer(0, index_type());
9798
if(clean_code.op0().id()==byte_extract_id() &&
9899
clean_code.op0().type().id()==ID_empty)
100+
{
101+
op0_offset=to_byte_extract_expr(clean_code.op0()).offset();
99102
clean_code.op0()=clean_code.op0().op0();
103+
}
100104
clean_expr(clean_code.op1(), state, false);
105+
exprt op1_offset=from_integer(0, index_type());
101106
if(clean_code.op1().id()==byte_extract_id() &&
102107
clean_code.op1().type().id()==ID_empty)
108+
{
109+
op1_offset=to_byte_extract_expr(clean_code.op1()).offset();
103110
clean_code.op1()=clean_code.op1().op0();
111+
}
104112

105113
process_array_expr(clean_code.op0());
106114
clean_expr(clean_code.op0(), state, true);
@@ -109,23 +117,45 @@ void goto_symext::symex_other(
109117

110118

111119
if(!base_type_eq(clean_code.op0().type(),
112-
clean_code.op1().type(), ns))
120+
clean_code.op1().type(), ns) ||
121+
!op0_offset.is_zero() || !op1_offset.is_zero())
113122
{
114123
byte_extract_exprt be(byte_extract_id());
115-
be.offset()=from_integer(0, index_type());
116124

117125
if(statement==ID_array_copy)
118126
{
119127
be.op()=clean_code.op1();
128+
be.offset()=op1_offset;
120129
be.type()=clean_code.op0().type();
121130
clean_code.op1()=be;
131+
132+
if(!op0_offset.is_zero())
133+
{
134+
byte_extract_exprt op0(
135+
byte_extract_id(),
136+
clean_code.op0(),
137+
op0_offset,
138+
clean_code.op0().type());
139+
clean_code.op0()=op0;
140+
}
122141
}
123142
else
124143
{
125144
// ID_array_replace
126145
be.op()=clean_code.op0();
146+
be.offset()=op0_offset;
127147
be.type()=clean_code.op1().type();
128148
clean_code.op0()=be;
149+
150+
if(!op1_offset.is_zero())
151+
{
152+
byte_extract_exprt op1(
153+
byte_extract_id(),
154+
clean_code.op1(),
155+
op1_offset,
156+
clean_code.op1().type());
157+
clean_code.op1()=op1;
158+
}
129159
}
130160
}
131161

0 commit comments

Comments
 (0)