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> |
Ted Kremenek | 8c3e7fb | 2008-09-16 23:24:45 +0000 | [diff] [blame] | 4 | #include <assert.h> |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 5 | |
Ted Kremenek | 2f54af4 | 2008-04-02 16:54:39 +0000 | [diff] [blame] | 6 | void f1(int *p) { |
7 | if (p) *p = 1; | ||||
8 | else *p = 0; // expected-warning{{ereference}} | ||||
9 | } | ||||
Ted Kremenek | b9ab690 | 2008-04-21 23:44:17 +0000 | [diff] [blame] | 10 | |
11 | struct foo_struct { | ||||
12 | int x; | ||||
13 | }; | ||||
14 | |||||
15 | int f2(struct foo_struct* p) { | ||||
16 | |||||
17 | if (p) | ||||
18 | p->x = 1; | ||||
19 | |||||
Ted Kremenek | 3603d73 | 2008-04-21 23:45:26 +0000 | [diff] [blame] | 20 | return p->x++; // expected-warning{{Dereference of null pointer.}} |
Ted Kremenek | b9ab690 | 2008-04-21 23:44:17 +0000 | [diff] [blame] | 21 | } |
Ted Kremenek | 9704eac | 2008-04-22 04:56:55 +0000 | [diff] [blame] | 22 | |
23 | int f3(char* x) { | ||||
24 | |||||
25 | int i = 2; | ||||
26 | |||||
27 | if (x) | ||||
28 | return x[i - 1]; | ||||
29 | |||||
30 | return x[i+1]; // expected-warning{{Dereference of null pointer.}} | ||||
31 | } | ||||
32 | |||||
Ted Kremenek | e2013f5 | 2008-04-29 23:25:09 +0000 | [diff] [blame] | 33 | int f3_b(char* x) { |
34 | |||||
35 | int i = 2; | ||||
36 | |||||
37 | if (x) | ||||
38 | return x[i - 1]; | ||||
39 | |||||
40 | return x[i+1]++; // expected-warning{{Dereference of null pointer.}} | ||||
41 | } | ||||
42 | |||||
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 43 | int f4(int *p) { |
44 | |||||
Daniel Dunbar | 4489fe1 | 2008-08-05 00:07:51 +0000 | [diff] [blame] | 45 | uintptr_t x = (uintptr_t) p; |
Ted Kremenek | 0fe33bc | 2008-04-22 21:10:18 +0000 | [diff] [blame] | 46 | |
47 | if (x) | ||||
48 | return 1; | ||||
49 | |||||
50 | int *q = (int*) x; | ||||
51 | return *q; // expected-warning{{Dereference of null pointer.}} | ||||
Ted Kremenek | a548846 | 2008-04-22 21:39:21 +0000 | [diff] [blame] | 52 | } |
53 | |||||
54 | int f5() { | ||||
55 | |||||
56 | char *s = "hello world"; | ||||
57 | return s[0]; // no-warning | ||||
58 | } | ||||
59 | |||||
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 60 | int bar(int* p, int q) __attribute__((nonnull)); |
Ted Kremenek | 584def7 | 2008-07-22 00:46:16 +0000 | [diff] [blame] | 61 | |
62 | int f6(int *p) { | ||||
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 63 | return !p ? bar(p, 1) // expected-warning {{Null pointer passed as an argument to a 'nonnull' parameter}} |
64 | : bar(p, 0); // no-warning | ||||
65 | } | ||||
Ted Kremenek | 584def7 | 2008-07-22 00:46:16 +0000 | [diff] [blame] | 66 | |
Ted Kremenek | 22bda88 | 2008-07-31 20:31:27 +0000 | [diff] [blame] | 67 | int* qux(); |
68 | |||||
69 | int f7(int x) { | ||||
70 | |||||
71 | int* p = 0; | ||||
72 | |||||
73 | if (0 == x) | ||||
74 | p = qux(); | ||||
75 | |||||
76 | if (0 == x) | ||||
77 | *p = 1; // no-warning | ||||
78 | |||||
79 | return x; | ||||
80 | } | ||||
81 | |||||
Ted Kremenek | dd463b8 | 2008-08-16 00:45:40 +0000 | [diff] [blame] | 82 | int f8(int *p, int *q) { |
83 | if (!p) | ||||
84 | if (p) | ||||
85 | *p = 1; // no-warning | ||||
86 | |||||
87 | if (q) | ||||
88 | if (!q) | ||||
89 | *q = 1; // no-warning | ||||
90 | } | ||||
Ted Kremenek | 8c3e7fb | 2008-09-16 23:24:45 +0000 | [diff] [blame] | 91 | |
92 | int* qux(); | ||||
93 | |||||
94 | int f9(int len) { | ||||
95 | assert (len != 0); | ||||
96 | int *p = 0; | ||||
97 | |||||
98 | for (int i = 0; i < len; ++i) | ||||
Ted Kremenek | e2b0083 | 2008-09-16 23:25:28 +0000 | [diff] [blame] | 99 | p = qux(i); |
Ted Kremenek | 8c3e7fb | 2008-09-16 23:24:45 +0000 | [diff] [blame] | 100 | |
101 | return *p++; // no-warning | ||||
102 | } | ||||
Ted Kremenek | f6e5ec4 | 2008-09-17 22:24:13 +0000 | [diff] [blame^] | 103 | |
104 | int f9b(int len) { | ||||
105 | assert (len > 0); // note use of '>' | ||||
106 | int *p = 0; | ||||
107 | |||||
108 | for (int i = 0; i < len; ++i) | ||||
109 | p = qux(i); | ||||
110 | |||||
111 | return *p++; // no-warning | ||||
112 | } | ||||
113 |