[AMDGPU][AsmParser][NFC] Get rid of custom default operand handlers.
[llvm-project.git] / clang / test / Analysis / cert / env34-c.c
blobdc7b0340c311ed58b8b88618ea9c1cce3cf9a601
1 // RUN: %clang_analyze_cc1 \
2 // RUN: -analyzer-checker=alpha.security.cert.env.InvalidPtr\
3 // RUN: -analyzer-output=text -verify -Wno-unused %s
5 #include "../Inputs/system-header-simulator.h"
6 char *getenv(const char *name);
7 char *setlocale(int category, const char *locale);
8 char *strerror(int errnum);
10 typedef struct {
11 char * field;
12 } lconv;
13 lconv *localeconv(void);
15 typedef struct {
16 } tm;
17 char *asctime(const tm *timeptr);
19 int strcmp(const char*, const char*);
20 extern void foo(char *e);
21 extern char* bar(void);
24 void getenv_test1(void) {
25 char *p;
27 p = getenv("VAR");
28 *p; // no-warning
30 p = getenv("VAR2");
31 *p; // no-warning, getenv result was assigned to the same pointer
34 void getenv_test2(void) {
35 char *p, *p2;
37 p = getenv("VAR");
38 // expected-note@-1{{previous function call was here}}
39 *p; // no-warning
41 p2 = getenv("VAR2");
42 // expected-note@-1{{'getenv' call may invalidate the result of the previous 'getenv'}}
44 *p;
45 // expected-warning@-1{{dereferencing an invalid pointer}}
46 // expected-note@-2{{dereferencing an invalid pointer}}
49 void getenv_test3(void) {
50 char *p, *p2, *p3;
52 p = getenv("VAR");
53 *p; // no-warning
55 p = getenv("VAR2");
56 // expected-note@-1{{previous function call was here}}
57 p2 = getenv("VAR2");
58 // expected-note@-1{{'getenv' call may invalidate the result of the previous 'getenv'}}
60 p3 = getenv("VAR3");
62 *p;
63 // expected-warning@-1{{dereferencing an invalid pointer}}
64 // expected-note@-2{{dereferencing an invalid pointer}}
67 void getenv_test4(void) {
68 char *p, *p2, *p3;
70 p = getenv("VAR");
71 // expected-note@-1{{previous function call was here}}
72 p2 = getenv("VAR2");
73 // expected-note@-1{{'getenv' call may invalidate the result of the previous 'getenv'}}
74 p3 = getenv("VAR3");
76 *p;
77 // expected-warning@-1{{dereferencing an invalid pointer}}
78 // expected-note@-2{{dereferencing an invalid pointer}}
81 void getenv_test5(void) {
82 char *p, *p2, *p3;
84 p = getenv("VAR");
85 p2 = getenv("VAR2");
86 // expected-note@-1{{previous function call was here}}
87 p3 = getenv("VAR3");
88 // expected-note@-1{{'getenv' call may invalidate the result of the previous 'getenv'}}
90 *p2;
91 // expected-warning@-1{{dereferencing an invalid pointer}}
92 // expected-note@-2{{dereferencing an invalid pointer}}
95 void getenv_test6(void) {
96 char *p, *p2;
97 p = getenv("VAR");
98 *p; // no-warning
100 p = getenv("VAR2");
101 // expected-note@-1{{previous function call was here}}
102 *p; // no-warning
104 p2 = getenv("VAR3");
105 // expected-note@-1{{previous function call was here}}
106 // expected-note@-2{{'getenv' call may invalidate the result of the previous 'getenv'}}
109 // expected-warning@-1{{dereferencing an invalid pointer}}
110 // expected-note@-2{{dereferencing an invalid pointer}}
112 *p2; // no-warning
114 p = getenv("VAR4");
115 // expected-note@-1{{'getenv' call may invalidate the result of the previous 'getenv'}}
117 *p; // no-warning
118 *p2;
119 // expected-warning@-1{{dereferencing an invalid pointer}}
120 // expected-note@-2{{dereferencing an invalid pointer}}
123 void getenv_test7(void) {
124 char *p, *p2;
125 p = getenv("VAR");
126 // expected-note@-1{{previous function call was here}}
127 *p; // no-warning
129 p2 = getenv("VAR2");
130 // expected-note@-1{{'getenv' call may invalidate the result of the previous 'getenv'}}
132 foo(p);
133 // expected-warning@-1{{use of invalidated pointer 'p' in a function call}}
134 // expected-note@-2{{use of invalidated pointer 'p' in a function call}}
137 void getenv_test8(void) {
138 static const char *array[] = {
141 "/var/tmp",
142 "/usr/tmp",
143 "/tmp",
147 if( !array[0] )
148 // expected-note@-1{{Taking true branch}}
149 array[0] = getenv("TEMPDIR");
150 // expected-note@-1{{previous function call was here}}
152 if( !array[1] )
153 // expected-note@-1{{Taking true branch}}
154 array[1] = getenv("TMPDIR");
155 // expected-note@-1{{'getenv' call may invalidate the result of the previous 'getenv'}}
157 *array[0];
158 // expected-warning@-1{{dereferencing an invalid pointer}}
159 // expected-note@-2{{dereferencing an invalid pointer}}
162 void getenv_test9(void) {
163 char *p, *p2;
164 p = getenv("something");
165 p = bar();
166 p2 = getenv("something");
167 *p; // no-warning: p does not point to getenv anymore
170 void getenv_test10(void) {
171 strcmp(getenv("VAR1"), getenv("VAR2"));
172 // expected-note@-1{{'getenv' call may invalidate the result of the previous 'getenv'}}
173 // expected-note@-2{{previous function call was here}}
174 // expected-warning@-3{{use of invalidated pointer 'getenv("VAR1")' in a function call}}
175 // expected-note@-4{{use of invalidated pointer 'getenv("VAR1")' in a function call}}
178 void dereference_pointer(char* a) {
180 // expected-warning@-1{{dereferencing an invalid pointer}}
181 // expected-note@-2{{dereferencing an invalid pointer}}
184 void getenv_test11(void) {
185 char *p = getenv("VAR");
186 // expected-note@-1{{previous function call was here}}
188 char *pp = getenv("VAR2");
189 // expected-note@-1{{'getenv' call may invalidate the result of the previous 'getenv'}}
191 dereference_pointer(p);
192 // expected-note@-1{{Calling 'dereference_pointer'}}
195 void getenv_test12(int flag1, int flag2) {
196 char *p = getenv("VAR");
197 // expected-note@-1{{previous function call was here}}
199 if (flag1) {
200 // expected-note@-1{{Assuming 'flag1' is not equal to 0}}
201 // expected-note@-2{{Taking true branch}}
202 char *pp = getenv("VAR2");
203 // expected-note@-1{{'getenv' call may invalidate the result of the previous 'getenv'}}
206 if (flag2) {
207 // expected-note@-1{{Assuming 'flag2' is not equal to 0}}
208 // expected-note@-2{{Taking true branch}}
210 // expected-warning@-1{{dereferencing an invalid pointer}}
211 // expected-note@-2{{dereferencing an invalid pointer}}
215 void setlocale_test1(void) {
216 char *p, *p2;
217 p = setlocale(0, "VAR");
218 *p; // no-warning
220 p = setlocale(0, "VAR2");
221 // expected-note@-1{{previous function call was here}}
222 *p; // no-warning
224 p2 = setlocale(0, "VAR3");
225 // expected-note@-1{{'setlocale' call may invalidate the result of the previous 'setlocale'}}
228 // expected-warning@-1{{dereferencing an invalid pointer}}
229 // expected-note@-2{{dereferencing an invalid pointer}}
232 void setlocale_test2(int flag) {
233 char *p, *p2;
234 p = setlocale(0, "VAR");
235 *p; // no-warning
237 p = setlocale(0, "VAR2");
238 // expected-note@-1{{previous function call was here}}
239 *p; // no-warning
241 if (flag) {
242 // expected-note@-1{{Assuming 'flag' is not equal to 0}}
243 // expected-note@-2{{Taking true branch}}
244 p2 = setlocale(0, "VAR3");
245 // expected-note@-1{{'setlocale' call may invalidate the result of the previous 'setlocale'}}
249 // expected-warning@-1{{dereferencing an invalid pointer}}
250 // expected-note@-2{{dereferencing an invalid pointer}}
253 void strerror_test1(void) {
254 char *p, *p2;
256 p = strerror(0);
257 *p; // no-warning
259 p = strerror(1);
260 // expected-note@-1{{previous function call was here}}
261 *p; // no-warning
263 p2 = strerror(2);
264 // expected-note@-1{{'strerror' call may invalidate the result of the previous 'strerror'}}
267 // expected-warning@-1{{dereferencing an invalid pointer}}
268 // expected-note@-2{{dereferencing an invalid pointer}}
271 void strerror_test2(int errno) {
272 char *p, *p2;
274 p = strerror(0);
275 *p; // no-warning
277 p = strerror(1);
278 // expected-note@-1{{previous function call was here}}
279 *p; // no-warning
281 if (0 == 1) {
282 // expected-note@-1{{0 is not equal to 1}}
283 // expected-note@-2{{Taking false branch}}
284 p2 = strerror(2);
287 *p; // no-warning
289 if (errno) {
290 // expected-note@-1{{Assuming 'errno' is not equal to 0}}
291 // expected-note@-2{{Taking true branch}}
292 p2 = strerror(errno);
293 // expected-note@-1{{'strerror' call may invalidate the result of the previous 'strerror'}}
297 // expected-warning@-1{{dereferencing an invalid pointer}}
298 // expected-note@-2{{dereferencing an invalid pointer}}
301 void asctime_test(void) {
302 const tm *t;
303 const tm *tt;
305 char* p = asctime(t);
306 // expected-note@-1{{previous function call was here}}
307 char* pp = asctime(tt);
308 // expected-note@-1{{'asctime' call may invalidate the result of the previous 'asctime'}}
311 // expected-warning@-1{{dereferencing an invalid pointer}}
312 // expected-note@-2{{dereferencing an invalid pointer}}
315 void localeconv_test1(void) {
316 lconv *lc1 = localeconv();
317 // expected-note@-1{{previous function call was here}}
318 lconv *lc2 = localeconv();
319 // expected-note@-1{{'localeconv' call may invalidate the result of the previous 'localeconv'}}
321 *lc1;
322 // expected-warning@-1{{dereferencing an invalid pointer}}
323 // expected-note@-2{{dereferencing an invalid pointer}}
326 void localeconv_test2(void) {
327 // TODO: false negative
328 lconv *lc1 = localeconv();
329 lconv *lc2 = localeconv();
330 lc1->field;