Debian Codegraph @

clang/test/Analysis/pointer-sub.c

raw
1 // RUN: %clang_analyze_cc1 -analyzer-checker=alpha.core.PointerSub -verify %s
2
3 void f1(void) {
4 int x, y, z[10];
5 int d = &y - &x; // expected-warning{{Subtraction of two pointers that do not point into the same array is undefined behavior}}
6 d = z - &y; // expected-warning{{Subtraction of two pointers that do not point into the same array is undefined behavior}}
7 d = &x - &x; // no-warning (subtraction of any two identical pointers is allowed)
8 d = (long *)&x - (long *)&x;
9 d = (&x + 1) - &x; // no-warning ('&x' is like a single-element array)
10 d = &x - (&x + 1); // no-warning
11 d = (&x + 0) - &x; // no-warning
12 d = (&x - 1) - &x; // expected-warning{{Indexing the address of a variable with other than 1 at this place is undefined behavior}}
13 d = (&x + 2) - &x; // expected-warning{{Indexing the address of a variable with other than 1 at this place is undefined behavior}}
14
15 d = (z + 9) - z; // no-warning (pointers to same array)
16 d = (z + 10) - z; // no-warning (pointer to "one after the end")
17 d = (z + 11) - z; // expected-warning{{Using an array index greater than the array size at pointer subtraction is undefined behavior}}
18 d = (z - 1) - z; // expected-warning{{Using a negative array index at pointer subtraction is undefined behavior}}
19 }
20
21 void f2(void) {
22 int a[10], b[10], c; // expected-note{{Array at the left-hand side of subtraction}} \
23 // expected-note2{{Array at the right-hand side of subtraction}}
24 int *p = &a[2];
25 int *q = &a[8];
26 int d = q - p; // no-warning (pointers into the same array)
27
28 q = &b[3];
29 d = q - p; // expected-warning{{Subtraction of two pointers that}}
30
31 q = a + 10;
32 d = q - p; // no warning (use of pointer to one after the end is allowed)
33 q = a + 11;
34 d = q - a; // expected-warning{{Using an array index greater than the array size at pointer subtraction is undefined behavior}}
35
36 d = &a[4] - a; // no-warning
37 d = &a[2] - p; // no-warning
38 d = &c - p; // expected-warning{{Subtraction of two pointers that}}
39
40 d = (int *)((char *)(&a[4]) + sizeof(int)) - &a[4]; // no-warning (pointers into the same array data)
41 d = (int *)((char *)(&a[4]) + 1) - &a[4]; // expected-warning{{Subtraction of two pointers that}}
42 }
43
44 void f3(void) {
45 int a[3][4]; // expected-note{{Array at the left-hand side of subtraction}} \
46 // expected-note2{{Array at the right-hand side of subtraction}}
47 int d;
48
49 d = &(a[2]) - &(a[1]);
50 d = a[2] - a[1]; // expected-warning{{Subtraction of two pointers that}}
51 d = a[1] - a[1];
52 d = &(a[1][2]) - &(a[1][0]);
53 d = &(a[1][2]) - &(a[0][0]); // expected-warning{{Subtraction of two pointers that}}
54
55 d = (int *)((char *)(&a[2][2]) + sizeof(int)) - &a[2][2]; // expected-warning{{Subtraction of two pointers that}}
56 d = (int *)((char *)(&a[2][2]) + 1) - &a[2][2]; // expected-warning{{Subtraction of two pointers that}}
57 d = (int (*)[4])((char *)&a[2] + sizeof(int (*)[4])) - &a[2]; // expected-warning{{Subtraction of two pointers that}}
58 d = (int (*)[4])((char *)&a[2] + 1) - &a[2]; // expected-warning{{Subtraction of two pointers that}}
59 }
60
61 void f4(void) {
62 int n = 4, m = 3;
63 int a[n][m];
64 int (*p)[m] = a; // p == &a[0]
65 p += 1; // p == &a[1]
66
67 // FIXME: This is a known problem with -Wpointer-arith (https://github.com/llvm/llvm-project/issues/28328)
68 int d = p - a; // d == 1 // expected-warning{{subtraction of pointers to type 'int[m]' of zero size has undefined behavior}}
69
70 // FIXME: This is a known problem with -Wpointer-arith (https://github.com/llvm/llvm-project/issues/28328)
71 d = &(a[2]) - &(a[1]); // expected-warning{{subtraction of pointers to type 'int[m]' of zero size has undefined behavior}}
72
73 d = a[2] - a[1]; // expected-warning{{Subtraction of two pointers that}}
74 }
75
76 typedef struct {
77 int a;
78 int b;
79 int c[10]; // expected-note2{{Array at the right-hand side of subtraction}}
80 int d[10]; // expected-note2{{Array at the left-hand side of subtraction}}
81 } S;
82
83 void f5(void) {
84 S s;
85 int y;
86 int d;
87
88 d = &s.b - &s.a; // expected-warning{{Subtraction of two pointers that}}
89 d = &s.c[0] - &s.a; // expected-warning{{Subtraction of two pointers that}}
90 d = &s.b - &y; // expected-warning{{Subtraction of two pointers that}}
91 d = &s.c[3] - &s.c[2];
92 d = &s.d[3] - &s.c[2]; // expected-warning{{Subtraction of two pointers that}}
93 d = s.d - s.c; // expected-warning{{Subtraction of two pointers that}}
94
95 S sa[10];
96 d = &sa[2] - &sa[1];
97 d = &sa[2].a - &sa[1].b; // expected-warning{{Subtraction of two pointers that}}
98 }
99
100 void f6(void) {
101 long long l;
102 char *a1 = (char *)&l;
103 int d = a1[3] - l;
104
105 long long la1[3]; // expected-note{{Array at the right-hand side of subtraction}}
106 long long la2[3]; // expected-note{{Array at the left-hand side of subtraction}}
107 char *pla1 = (char *)la1;
108 char *pla2 = (char *)la2;
109 d = pla1[1] - pla1[0];
110 d = (long long *)&pla1[1] - &l; // expected-warning{{Subtraction of two pointers that}}
111 d = &pla2[3] - &pla1[3]; // expected-warning{{Subtraction of two pointers that}}
112 }
113
114 void f7(int *p) {
115 int a[10];
116 int d = &a[10] - p; // no-warning ('p' is unknown, even if it cannot point into 'a')
117 }
118
119 void f8(int n) {
120 int a[10];
121 int d = a[n] - a[0];
122 }