Skip to content

Commit e166f4c

Browse files
authored
Merge pull request #5961 from padhi-aws-forks/loop_modifies_fix
2 parents 04466cc + 867168c commit e166f4c

File tree

5 files changed

+70
-4
lines changed

5 files changed

+70
-4
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <assert.h>
2+
#include <stdlib.h>
3+
4+
void main()
5+
{
6+
char *data = malloc(1);
7+
*data = 42;
8+
9+
unsigned i;
10+
while(i > 0)
11+
__CPROVER_loop_invariant(*data == 42)
12+
{
13+
*data = 42;
14+
i--;
15+
}
16+
17+
assert(*data = 42);
18+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
CORE
2+
main.c
3+
--enforce-all-contracts --pointer-check
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^\[main.1\] .* Check loop invariant before entry: SUCCESS$
7+
^\[main.2\] .* Check that loop invariant is preserved: SUCCESS$
8+
^\[main.assertion.1\] .* assertion \*data = 42: SUCCESS$
9+
^VERIFICATION SUCCESSFUL$
10+
--
11+
^\[main.pointer_dereference.*\] line .* dereference failure: pointer NULL in \*data: FAILURE
12+
--
13+
This test checks that modifications to deref-ed pointers are correctly handled.
14+
In such cases, pointers should not be havoc'ed, only the value should be.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <assert.h>
2+
#include <stdlib.h>
3+
4+
void main()
5+
{
6+
char *copy, *data = malloc(1);
7+
8+
copy = data;
9+
*data = 42;
10+
11+
unsigned i;
12+
while(i > 0)
13+
__CPROVER_loop_invariant(*data == 42)
14+
{
15+
*data = 42;
16+
i--;
17+
}
18+
19+
assert(data == copy);
20+
assert(*copy = 42);
21+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
CORE
2+
main.c
3+
--enforce-all-contracts --pointer-check
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^\[main.1\] .* Check loop invariant before entry: SUCCESS$
7+
^\[main.2\] .* Check that loop invariant is preserved: SUCCESS$
8+
^\[main.assertion.1\] .* assertion data == copy: SUCCESS$
9+
^\[main.assertion.2\] .* assertion \*copy = 42: SUCCESS$
10+
^VERIFICATION SUCCESSFUL$
11+
--
12+
^\[main.pointer_dereference.*\] line .* dereference failure: pointer NULL in \*data: FAILURE
13+
--
14+
This test checks that modifications to aliased pointers are correctly handled.

src/goto-instrument/loop_utils.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,9 @@ void get_modifies_lhs(
6565
modifies.insert(lhs);
6666
else if(lhs.id()==ID_dereference)
6767
{
68-
modifiest m=local_may_alias.get(t, to_dereference_expr(lhs).pointer());
69-
for(modifiest::const_iterator m_it=m.begin();
70-
m_it!=m.end(); m_it++)
71-
get_modifies_lhs(local_may_alias, t, *m_it, modifies);
68+
const auto &pointer = to_dereference_expr(lhs).pointer();
69+
for(const auto &mod : local_may_alias.get(t, pointer))
70+
modifies.insert(dereference_exprt{mod});
7271
}
7372
else if(lhs.id()==ID_member)
7473
{

0 commit comments

Comments
 (0)