Skip to content

Commit bdd71ab

Browse files
committed
Pointer arithmetic has two operands by construction
Simplify the code accordingly and include a regression test that exercises it.
1 parent b0352f9 commit bdd71ab

File tree

3 files changed

+44
-21
lines changed

3 files changed

+44
-21
lines changed
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <stdlib.h>
2+
3+
int main() {
4+
int *p = malloc(2 * sizeof(int)), *p2;
5+
size_t other1, other2;
6+
7+
p2 = p + other1;
8+
p2 = p + other2 + other1;
9+
p2 = other1 + other2 + p;
10+
p2 = p - other1;
11+
p2 = p - other2 - other1;
12+
13+
p2 = p + sizeof(int);
14+
p2 = p + sizeof(int) + sizeof(int);
15+
16+
return 0;
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CORE
2+
main.c
3+
--pointer-overflow-check --unsigned-overflow-check
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
^\[main\.overflow\.\d+\] (pointer )?arithmetic overflow on .*sizeof\(signed int\) .* : SUCCESS
7+
^VERIFICATION FAILED$
8+
^\*\* 8 of 11 failed
9+
--
10+
^\[main\.overflow\.\d+\] (pointer )?arithmetic overflow on .*sizeof\(signed int\) .* : FAILURE
11+
^warning: ignoring

src/analyses/goto_check.cpp

+16-21
Original file line numberDiff line numberDiff line change
@@ -894,23 +894,22 @@ void goto_checkt::pointer_overflow_check(
894894
if(!enable_pointer_overflow_check)
895895
return;
896896

897-
if(expr.id()==ID_plus ||
898-
expr.id()==ID_minus)
899-
{
900-
if(expr.operands().size()==2)
901-
{
902-
exprt overflow("overflow-"+expr.id_string(), bool_typet());
903-
overflow.operands()=expr.operands();
897+
if(expr.id() != ID_plus && expr.id() != ID_minus)
898+
return;
904899

905-
add_guarded_claim(
906-
not_exprt(overflow),
907-
"pointer arithmetic overflow on "+expr.id_string(),
908-
"overflow",
909-
expr.find_source_location(),
910-
expr,
911-
guard);
912-
}
913-
}
900+
if(expr.operands().size() != 2)
901+
throw "pointer arithmetic expected to have exactly 2 operands";
902+
903+
exprt overflow("overflow-" + expr.id_string(), bool_typet());
904+
overflow.operands() = expr.operands();
905+
906+
add_guarded_claim(
907+
not_exprt(overflow),
908+
"pointer arithmetic overflow on " + expr.id_string(),
909+
"overflow",
910+
expr.find_source_location(),
911+
expr,
912+
guard);
914913
}
915914

916915
void goto_checkt::pointer_validity_check(
@@ -1436,11 +1435,7 @@ void goto_checkt::check_rec(
14361435
if(expr.type().id()==ID_signedbv ||
14371436
expr.type().id()==ID_unsignedbv)
14381437
{
1439-
if(expr.operands().size()==2 &&
1440-
expr.op0().type().id()==ID_pointer)
1441-
pointer_overflow_check(expr, guard);
1442-
else
1443-
integer_overflow_check(expr, guard);
1438+
integer_overflow_check(expr, guard);
14441439
}
14451440
else if(expr.type().id()==ID_floatbv)
14461441
{

0 commit comments

Comments
 (0)