Skip to content

Commit 9a94418

Browse files
committed
Default fall-through for function pointer removal
When the pointer point to neither of the candidates we shouldn't just call the first one (which does happen if the user does not request pointer-check). This PR inserts an ASSUME (false) which makes all subsequent assertions unreachable (and thus reported as not being violated). A small regression test is included.
1 parent 73be2ed commit 9a94418

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// f is set to 0 -> does not point to either f1 or f2
2+
#include <assert.h>
3+
4+
typedef int (*f_ptr)(int);
5+
6+
extern f_ptr f;
7+
8+
int f1(int j);
9+
int f2(int i);
10+
11+
int f1(int j)
12+
{
13+
f = &f2;
14+
return 1;
15+
}
16+
int f2(int i)
17+
{
18+
assert(0);
19+
return 2;
20+
}
21+
22+
f_ptr f = 0;
23+
24+
int main()
25+
{
26+
int x = 0;
27+
28+
x = f(x);
29+
assert(x == 1);
30+
x = f(x);
31+
assert(x == 2);
32+
33+
return 0;
34+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CORE
2+
main.c
3+
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
\[f2.assertion.1\] line [0-9]+ assertion 0: SUCCESS
7+
\[main.assertion.1\] line [0-9]+ assertion x==1: SUCCESS
8+
\[main.assertion.2\] line [0-9]+ assertion x==2: SUCCESS
9+
^VERIFICATION SUCCESSFUL$
10+
--
11+
^warning: ignoring

src/goto-programs/remove_function_pointers.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,14 +408,18 @@ void remove_function_pointerst::remove_function_pointer(
408408
goto_programt::make_goto(call, equal_exprt(pointer, casted_address)));
409409
}
410410

411+
goto_programt::targett t;
411412
// fall-through
412413
if(add_safety_assertion)
413414
{
414-
goto_programt::targett t =
415-
new_code_gotos.add(goto_programt::make_assertion(false_exprt()));
415+
t = new_code_gotos.add(goto_programt::make_assertion(false_exprt()));
416416
t->source_location.set_property_class("pointer dereference");
417417
t->source_location.set_comment("invalid function pointer");
418418
}
419+
else
420+
{
421+
t = new_code_gotos.add(goto_programt::make_assumption(false_exprt()));
422+
}
419423

420424
goto_programt new_code;
421425

0 commit comments

Comments
 (0)