Example: pointer_offset_from_ptr_subtraction_global_xy.c

up: index
prev: cheri_03_ii.c
next: pointer_offset_from_ptr_subtraction_global_yx.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
    #include <stdio.h>
    #include <string.h> 
    #include <stddef.h>
    int x=1, y=2;
    int main() {
      int *p = &x;
      int *q = &y;
      ptrdiff_t offset = q - p;
      int *r = p + offset;
      if (memcmp(&r, &q, sizeof(r)) == 0) {
        *r = 11; // is this free of UB?
        printf("y=%d *q=%d *r=%d\n",y,*q,*r); 
      }
    }
[link to run test in Cerberus]

Experimental data (what does this mean?)

cerberus-concrete-PVI-plain Undefined behaviour: the subtraction of two pointers must be between pointers that points into, or just beyond, the same array object at other_location(Concrete)
cerberus-concrete-PVI-ae Undefined behaviour: the subtraction of two pointers must be between pointers that points into, or just beyond, the same array object at other_location(Concrete)
cerberus-concrete-PVI-ae-udi Undefined behaviour: the subtraction of two pointers must be between pointers that points into, or just beyond, the same array object at other_location(Concrete)
gcc-8.3-O0 y=11 *q=11 *r=11
gcc-8.3-O2 y=11 *q=11 *r=11
gcc-8.3-O3 y=11 *q=11 *r=11
gcc-8.3-O2-no-strict-aliasing y=11 *q=11 *r=11
gcc-8.3-O3-no-strict-aliasing y=11 *q=11 *r=11
clang-7.0.1-O0 y=11 *q=11 *r=11
clang-7.0.1-O2 y=11 *q=11 *r=11
clang-7.0.1-O3 y=11 *q=11 *r=11
clang-7.0.1-O2-no-strict-aliasing y=11 *q=11 *r=11
clang-7.0.1-O3-no-strict-aliasing y=11 *q=11 *r=11
icc-19-O0 y=11 *q=11 *r=11
icc-19-O2 y=11 *q=11 *r=11
icc-19-O3 y=11 *q=11 *r=11
icc-19-O2-no-strict-aliasing y=11 *q=11 *r=11
icc-19-O3-no-strict-aliasing y=11 *q=11 *r=11