| 1 |
// RUN: %clang_analyze_cc1 -verify %s \ |
| 2 |
// RUN: -analyzer-checker=core |
| 3 |
|
| 4 |
extern int *get_pointer(); |
| 5 |
|
| 6 |
int *test_add1(int offset) { |
| 7 |
int *p = get_pointer(); |
| 8 |
if (p) {} |
| 9 |
return p + offset; // expected-warning{{Addition of a null pointer (from variable 'p') and a probably nonzero integer value (from variable 'offset') may result in undefined behavior}} |
| 10 |
} |
| 11 |
|
| 12 |
int *test_add2(int offset) { |
| 13 |
int *p = get_pointer(); |
| 14 |
if (p) {} |
| 15 |
if (offset) {} |
| 16 |
return p + offset; // expected-warning{{Addition of a null pointer (from variable 'p') and a nonzero integer value (from variable 'offset') results in undefined behavior}} |
| 17 |
} |
| 18 |
|
| 19 |
int *test_add3(int offset) { |
| 20 |
int *p = get_pointer(); |
| 21 |
if (p) {} |
| 22 |
if (offset != 0) return 0; |
| 23 |
return p + offset; |
| 24 |
} |
| 25 |
|
| 26 |
int *test_add4(int offset) { |
| 27 |
int *p = get_pointer(); |
| 28 |
if (p) {} |
| 29 |
if (offset == 0) return 0; |
| 30 |
return p + offset; // expected-warning{{Addition of a null pointer (from variable 'p') and a nonzero integer value (from variable 'offset') results in undefined behavior}} |
| 31 |
} |
| 32 |
|
| 33 |
int *test_add5(int offset) { |
| 34 |
int *p = get_pointer(); |
| 35 |
if (p) {} |
| 36 |
return offset + p; // expected-warning{{Addition of a probably nonzero integer value (from variable 'offset') and a null pointer (from variable 'p') may result in undefined behavior}} |
| 37 |
} |
| 38 |
|
| 39 |
int *test_sub1(int offset) { |
| 40 |
int *p = get_pointer(); |
| 41 |
if (p) {} |
| 42 |
return p - offset; // expected-warning{{Subtraction of a null pointer (from variable 'p') and a probably nonzero integer value (from variable 'offset') may result in undefined behavior}} |
| 43 |
} |
| 44 |
|
| 45 |
int test_sub_p1() { |
| 46 |
int *p = get_pointer(); |
| 47 |
if (p) {} |
| 48 |
return p - p; |
| 49 |
} |
| 50 |
|
| 51 |
int test_sub_p2() { |
| 52 |
int *p1 = get_pointer(); |
| 53 |
int *p2 = get_pointer(); |
| 54 |
if (p1) {} |
| 55 |
if (p2) {} |
| 56 |
return p1 - p2; |
| 57 |
// expected-warning@-1{{Subtraction of a non-null pointer (from variable 'p1') and a null pointer (from variable 'p2') results in undefined behavior}} |
| 58 |
// expected-warning@-2{{Subtraction of a null pointer (from variable 'p1') and a non-null pointer (from variable 'p2') results in undefined behavior}} |
| 59 |
} |
| 60 |
|
| 61 |
int test_sub_p3() { |
| 62 |
int *p1 = get_pointer(); |
| 63 |
int *p2 = get_pointer(); |
| 64 |
if (p1) {} |
| 65 |
return p1 - p2; // expected-warning{{Subtraction of a null pointer (from variable 'p1') and a probably non-null pointer (from variable 'p2') may result in undefined behavior}} |
| 66 |
} |
| 67 |
|
| 68 |
struct S { |
| 69 |
char *p; |
| 70 |
int offset; |
| 71 |
}; |
| 72 |
|
| 73 |
char *test_struct(struct S s) { |
| 74 |
if (s.p) {} |
| 75 |
return s.p + s.offset; // expected-warning{{Addition of a null pointer (via field 'p') and a probably nonzero integer value (via field 'offset') may result in undefined behavior}} |
| 76 |
} |