Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 1 | // RUN: clang -checker-simple -verify %s |
Ted Kremenek | 2f54af4 | 2008-04-02 16:54:39 +0000 | [diff] [blame] | 2 | |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 3 | #include<stdint.h> |
| 4 | |
Ted Kremenek | 2f54af4 | 2008-04-02 16:54:39 +0000 | [diff] [blame] | 5 | void f1(int *p) { |
| 6 | if (p) *p = 1; |
| 7 | else *p = 0; // expected-warning{{ereference}} |
| 8 | } |
Ted Kremenek | b9ab690 | 2008-04-21 23:44:17 +0000 | [diff] [blame] | 9 | |
| 10 | struct foo_struct { |
| 11 | int x; |
| 12 | }; |
| 13 | |
| 14 | int f2(struct foo_struct* p) { |
| 15 | |
| 16 | if (p) |
| 17 | p->x = 1; |
| 18 | |
Ted Kremenek | 3603d73 | 2008-04-21 23:45:26 +0000 | [diff] [blame] | 19 | return p->x++; // expected-warning{{Dereference of null pointer.}} |
Ted Kremenek | b9ab690 | 2008-04-21 23:44:17 +0000 | [diff] [blame] | 20 | } |
Ted Kremenek | 9704eac | 2008-04-22 04:56:55 +0000 | [diff] [blame] | 21 | |
| 22 | int f3(char* x) { |
| 23 | |
| 24 | int i = 2; |
| 25 | |
| 26 | if (x) |
| 27 | return x[i - 1]; |
| 28 | |
| 29 | return x[i+1]; // expected-warning{{Dereference of null pointer.}} |
| 30 | } |
| 31 | |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 32 | int f4(int *p) { |
| 33 | |
| 34 | uintptr_t x = p; |
| 35 | |
| 36 | if (x) |
| 37 | return 1; |
| 38 | |
| 39 | int *q = (int*) x; |
| 40 | return *q; // expected-warning{{Dereference of null pointer.}} |
Ted Kremenek | a548846 | 2008-04-22 21:39:21 +0000 | [diff] [blame^] | 41 | } |
| 42 | |
| 43 | int f5() { |
| 44 | |
| 45 | char *s = "hello world"; |
| 46 | return s[0]; // no-warning |
| 47 | } |
| 48 | |