Debian Codegraph @

clang/test/Analysis/pointer-sub-notes.c

raw
1 // RUN: %clang_analyze_cc1 -analyzer-checker=alpha.core.PointerSub -analyzer-output=text -verify %s
2
3 void negative_1() {
4 int a[3];
5 int x = -1;
6 // FIXME: should indicate that 'x' is -1
7 int d = &a[x] - &a[0]; // expected-warning{{Using a negative array index at pointer subtraction is undefined behavior}} \
8 // expected-note{{Using a negative array index at pointer subtraction is undefined behavior}}
9 }
10
11 void negative_2() {
12 int a[3];
13 int *p1 = a, *p2 = a;
14 --p2;
15 // FIXME: should indicate that 'p2' is negative
16 int d = p1 - p2; // expected-warning{{Using a negative array index at pointer subtraction is undefined behavior}} \
17 // expected-note{{Using a negative array index at pointer subtraction is undefined behavior}}
18 }
19
20 void different_1() {
21 int a[3]; // expected-note{{Array at the left-hand side of subtraction}}
22 int b[3]; // expected-note{{Array at the right-hand side of subtraction}}
23 int d = &a[2] - &b[0]; // expected-warning{{Subtraction of two pointers that do not point into the same array is undefined behavior}} \
24 // expected-note{{Subtraction of two pointers that do not point into the same array is undefined behavior}}
25 }
26
27 void different_2() {
28 int a[3]; // expected-note{{Array at the right-hand side of subtraction}}
29 int b[3]; // expected-note{{Array at the left-hand side of subtraction}}
30 int *p1 = a + 1;
31 int *p2 = b;
32 int d = p2 - p1; // expected-warning{{Subtraction of two pointers that do not point into the same array is undefined behavior}} \
33 // expected-note{{Subtraction of two pointers that do not point into the same array is undefined behavior}}
34 }
35
36 int different_3() {
37 struct {
38 int array[5];
39 } a, b;
40 return &a.array[3] - &b.array[2]; // expected-warning{{Subtraction of two pointers that do not point into the same array is undefined behavior}} \
41 // expected-note{{Subtraction of two pointers that do not point into the same array is undefined behavior}}
42 }
43
44 int different_4() {
45 struct {
46 int array1[5]; // expected-note{{Array at the left-hand side of subtraction}}
47 int array2[5]; // expected-note{{Array at the right-hand side of subtraction}}
48 } a;
49 return &a.array1[3] - &a.array2[4]; // expected-warning{{Subtraction of two pointers that do not point into the same array is undefined behavior}} \
50 // expected-note{{Subtraction of two pointers that do not point into the same array is undefined behavior}}
51 }
52
53 void different_5() {
54 int d;
55 static int x[10][10]; // expected-note2{{Array at the left-hand side of subtraction}}
56 int *y1 = &(x[3][5]);
57 char *z = ((char *) y1) + 2;
58 int *y2 = (int *)(z - 2);
59 int *y3 = ((int *)x) + 35; // This is offset for [3][5].
60
61 d = y2 - y1; // expected-warning{{Subtraction of two pointers that do not point into the same array is undefined behavior}} \
62 // expected-note{{Subtraction of two pointers that do not point into the same array is undefined behavior}}
63 d = y3 - y1; // expected-warning{{Subtraction of two pointers that do not point into the same array is undefined behavior}} \
64 // expected-note{{Subtraction of two pointers that do not point into the same array is undefined behavior}}
65 d = y3 - y2;
66 }