Skip to content

Pointer difference over void* is difference over char* #1631

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions regression/cbmc/void_pointer3/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <stddef.h>

int main()
{
// make sure they are NULL objects
void *p=0, *q=0;
// with the object:offset encoding we need to make sure the address arithmetic
// below will only touch the offset part
__CPROVER_assume(sizeof(unsigned char)<sizeof(void*));
unsigned char o1, o2;
// there is ample undefined behaviour here, but the goal of this test solely
// is exercising CBMC's pointer subtraction encoding
p+=o1;
q+=o2;

ptrdiff_t d=p-q;
__CPROVER_assert(p-d==q, "");
return 0;
}
8 changes: 8 additions & 0 deletions regression/cbmc/void_pointer3/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CORE
main.c

^EXIT=0$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
--
^warning: ignoring
16 changes: 10 additions & 6 deletions src/solvers/flattening/bv_pointers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,9 +412,11 @@ bvt bv_pointerst::convert_pointer_type(const exprt &expr)

bv=convert_bv(expr.op0());

mp_integer element_size=
pointer_offset_size(expr.op0().type().subtype(), ns);
DATA_INVARIANT(element_size>0, "object size expected to be non-zero");
typet pointer_sub_type=expr.op0().type().subtype();
if(pointer_sub_type.id()==ID_empty)
pointer_sub_type=char_type();
mp_integer element_size=pointer_offset_size(pointer_sub_type, ns);
DATA_INVARIANT(element_size>0, "object size expected to be positive");

offset_arithmetic(bv, element_size, neg_op1);

Expand Down Expand Up @@ -469,9 +471,11 @@ bvt bv_pointerst::convert_bitvector(const exprt &expr)

bvt bv=bv_utils.sub(op0, op1);

mp_integer element_size=
pointer_offset_size(expr.op0().type().subtype(), ns);
DATA_INVARIANT(element_size>0, "object size expected to be non-zero");
typet pointer_sub_type=expr.op0().type().subtype();
if(pointer_sub_type.id()==ID_empty)
pointer_sub_type=char_type();
mp_integer element_size=pointer_offset_size(pointer_sub_type, ns);
DATA_INVARIANT(element_size>0, "object size expected to be positive");

if(element_size!=1)
{
Expand Down