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
);
13 lconv
*localeconv(void);
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) {
31 *p
; // no-warning, getenv result was assigned to the same pointer
34 void getenv_test2(void) {
38 // expected-note@-1{{previous function call was here}}
42 // expected-note@-1{{'getenv' call may invalidate the result of the previous 'getenv'}}
45 // expected-warning@-1{{dereferencing an invalid pointer}}
46 // expected-note@-2{{dereferencing an invalid pointer}}
49 void getenv_test3(void) {
56 // expected-note@-1{{previous function call was here}}
58 // expected-note@-1{{'getenv' call may invalidate the result of the previous 'getenv'}}
63 // expected-warning@-1{{dereferencing an invalid pointer}}
64 // expected-note@-2{{dereferencing an invalid pointer}}
67 void getenv_test4(void) {
71 // expected-note@-1{{previous function call was here}}
73 // expected-note@-1{{'getenv' call may invalidate the result of the previous 'getenv'}}
77 // expected-warning@-1{{dereferencing an invalid pointer}}
78 // expected-note@-2{{dereferencing an invalid pointer}}
81 void getenv_test5(void) {
86 // expected-note@-1{{previous function call was here}}
88 // expected-note@-1{{'getenv' call may invalidate the result of the previous 'getenv'}}
91 // expected-warning@-1{{dereferencing an invalid pointer}}
92 // expected-note@-2{{dereferencing an invalid pointer}}
95 void getenv_test6(void) {
101 // expected-note@-1{{previous function call was here}}
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}}
115 // expected-note@-1{{'getenv' call may invalidate the result of the previous 'getenv'}}
119 // expected-warning@-1{{dereferencing an invalid pointer}}
120 // expected-note@-2{{dereferencing an invalid pointer}}
123 void getenv_test7(void) {
126 // expected-note@-1{{previous function call was here}}
130 // expected-note@-1{{'getenv' call may invalidate the result of the previous 'getenv'}}
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
[] = {
148 // expected-note@-1{{Taking true branch}}
149 array
[0] = getenv("TEMPDIR");
150 // expected-note@-1{{previous function call was here}}
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'}}
158 // expected-warning@-1{{dereferencing an invalid pointer}}
159 // expected-note@-2{{dereferencing an invalid pointer}}
162 void getenv_test9(void) {
164 p
= getenv("something");
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}}
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'}}
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) {
217 p
= setlocale(0, "VAR");
220 p
= setlocale(0, "VAR2");
221 // expected-note@-1{{previous function call was here}}
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
) {
234 p
= setlocale(0, "VAR");
237 p
= setlocale(0, "VAR2");
238 // expected-note@-1{{previous function call was here}}
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) {
260 // expected-note@-1{{previous function call was here}}
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
) {
278 // expected-note@-1{{previous function call was here}}
282 // expected-note@-1{{0 is not equal to 1}}
283 // expected-note@-2{{Taking false branch}}
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) {
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'}}
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();