source: src/Clight/test/endptr.c

Last change on this file was 2393, checked in by campbell, 8 years ago

A pointer comparison test case that illustrates a bug.

File size: 913 bytes
Line 
1/* C allows pointers just past the end of an object (e.g., to facilitate
2   some looping code that maintains a pointer into an array), but we need
3   to be careful that we don't leak too much information about neighbouring
4   data.  In the code below, a compiler might have &py+1 equal to the address
5   of one of the adjacent variables, so the semantics should leave the
6   comparison undefined so that the stack frame layout can be decided by the
7   compiler. */
8
9int main(void) {
10  int x,y,z;
11  int *px = &x;
12  int *py = &y;
13  int *pz = &z;
14  int *px1 = &x + 1;
15  int *py1 = &y + 1;
16  int *pz1 = &z + 1;
17
18  if (px1 - px != 1
19   || py1 - py != 1
20   || pz1 - pz != 1) return 1;
21
22  if (py == py1
23   || py+1 != py1) return 2;
24
25  /* These should be undefined because they may "change" during compilation */
26  if (px1 == py
27   || py1 == pz
28   || py1 == px
29   || py1 == pz1
30   || py1 == px1) return 3;
31
32  return 4;
33}
Note: See TracBrowser for help on using the repository browser.