Skip to content

Commit 7f6443b

Browse files
committed
The sub-size of void* is undefined; use char* as specified in the standard
This patch includes a review of all uses of functions from pointer_offset_size.h as all of those may return nil or a negative number if the size could not be determined. Calling sites need to handle those cases as appropriate in a given context.
1 parent 20de0d7 commit 7f6443b

File tree

19 files changed

+130
-15
lines changed

19 files changed

+130
-15
lines changed

regression/cbmc/void_pointer1/main.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
char buffer[2];
2+
int length = 2;
3+
4+
void func(void* buf, int len)
5+
{
6+
while( len-- )
7+
*(char *)buf++;
8+
}
9+
10+
void main(){
11+
func(buffer,length);
12+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
main.c
3+
--pointer-check
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring

regression/cbmc/void_pointer2/main.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
char buffer[2];
2+
int length = 2;
3+
4+
void func(void* buf, int len)
5+
{
6+
while( len-- )
7+
*(char *)buf++;
8+
}
9+
10+
void main(){
11+
func(buffer,length);
12+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
main.c
3+
--pointer-check --no-simplify --unwind 3
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring

src/analyses/goto_rw.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ void rw_range_sett::get_objects_complex(
149149

150150
range_spect sub_size=
151151
to_range_spect(pointer_offset_bits(op.type().subtype(), ns));
152+
assert(sub_size>0);
152153
range_spect offset=
153154
(range_start==-1 || expr.id()==ID_complex_real) ? 0 : sub_size;
154155

src/ansi-c/c_typecheck_expr.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,8 +1318,8 @@ void c_typecheck_baset::typecheck_expr_typecast(exprt &expr)
13181318
// an integer/float of the same size
13191319
if((expr_type.id()==ID_signedbv ||
13201320
expr_type.id()==ID_unsignedbv) &&
1321-
pointer_offset_size(expr_type, *this)==
1322-
pointer_offset_size(op_vector_type, *this))
1321+
pointer_offset_bits(expr_type, *this)==
1322+
pointer_offset_bits(op_vector_type, *this))
13231323
{
13241324
}
13251325
else

src/ansi-c/padding.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,9 @@ void add_padding(union_typet &type, const namespacet &ns)
339339
mp_integer max_alignment=alignment(type, ns)*8;
340340
mp_integer size_bits=pointer_offset_bits(type, ns);
341341

342+
if(size_bits<0)
343+
throw "type of unknown size:\n"+type.pretty();
344+
342345
union_typet::componentst &components=type.components();
343346

344347
// Is the union packed?

src/goto-instrument/alignment_checks.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ void print_struct_alignment_problems(
6060
const namespacet ns(symbol_table);
6161
mp_integer size=pointer_offset_size(it_type, ns);
6262

63+
if(size<0)
64+
throw "type of unknown size:\n"+it_type.pretty();
65+
6366
cumulated_length+=size;
6467
// [it_mem;it_next] cannot be covered by an instruction
6568
if(cumulated_length>config.ansi_c.memory_operand_size)
@@ -99,6 +102,9 @@ void print_struct_alignment_problems(
99102
const mp_integer size=
100103
pointer_offset_size(array.subtype(), ns);
101104

105+
if(size<0)
106+
throw "type of unknown size:\n"+it_type.pretty();
107+
102108
if(2*integer2long(size)<=config.ansi_c.memory_operand_size)
103109
{
104110
out << "\nWARNING: "

src/pointer-analysis/value_set.cpp

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -717,10 +717,23 @@ void value_sett::get_value_set_rec(
717717

718718
if(i_is_set)
719719
{
720-
i*=pointer_offset_size(ptr_operand.type().subtype(), ns);
720+
typet pointer_sub_type=ptr_operand.type().subtype();
721+
if(pointer_sub_type.id()==ID_empty)
722+
pointer_sub_type=char_type();
721723

722-
if(expr.id()==ID_minus)
723-
i.negate();
724+
mp_integer size=pointer_offset_size(pointer_sub_type, ns);
725+
726+
if(size<=0)
727+
{
728+
i_is_set=false;
729+
}
730+
else
731+
{
732+
i*=size;
733+
734+
if(expr.id()==ID_minus)
735+
i.negate();
736+
}
724737
}
725738

726739
get_value_set_rec(
@@ -1155,7 +1168,14 @@ void value_sett::get_reference_set_rec(
11551168
}
11561169
else if(!to_integer(offset, i) &&
11571170
o.offset_is_zero())
1158-
o.offset=i*pointer_offset_size(array_type.subtype(), ns);
1171+
{
1172+
mp_integer size=pointer_offset_size(array_type.subtype(), ns);
1173+
1174+
if(size<=0)
1175+
o.offset_is_set=false;
1176+
else
1177+
o.offset=i*size;
1178+
}
11591179
else
11601180
o.offset_is_set=false;
11611181

src/pointer-analysis/value_set_dereference.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,10 @@ value_set_dereferencet::valuet value_set_dereferencet::build_reference_to(
566566
// no need to adjust offset
567567
adjusted_offset=offset;
568568
}
569+
else if(element_size<=0)
570+
{
571+
throw "unknown or invalid type size of:\n"+dereference_type.pretty();
572+
}
569573
else
570574
{
571575
exprt element_size_expr=
@@ -965,7 +969,12 @@ bool value_set_dereferencet::memory_model_bytes(
965969
// upper bound
966970
{
967971
mp_integer from_width=pointer_offset_size(from_type, ns);
972+
if(from_width<=0)
973+
throw "unknown or invalid type size:\n"+from_type.pretty();
974+
968975
mp_integer to_width=pointer_offset_size(to_type, ns);
976+
if(to_width<=0)
977+
throw "unknown or invalid type size:\n"+to_type.pretty();
969978

970979
exprt bound=from_integer(from_width-to_width, offset.type());
971980

0 commit comments

Comments
 (0)