1 // RUN: %clang_analyze_cc1 -verify %s -Wno-null-dereference \
2 // RUN:   -analyzer-checker=core \
3 // RUN:   -analyzer-checker=unix.cstring \
4 // RUN:   -analyzer-checker=unix.Malloc \
5 // RUN:   -analyzer-checker=alpha.unix.cstring \
6 // RUN:   -analyzer-checker=debug.ExprInspection \
7 // RUN:   -analyzer-config eagerly-assume=false
8 //
9 // RUN: %clang_analyze_cc1 -verify %s -Wno-null-dereference -DUSE_BUILTINS \
10 // RUN:   -analyzer-checker=core \
11 // RUN:   -analyzer-checker=unix.cstring \
12 // RUN:   -analyzer-checker=unix.Malloc \
13 // RUN:   -analyzer-checker=alpha.unix.cstring \
14 // RUN:   -analyzer-checker=debug.ExprInspection \
15 // RUN:   -analyzer-config eagerly-assume=false
16 //
17 // RUN: %clang_analyze_cc1 -verify %s -Wno-null-dereference -DVARIANT \
18 // RUN:   -analyzer-checker=core \
19 // RUN:   -analyzer-checker=unix.cstring \
20 // RUN:   -analyzer-checker=unix.Malloc \
21 // RUN:   -analyzer-checker=alpha.unix.cstring \
22 // RUN:   -analyzer-checker=debug.ExprInspection \
23 // RUN:   -analyzer-config eagerly-assume=false
24 //
25 // RUN: %clang_analyze_cc1 -verify %s -Wno-null-dereference \
26 // RUN:   -DUSE_BUILTINS -DVARIANT \
27 // RUN:   -analyzer-checker=core \
28 // RUN:   -analyzer-checker=alpha.security.taint \
29 // RUN:   -analyzer-checker=unix.cstring \
30 // RUN:   -analyzer-checker=unix.Malloc \
31 // RUN:   -analyzer-checker=alpha.unix.cstring \
32 // RUN:   -analyzer-checker=debug.ExprInspection \
33 // RUN:   -analyzer-config eagerly-assume=false
34 //
35 // RUN: %clang_analyze_cc1 -verify %s -Wno-null-dereference \
36 // RUN:   -DSUPPRESS_OUT_OF_BOUND \
37 // RUN:   -analyzer-checker=core \
38 // RUN:   -analyzer-checker=unix.cstring \
39 // RUN:   -analyzer-checker=unix.Malloc \
40 // RUN:   -analyzer-checker=alpha.unix.cstring.BufferOverlap \
41 // RUN:   -analyzer-checker=alpha.unix.cstring.NotNullTerminated \
42 // RUN:   -analyzer-checker=debug.ExprInspection \
43 // RUN:   -analyzer-config eagerly-assume=false
44 
45 //===----------------------------------------------------------------------===
46 // Declarations
47 //===----------------------------------------------------------------------===
48 
49 // Some functions are so similar to each other that they follow the same code
50 // path, such as memcpy and __memcpy_chk, or memcmp and bcmp. If VARIANT is
51 // defined, make sure to use the variants instead to make sure they are still
52 // checked by the analyzer.
53 
54 // Some functions are implemented as builtins. These should be #defined as
55 // BUILTIN(f), which will prepend "__builtin_" if USE_BUILTINS is defined.
56 
57 // Functions that have variants and are also available as builtins should be
58 // declared carefully! See memcpy() for an example.
59 
60 #ifdef USE_BUILTINS
61 # define BUILTIN(f) __builtin_ ## f
62 #else /* USE_BUILTINS */
63 # define BUILTIN(f) f
64 #endif /* USE_BUILTINS */
65 
66 #define NULL 0
67 typedef typeof(sizeof(int)) size_t;
68 
69 void clang_analyzer_eval(int);
70 
71 int scanf(const char *restrict format, ...);
72 void *malloc(size_t);
73 void free(void *);
74 
75 //===----------------------------------------------------------------------===
76 // strlen()
77 //===----------------------------------------------------------------------===
78 
79 #define strlen BUILTIN(strlen)
80 size_t strlen(const char *s);
81 
82 void strlen_constant0() {
83   clang_analyzer_eval(strlen("123") == 3); // expected-warning{{TRUE}}
84 }
85 
86 void strlen_constant1() {
87   const char *a = "123";
88   clang_analyzer_eval(strlen(a) == 3); // expected-warning{{TRUE}}
89 }
90 
91 void strlen_constant2(char x) {
92   char a[] = "123";
93   clang_analyzer_eval(strlen(a) == 3); // expected-warning{{TRUE}}
94 
95   a[0] = x;
96   clang_analyzer_eval(strlen(a) == 3); // expected-warning{{UNKNOWN}}
97 }
98 
99 size_t strlen_null() {
100   return strlen(0); // expected-warning{{Null pointer argument in call to string length function}}
101 }
102 
103 size_t strlen_fn() {
104   return strlen((char*)&strlen_fn); // expected-warning{{Argument to string length function is the address of the function 'strlen_fn', which is not a null-terminated string}}
105 }
106 
107 size_t strlen_nonloc() {
108 label:
109   return strlen((char*)&&label); // expected-warning{{Argument to string length function is the address of the label 'label', which is not a null-terminated string}}
110 }
111 
112 void strlen_subregion() {
113   struct two_strings { char a[2], b[2]; };
114   extern void use_two_strings(struct two_strings *);
115 
116   struct two_strings z;
117   use_two_strings(&z);
118 
119   size_t a = strlen(z.a);
120   z.b[0] = 5;
121   size_t b = strlen(z.a);
122   if (a == 0)
123     clang_analyzer_eval(b == 0); // expected-warning{{TRUE}}
124 
125   use_two_strings(&z);
126 
127   size_t c = strlen(z.a);
128   if (a == 0)
129     clang_analyzer_eval(c == 0); // expected-warning{{UNKNOWN}}
130 }
131 
132 extern void use_string(char *);
133 void strlen_argument(char *x) {
134   size_t a = strlen(x);
135   size_t b = strlen(x);
136   if (a == 0)
137     clang_analyzer_eval(b == 0); // expected-warning{{TRUE}}
138 
139   use_string(x);
140 
141   size_t c = strlen(x);
142   if (a == 0)
143     clang_analyzer_eval(c == 0); // expected-warning{{UNKNOWN}}
144 }
145 
146 extern char global_str[];
147 void strlen_global() {
148   size_t a = strlen(global_str);
149   size_t b = strlen(global_str);
150   if (a == 0) {
151     clang_analyzer_eval(b == 0); // expected-warning{{TRUE}}
152     // Make sure clang_analyzer_eval does not invalidate globals.
153     clang_analyzer_eval(strlen(global_str) == 0); // expected-warning{{TRUE}}
154   }
155 
156   // Call a function with unknown effects, which should invalidate globals.
157   use_string(0);
158 
159   size_t c = strlen(global_str);
160   if (a == 0)
161     clang_analyzer_eval(c == 0); // expected-warning{{UNKNOWN}}
162 }
163 
164 void strlen_indirect(char *x) {
165   size_t a = strlen(x);
166   char *p = x;
167   char **p2 = &p;
168   size_t b = strlen(x);
169   if (a == 0)
170     clang_analyzer_eval(b == 0); // expected-warning{{TRUE}}
171 
172   extern void use_string_ptr(char*const*);
173   use_string_ptr(p2);
174 
175   size_t c = strlen(x);
176   if (a == 0)
177     clang_analyzer_eval(c == 0); // expected-warning{{UNKNOWN}}
178 }
179 
180 void strlen_indirect2(char *x) {
181   size_t a = strlen(x);
182   char *p = x;
183   char **p2 = &p;
184   extern void use_string_ptr2(char**);
185   use_string_ptr2(p2);
186 
187   size_t c = strlen(x);
188   if (a == 0)
189     clang_analyzer_eval(c == 0); // expected-warning{{UNKNOWN}}
190 }
191 
192 void strlen_liveness(const char *x) {
193   if (strlen(x) < 5)
194     return;
195   clang_analyzer_eval(strlen(x) < 5); // expected-warning{{FALSE}}
196 }
197 
198 
199 size_t strlenWrapper(const char *str) {
200   return strlen(str);
201 }
202 
203 extern void invalidate(char *s);
204 
205 void testStrlenCallee() {
206   char str[42];
207   invalidate(str);
208   size_t lenBefore = strlenWrapper(str);
209   invalidate(str);
210   size_t lenAfter = strlenWrapper(str);
211   clang_analyzer_eval(lenBefore == lenAfter); // expected-warning{{UNKNOWN}}
212 }
213 
214 
215 //===----------------------------------------------------------------------===
216 // strnlen()
217 //===----------------------------------------------------------------------===
218 
219 size_t strnlen(const char *s, size_t maxlen);
220 
221 void strnlen_constant0() {
222   clang_analyzer_eval(strnlen("123", 10) == 3); // expected-warning{{TRUE}}
223 }
224 
225 void strnlen_constant1() {
226   const char *a = "123";
227   clang_analyzer_eval(strnlen(a, 10) == 3); // expected-warning{{TRUE}}
228 }
229 
230 void strnlen_constant2(char x) {
231   char a[] = "123";
232   clang_analyzer_eval(strnlen(a, 10) == 3); // expected-warning{{TRUE}}
233   a[0] = x;
234   clang_analyzer_eval(strnlen(a, 10) == 3); // expected-warning{{UNKNOWN}}
235 }
236 
237 void strnlen_constant4() {
238   clang_analyzer_eval(strnlen("123456", 3) == 3); // expected-warning{{TRUE}}
239 }
240 
241 void strnlen_constant5() {
242   const char *a = "123456";
243   clang_analyzer_eval(strnlen(a, 3) == 3); // expected-warning{{TRUE}}
244 }
245 
246 void strnlen_constant6(char x) {
247   char a[] = "123456";
248   clang_analyzer_eval(strnlen(a, 3) == 3); // expected-warning{{TRUE}}
249   a[0] = x;
250   clang_analyzer_eval(strnlen(a, 3) == 3); // expected-warning{{UNKNOWN}}
251 }
252 
253 size_t strnlen_null() {
254   return strnlen(0, 3); // expected-warning{{Null pointer argument in call to string length function}}
255 }
256 
257 size_t strnlen_fn() {
258   return strnlen((char*)&strlen_fn, 3); // expected-warning{{Argument to string length function is the address of the function 'strlen_fn', which is not a null-terminated string}}
259 }
260 
261 size_t strnlen_nonloc() {
262 label:
263   return strnlen((char*)&&label, 3); // expected-warning{{Argument to string length function is the address of the label 'label', which is not a null-terminated string}}
264 }
265 
266 void strnlen_zero() {
267   clang_analyzer_eval(strnlen("abc", 0) == 0); // expected-warning{{TRUE}}
268   clang_analyzer_eval(strnlen(NULL, 0) == 0); // expected-warning{{TRUE}}
269 }
270 
271 size_t strnlen_compound_literal() {
272   // This used to crash because we don't model the string lengths of
273   // compound literals.
274   return strnlen((char[]) { 'a', 'b', 0 }, 1);
275 }
276 
277 size_t strnlen_unknown_limit(float f) {
278   // This used to crash because we don't model the integer values of floats.
279   return strnlen("abc", (int)f);
280 }
281 
282 void strnlen_is_not_strlen(char *x) {
283   clang_analyzer_eval(strnlen(x, 10) == strlen(x)); // expected-warning{{UNKNOWN}}
284 }
285 
286 void strnlen_at_limit(char *x) {
287   size_t len = strnlen(x, 10);
288   clang_analyzer_eval(len <= 10); // expected-warning{{TRUE}}
289   clang_analyzer_eval(len == 10); // expected-warning{{UNKNOWN}}
290   clang_analyzer_eval(len < 10); // expected-warning{{UNKNOWN}}
291 }
292 
293 void strnlen_at_actual(size_t limit) {
294   size_t len = strnlen("abc", limit);
295   clang_analyzer_eval(len <= 3); // expected-warning{{TRUE}}
296   // This is due to eager assertion in strnlen.
297   if (limit == 0) {
298     clang_analyzer_eval(len == 0); // expected-warning{{TRUE}}
299   } else {
300     clang_analyzer_eval(len == 3); // expected-warning{{UNKNOWN}}
301     clang_analyzer_eval(len < 3); // expected-warning{{UNKNOWN}}
302   }
303 }
304 
305 //===----------------------------------------------------------------------===
306 // strcpy()
307 //===----------------------------------------------------------------------===
308 
309 #ifdef VARIANT
310 
311 #define __strcpy_chk BUILTIN(__strcpy_chk)
312 char *__strcpy_chk(char *restrict s1, const char *restrict s2, size_t destlen);
313 
314 #define strcpy(a,b) __strcpy_chk(a,b,(size_t)-1)
315 
316 #else /* VARIANT */
317 
318 #define strcpy BUILTIN(strcpy)
319 char *strcpy(char *restrict s1, const char *restrict s2);
320 
321 #endif /* VARIANT */
322 
323 
324 void strcpy_null_dst(char *x) {
325   strcpy(NULL, x); // expected-warning{{Null pointer argument in call to string copy function}}
326 }
327 
328 void strcpy_null_src(char *x) {
329   strcpy(x, NULL); // expected-warning{{Null pointer argument in call to string copy function}}
330 }
331 
332 void strcpy_fn(char *x) {
333   strcpy(x, (char*)&strcpy_fn); // expected-warning{{Argument to string copy function is the address of the function 'strcpy_fn', which is not a null-terminated string}}
334 }
335 
336 void strcpy_fn_const(char *x) {
337   strcpy(x, (const char*)&strcpy_fn); // expected-warning{{Argument to string copy function is the address of the function 'strcpy_fn', which is not a null-terminated string}}
338 }
339 
340 extern int globalInt;
341 void strcpy_effects(char *x, char *y) {
342   char a = x[0];
343   if (globalInt != 42)
344     return;
345 
346   clang_analyzer_eval(strcpy(x, y) == x); // expected-warning{{TRUE}}
347   clang_analyzer_eval(strlen(x) == strlen(y)); // expected-warning{{TRUE}}
348   clang_analyzer_eval(a == x[0]); // expected-warning{{UNKNOWN}}
349   clang_analyzer_eval(globalInt == 42); // expected-warning{{TRUE}}
350 }
351 
352 #ifndef SUPPRESS_OUT_OF_BOUND
353 void strcpy_overflow(char *y) {
354   char x[4];
355   if (strlen(y) == 4)
356     strcpy(x, y); // expected-warning{{String copy function overflows destination buffer}}
357 }
358 #endif
359 
360 void strcpy_no_overflow(char *y) {
361   char x[4];
362   if (strlen(y) == 3)
363     strcpy(x, y); // no-warning
364 }
365 
366 //===----------------------------------------------------------------------===
367 // stpcpy()
368 //===----------------------------------------------------------------------===
369 
370 #ifdef VARIANT
371 
372 #define __stpcpy_chk BUILTIN(__stpcpy_chk)
373 char *__stpcpy_chk(char *restrict s1, const char *restrict s2, size_t destlen);
374 
375 #define stpcpy(a,b) __stpcpy_chk(a,b,(size_t)-1)
376 
377 #else /* VARIANT */
378 
379 #define stpcpy BUILTIN(stpcpy)
380 char *stpcpy(char *restrict s1, const char *restrict s2);
381 
382 #endif /* VARIANT */
383 
384 
385 void stpcpy_effect(char *x, char *y) {
386   char a = x[0];
387 
388   clang_analyzer_eval(stpcpy(x, y) == &x[strlen(y)]); // expected-warning{{TRUE}}
389   clang_analyzer_eval(strlen(x) == strlen(y)); // expected-warning{{TRUE}}
390   clang_analyzer_eval(a == x[0]); // expected-warning{{UNKNOWN}}
391 }
392 
393 #ifndef SUPPRESS_OUT_OF_BOUND
394 void stpcpy_overflow(char *y) {
395   char x[4];
396   if (strlen(y) == 4)
397     stpcpy(x, y); // expected-warning{{String copy function overflows destination buffer}}
398 }
399 #endif
400 
401 void stpcpy_no_overflow(char *y) {
402   char x[4];
403   if (strlen(y) == 3)
404     stpcpy(x, y); // no-warning
405 }
406 
407 //===----------------------------------------------------------------------===
408 // strcat()
409 //===----------------------------------------------------------------------===
410 
411 #ifdef VARIANT
412 
413 #define __strcat_chk BUILTIN(__strcat_chk)
414 char *__strcat_chk(char *restrict s1, const char *restrict s2, size_t destlen);
415 
416 #define strcat(a,b) __strcat_chk(a,b,(size_t)-1)
417 
418 #else /* VARIANT */
419 
420 #define strcat BUILTIN(strcat)
421 char *strcat(char *restrict s1, const char *restrict s2);
422 
423 #endif /* VARIANT */
424 
425 
426 void strcat_null_dst(char *x) {
427   strcat(NULL, x); // expected-warning{{Null pointer argument in call to string copy function}}
428 }
429 
430 void strcat_null_src(char *x) {
431   strcat(x, NULL); // expected-warning{{Null pointer argument in call to string copy function}}
432 }
433 
434 void strcat_fn(char *x) {
435   strcat(x, (char*)&strcat_fn); // expected-warning{{Argument to string copy function is the address of the function 'strcat_fn', which is not a null-terminated string}}
436 }
437 
438 void strcat_effects(char *y) {
439   char x[8] = "123";
440   size_t orig_len = strlen(x);
441   char a = x[0];
442 
443   if (strlen(y) != 4)
444     return;
445 
446   clang_analyzer_eval(strcat(x, y) == x); // expected-warning{{TRUE}}
447   clang_analyzer_eval((int)strlen(x) == (orig_len + strlen(y))); // expected-warning{{TRUE}}
448 }
449 
450 #ifndef SUPPRESS_OUT_OF_BOUND
451 void strcat_overflow_0(char *y) {
452   char x[4] = "12";
453   if (strlen(y) == 4)
454     strcat(x, y); // expected-warning{{String copy function overflows destination buffer}}
455 }
456 
457 void strcat_overflow_1(char *y) {
458   char x[4] = "12";
459   if (strlen(y) == 3)
460     strcat(x, y); // expected-warning{{String copy function overflows destination buffer}}
461 }
462 
463 void strcat_overflow_2(char *y) {
464   char x[4] = "12";
465   if (strlen(y) == 2)
466     strcat(x, y); // expected-warning{{String copy function overflows destination buffer}}
467 }
468 #endif
469 
470 void strcat_no_overflow(char *y) {
471   char x[5] = "12";
472   if (strlen(y) == 2)
473     strcat(x, y); // no-warning
474 }
475 
476 void strcat_symbolic_dst_length(char *dst) {
477 	strcat(dst, "1234");
478   clang_analyzer_eval(strlen(dst) >= 4); // expected-warning{{TRUE}}
479 }
480 
481 void strcat_symbolic_dst_length_taint(char *dst) {
482   scanf("%s", dst); // Taint data.
483   strcat(dst, "1234");
484   clang_analyzer_eval(strlen(dst) >= 4); // expected-warning{{TRUE}}
485 }
486 
487 void strcat_unknown_src_length(char *src, int offset) {
488 	char dst[8] = "1234";
489 	strcat(dst, &src[offset]);
490   clang_analyzer_eval(strlen(dst) >= 4); // expected-warning{{TRUE}}
491 }
492 
493 // There is no strcat_unknown_dst_length because if we can't get a symbolic
494 // length for the "before" strlen, we won't be able to set one for "after".
495 
496 void strcat_too_big(char *dst, char *src) {
497   // We assume this can never actually happen, so we don't get a warning.
498 	if (strlen(dst) != (((size_t)0) - 2))
499 		return;
500 	if (strlen(src) != 2)
501 		return;
502 	strcat(dst, src);
503 }
504 
505 
506 //===----------------------------------------------------------------------===
507 // strncpy()
508 //===----------------------------------------------------------------------===
509 
510 #ifdef VARIANT
511 
512 #define __strncpy_chk BUILTIN(__strncpy_chk)
513 char *__strncpy_chk(char *restrict s1, const char *restrict s2, size_t n, size_t destlen);
514 
515 #define strncpy(a,b,n) __strncpy_chk(a,b,n,(size_t)-1)
516 
517 #else /* VARIANT */
518 
519 #define strncpy BUILTIN(strncpy)
520 char *strncpy(char *restrict s1, const char *restrict s2, size_t n);
521 
522 #endif /* VARIANT */
523 
524 
525 void strncpy_null_dst(char *x) {
526   strncpy(NULL, x, 5); // expected-warning{{Null pointer argument in call to string copy function}}
527 }
528 
529 void strncpy_null_src(char *x) {
530   strncpy(x, NULL, 5); // expected-warning{{Null pointer argument in call to string copy function}}
531 }
532 
533 void strncpy_fn(char *x) {
534   strncpy(x, (char*)&strcpy_fn, 5); // expected-warning{{Argument to string copy function is the address of the function 'strcpy_fn', which is not a null-terminated string}}
535 }
536 
537 void strncpy_effects(char *x, char *y) {
538   char a = x[0];
539 
540   clang_analyzer_eval(strncpy(x, y, 5) == x); // expected-warning{{TRUE}}
541   clang_analyzer_eval(strlen(x) == strlen(y)); // expected-warning{{UNKNOWN}}
542   clang_analyzer_eval(a == x[0]); // expected-warning{{UNKNOWN}}
543 }
544 
545 #ifndef SUPPRESS_OUT_OF_BOUND
546 // Enabling the malloc checker enables some of the buffer-checking portions
547 // of the C-string checker.
548 void cstringchecker_bounds_nocrash() {
549   char *p = malloc(2);
550   strncpy(p, "AAA", sizeof("AAA")); // expected-warning {{Size argument is greater than the length of the destination buffer}}
551   free(p);
552 }
553 
554 void strncpy_overflow(char *y) {
555   char x[4];
556   if (strlen(y) == 4)
557     strncpy(x, y, 5); // expected-warning{{Size argument is greater than the length of the destination buffer}}
558 #ifndef VARIANT
559   // expected-warning@-2{{size argument is too large; destination buffer has size 4, but size argument is 5}}
560 #endif
561 }
562 
563 void strncpy_no_overflow(char *y) {
564   char x[4];
565   if (strlen(y) == 3)
566     strncpy(x, y, 5); // expected-warning{{Size argument is greater than the length of the destination buffer}}
567 #ifndef VARIANT
568   // expected-warning@-2{{size argument is too large; destination buffer has size 4, but size argument is 5}}
569 #endif
570 }
571 
572 void strncpy_no_overflow2(char *y, int n) {
573 	if (n <= 4)
574 		return;
575 
576   char x[4];
577   if (strlen(y) == 3)
578     strncpy(x, y, n); // expected-warning{{Size argument is greater than the length of the destination buffer}}
579 }
580 #endif
581 
582 void strncpy_truncate(char *y) {
583   char x[4];
584   if (strlen(y) == 4)
585     strncpy(x, y, 3); // no-warning
586 }
587 
588 void strncpy_no_truncate(char *y) {
589   char x[4];
590   if (strlen(y) == 3)
591     strncpy(x, y, 3); // no-warning
592 }
593 
594 void strncpy_exactly_matching_buffer(char *y) {
595 	char x[4];
596 	strncpy(x, y, 4); // no-warning
597 
598 	// strncpy does not null-terminate, so we have no idea what the strlen is
599 	// after this.
600   clang_analyzer_eval(strlen(x) > 4); // expected-warning{{UNKNOWN}}
601 }
602 
603 void strncpy_zero(char *src) {
604   char dst[] = "123";
605   strncpy(dst, src, 0); // no-warning
606 }
607 
608 void strncpy_empty() {
609   char dst[] = "123";
610   char src[] = "";
611   strncpy(dst, src, 4); // no-warning
612 }
613 
614 //===----------------------------------------------------------------------===
615 // strncat()
616 //===----------------------------------------------------------------------===
617 
618 #ifdef VARIANT
619 
620 #define __strncat_chk BUILTIN(__strncat_chk)
621 char *__strncat_chk(char *restrict s1, const char *restrict s2, size_t n, size_t destlen);
622 
623 #define strncat(a,b,c) __strncat_chk(a,b,c, (size_t)-1)
624 
625 #else /* VARIANT */
626 
627 #define strncat BUILTIN(strncat)
628 char *strncat(char *restrict s1, const char *restrict s2, size_t n);
629 
630 #endif /* VARIANT */
631 
632 
633 void strncat_null_dst(char *x) {
634   strncat(NULL, x, 4); // expected-warning{{Null pointer argument in call to string copy function}}
635 }
636 
637 void strncat_null_src(char *x) {
638   strncat(x, NULL, 4); // expected-warning{{Null pointer argument in call to string copy function}}
639 }
640 
641 void strncat_fn(char *x) {
642   strncat(x, (char*)&strncat_fn, 4); // expected-warning{{Argument to string copy function is the address of the function 'strncat_fn', which is not a null-terminated string}}
643 }
644 
645 void strncat_effects(char *y) {
646   char x[8] = "123";
647   size_t orig_len = strlen(x);
648   char a = x[0];
649 
650   if (strlen(y) != 4)
651     return;
652 
653   clang_analyzer_eval(strncat(x, y, strlen(y)) == x); // expected-warning{{TRUE}}
654   clang_analyzer_eval(strlen(x) == (orig_len + strlen(y))); // expected-warning{{TRUE}}
655 }
656 
657 #ifndef SUPPRESS_OUT_OF_BOUND
658 void strncat_overflow_0(char *y) {
659   char x[4] = "12";
660   if (strlen(y) == 4)
661     strncat(x, y, strlen(y)); // expected-warning{{Size argument is greater than the free space in the destination buffer}}
662 }
663 
664 void strncat_overflow_1(char *y) {
665   char x[4] = "12";
666   if (strlen(y) == 3)
667     strncat(x, y, strlen(y)); // expected-warning{{Size argument is greater than the free space in the destination buffer}}
668 }
669 
670 void strncat_overflow_2(char *y) {
671   char x[4] = "12";
672   if (strlen(y) == 2)
673     strncat(x, y, strlen(y)); // expected-warning{{Size argument is greater than the free space in the destination buffer}}
674 }
675 
676 void strncat_overflow_3(char *y) {
677   char x[4] = "12";
678   if (strlen(y) == 4)
679     strncat(x, y, 2); // expected-warning{{Size argument is greater than the free space in the destination buffer}}
680 }
681 #endif
682 
683 void strncat_no_overflow_1(char *y) {
684   char x[5] = "12";
685   if (strlen(y) == 2)
686     strncat(x, y, strlen(y)); // no-warning
687 }
688 
689 void strncat_no_overflow_2(char *y) {
690   char x[4] = "12";
691   if (strlen(y) == 4)
692     strncat(x, y, 1); // no-warning
693 }
694 
695 void strncat_symbolic_dst_length(char *dst) {
696   strncat(dst, "1234", 5);
697   clang_analyzer_eval(strlen(dst) >= 4); // expected-warning{{TRUE}}
698 }
699 
700 #ifndef SUPPRESS_OUT_OF_BOUND
701 void strncat_symbolic_src_length(char *src) {
702   char dst[8] = "1234";
703   strncat(dst, src, 3);
704   clang_analyzer_eval(strlen(dst) >= 4); // expected-warning{{TRUE}}
705 
706   char dst2[8] = "1234";
707   strncat(dst2, src, 4); // expected-warning{{Size argument is greater than the free space in the destination buffer}}
708 }
709 
710 void strncat_unknown_src_length(char *src, int offset) {
711   char dst[8] = "1234";
712   strncat(dst, &src[offset], 3);
713   clang_analyzer_eval(strlen(dst) >= 4); // expected-warning{{TRUE}}
714 
715   char dst2[8] = "1234";
716   strncat(dst2, &src[offset], 4); // expected-warning{{Size argument is greater than the free space in the destination buffer}}
717 }
718 #endif
719 
720 // There is no strncat_unknown_dst_length because if we can't get a symbolic
721 // length for the "before" strlen, we won't be able to set one for "after".
722 
723 void strncat_symbolic_limit(unsigned limit) {
724   char dst[6] = "1234";
725   char src[] = "567";
726   strncat(dst, src, limit); // no-warning
727 
728   clang_analyzer_eval(strlen(dst) >= 4); // expected-warning{{TRUE}}
729   clang_analyzer_eval(strlen(dst) == 4); // expected-warning{{UNKNOWN}}
730 }
731 
732 void strncat_unknown_limit(float limit) {
733   char dst[6] = "1234";
734   char src[] = "567";
735   strncat(dst, src, (size_t)limit); // no-warning
736 
737   clang_analyzer_eval(strlen(dst) >= 4); // expected-warning{{TRUE}}
738   clang_analyzer_eval(strlen(dst) == 4); // expected-warning{{UNKNOWN}}
739 }
740 
741 void strncat_too_big(char *dst, char *src) {
742   // We assume this will never actually happen, so we don't get a warning.
743   if (strlen(dst) != (((size_t)0) - 2))
744     return;
745   if (strlen(src) != 2)
746     return;
747   strncat(dst, src, 2);
748 }
749 
750 void strncat_zero(char *src) {
751   char dst[] = "123";
752   strncat(dst, src, 0); // no-warning
753 }
754 
755 void strncat_empty() {
756   char dst[8] = "123";
757   char src[] = "";
758   strncat(dst, src, 4); // no-warning
759 }
760 
761 //===----------------------------------------------------------------------===
762 // strcmp()
763 //===----------------------------------------------------------------------===
764 
765 #define strcmp BUILTIN(strcmp)
766 int strcmp(const char * s1, const char * s2);
767 
768 void strcmp_check_modelling() {
769   char *x = "aa";
770   char *y = "a";
771   clang_analyzer_eval(strcmp(x, y) > 0); // expected-warning{{TRUE}}
772   clang_analyzer_eval(strcmp(x, y) <= 0); // expected-warning{{FALSE}}
773   clang_analyzer_eval(strcmp(x, y) > 1); // expected-warning{{UNKNOWN}}
774 
775   clang_analyzer_eval(strcmp(y, x) < 0); // expected-warning{{TRUE}}
776   clang_analyzer_eval(strcmp(y, x) >= 0); // expected-warning{{FALSE}}
777   clang_analyzer_eval(strcmp(y, x) < -1); // expected-warning{{UNKNOWN}}
778 }
779 
780 void strcmp_constant0() {
781   clang_analyzer_eval(strcmp("123", "123") == 0); // expected-warning{{TRUE}}
782 }
783 
784 void strcmp_constant_and_var_0() {
785   char *x = "123";
786   clang_analyzer_eval(strcmp(x, "123") == 0); // expected-warning{{TRUE}}
787 }
788 
789 void strcmp_constant_and_var_1() {
790   char *x = "123";
791   clang_analyzer_eval(strcmp("123", x) == 0); // expected-warning{{TRUE}}
792 }
793 
794 void strcmp_0() {
795   char *x = "123";
796   char *y = "123";
797   clang_analyzer_eval(strcmp(x, y) == 0); // expected-warning{{TRUE}}
798 }
799 
800 void strcmp_1() {
801   char *x = "234";
802   char *y = "123";
803   clang_analyzer_eval(strcmp(x, y) > 0); // expected-warning{{TRUE}}
804 }
805 
806 void strcmp_2() {
807   char *x = "123";
808   char *y = "234";
809   clang_analyzer_eval(strcmp(x, y) < 0); // expected-warning{{TRUE}}
810 }
811 
812 void strcmp_null_0() {
813   char *x = NULL;
814   char *y = "123";
815   strcmp(x, y); // expected-warning{{Null pointer argument in call to string comparison function}}
816 }
817 
818 void strcmp_null_1() {
819   char *x = "123";
820   char *y = NULL;
821   strcmp(x, y); // expected-warning{{Null pointer argument in call to string comparison function}}
822 }
823 
824 void strcmp_diff_length_0() {
825   char *x = "12345";
826   char *y = "234";
827   clang_analyzer_eval(strcmp(x, y) < 0); // expected-warning{{TRUE}}
828 }
829 
830 void strcmp_diff_length_1() {
831   char *x = "123";
832   char *y = "23456";
833   clang_analyzer_eval(strcmp(x, y) < 0); // expected-warning{{TRUE}}
834 }
835 
836 void strcmp_diff_length_2() {
837   char *x = "12345";
838   char *y = "123";
839   clang_analyzer_eval(strcmp(x, y) > 0); // expected-warning{{TRUE}}
840 }
841 
842 void strcmp_diff_length_3() {
843   char *x = "123";
844   char *y = "12345";
845   clang_analyzer_eval(strcmp(x, y) < 0); // expected-warning{{TRUE}}
846 }
847 
848 void strcmp_embedded_null () {
849 	clang_analyzer_eval(strcmp("\0z", "\0y") == 0); // expected-warning{{TRUE}}
850 }
851 
852 void strcmp_unknown_arg (char *unknown) {
853 	clang_analyzer_eval(strcmp(unknown, unknown) == 0); // expected-warning{{TRUE}}
854 }
855 
856 union argument {
857    char *f;
858 };
859 
860 void function_pointer_cast_helper(char **a) {
861   strcmp("Hi", *a); // PR24951 crash
862 }
863 
864 void strcmp_union_function_pointer_cast(union argument a) {
865   void (*fPtr)(union argument *) = (void (*)(union argument *))function_pointer_cast_helper;
866 
867   fPtr(&a);
868 }
869 
870 //===----------------------------------------------------------------------===
871 // strncmp()
872 //===----------------------------------------------------------------------===
873 
874 #define strncmp BUILTIN(strncmp)
875 int strncmp(const char *s1, const char *s2, size_t n);
876 
877 void strncmp_check_modelling() {
878   char *x = "aa";
879   char *y = "a";
880   clang_analyzer_eval(strncmp(x, y, 2) > 0); // expected-warning{{TRUE}}
881   clang_analyzer_eval(strncmp(x, y, 2) <= 0); // expected-warning{{FALSE}}
882   clang_analyzer_eval(strncmp(x, y, 2) > 1); // expected-warning{{UNKNOWN}}
883 
884   clang_analyzer_eval(strncmp(y, x, 2) < 0); // expected-warning{{TRUE}}
885   clang_analyzer_eval(strncmp(y, x, 2) >= 0); // expected-warning{{FALSE}}
886   clang_analyzer_eval(strncmp(y, x, 2) < -1); // expected-warning{{UNKNOWN}}
887 }
888 
889 void strncmp_constant0() {
890   clang_analyzer_eval(strncmp("123", "123", 3) == 0); // expected-warning{{TRUE}}
891 }
892 
893 void strncmp_constant_and_var_0() {
894   char *x = "123";
895   clang_analyzer_eval(strncmp(x, "123", 3) == 0); // expected-warning{{TRUE}}
896 }
897 
898 void strncmp_constant_and_var_1() {
899   char *x = "123";
900   clang_analyzer_eval(strncmp("123", x, 3) == 0); // expected-warning{{TRUE}}
901 }
902 
903 void strncmp_0() {
904   char *x = "123";
905   char *y = "123";
906   clang_analyzer_eval(strncmp(x, y, 3) == 0); // expected-warning{{TRUE}}
907 }
908 
909 void strncmp_1() {
910   char *x = "234";
911   char *y = "123";
912   clang_analyzer_eval(strncmp(x, y, 3) > 0); // expected-warning{{TRUE}}
913 }
914 
915 void strncmp_2() {
916   char *x = "123";
917   char *y = "234";
918   clang_analyzer_eval(strncmp(x, y, 3) < 0); // expected-warning{{TRUE}}
919 }
920 
921 void strncmp_null_0() {
922   char *x = NULL;
923   char *y = "123";
924   strncmp(x, y, 3); // expected-warning{{Null pointer argument in call to string comparison function}}
925 }
926 
927 void strncmp_null_1() {
928   char *x = "123";
929   char *y = NULL;
930   strncmp(x, y, 3); // expected-warning{{Null pointer argument in call to string comparison function}}
931 }
932 
933 void strncmp_diff_length_0() {
934   char *x = "12345";
935   char *y = "234";
936   clang_analyzer_eval(strncmp(x, y, 5) < 0); // expected-warning{{TRUE}}
937 }
938 
939 void strncmp_diff_length_1() {
940   char *x = "123";
941   char *y = "23456";
942   clang_analyzer_eval(strncmp(x, y, 5) < 0); // expected-warning{{TRUE}}
943 }
944 
945 void strncmp_diff_length_2() {
946   char *x = "12345";
947   char *y = "123";
948   clang_analyzer_eval(strncmp(x, y, 5) > 0); // expected-warning{{TRUE}}
949 }
950 
951 void strncmp_diff_length_3() {
952   char *x = "123";
953   char *y = "12345";
954   clang_analyzer_eval(strncmp(x, y, 5) < 0); // expected-warning{{TRUE}}
955 }
956 
957 void strncmp_diff_length_4() {
958   char *x = "123";
959   char *y = "12345";
960   clang_analyzer_eval(strncmp(x, y, 3) == 0); // expected-warning{{TRUE}}
961 }
962 
963 void strncmp_diff_length_5() {
964   char *x = "012";
965   char *y = "12345";
966   clang_analyzer_eval(strncmp(x, y, 3) < 0); // expected-warning{{TRUE}}
967 }
968 
969 void strncmp_diff_length_6() {
970   char *x = "234";
971   char *y = "12345";
972   clang_analyzer_eval(strncmp(x, y, 3) > 0); // expected-warning{{TRUE}}
973 }
974 
975 void strncmp_embedded_null () {
976 	clang_analyzer_eval(strncmp("ab\0zz", "ab\0yy", 4) == 0); // expected-warning{{TRUE}}
977 }
978 
979 //===----------------------------------------------------------------------===
980 // strcasecmp()
981 //===----------------------------------------------------------------------===
982 
983 #define strcasecmp BUILTIN(strcasecmp)
984 int strcasecmp(const char *s1, const char *s2);
985 
986 void strcasecmp_check_modelling() {
987   char *x = "aa";
988   char *y = "a";
989   clang_analyzer_eval(strcasecmp(x, y) > 0); // expected-warning{{TRUE}}
990   clang_analyzer_eval(strcasecmp(x, y) <= 0); // expected-warning{{FALSE}}
991   clang_analyzer_eval(strcasecmp(x, y) > 1); // expected-warning{{UNKNOWN}}
992 
993   clang_analyzer_eval(strcasecmp(y, x) < 0); // expected-warning{{TRUE}}
994   clang_analyzer_eval(strcasecmp(y, x) >= 0); // expected-warning{{FALSE}}
995   clang_analyzer_eval(strcasecmp(y, x) < -1); // expected-warning{{UNKNOWN}}
996 }
997 
998 void strcasecmp_constant0() {
999   clang_analyzer_eval(strcasecmp("abc", "Abc") == 0); // expected-warning{{TRUE}}
1000 }
1001 
1002 void strcasecmp_constant_and_var_0() {
1003   char *x = "abc";
1004   clang_analyzer_eval(strcasecmp(x, "Abc") == 0); // expected-warning{{TRUE}}
1005 }
1006 
1007 void strcasecmp_constant_and_var_1() {
1008   char *x = "abc";
1009   clang_analyzer_eval(strcasecmp("Abc", x) == 0); // expected-warning{{TRUE}}
1010 }
1011 
1012 void strcasecmp_0() {
1013   char *x = "abc";
1014   char *y = "Abc";
1015   clang_analyzer_eval(strcasecmp(x, y) == 0); // expected-warning{{TRUE}}
1016 }
1017 
1018 void strcasecmp_1() {
1019   char *x = "Bcd";
1020   char *y = "abc";
1021   clang_analyzer_eval(strcasecmp(x, y) > 0); // expected-warning{{TRUE}}
1022 }
1023 
1024 void strcasecmp_2() {
1025   char *x = "abc";
1026   char *y = "Bcd";
1027   clang_analyzer_eval(strcasecmp(x, y) < 0); // expected-warning{{TRUE}}
1028 }
1029 
1030 void strcasecmp_null_0() {
1031   char *x = NULL;
1032   char *y = "123";
1033   strcasecmp(x, y); // expected-warning{{Null pointer argument in call to string comparison function}}
1034 }
1035 
1036 void strcasecmp_null_1() {
1037   char *x = "123";
1038   char *y = NULL;
1039   strcasecmp(x, y); // expected-warning{{Null pointer argument in call to string comparison function}}
1040 }
1041 
1042 void strcasecmp_diff_length_0() {
1043   char *x = "abcde";
1044   char *y = "aBd";
1045   clang_analyzer_eval(strcasecmp(x, y) < 0); // expected-warning{{TRUE}}
1046 }
1047 
1048 void strcasecmp_diff_length_1() {
1049   char *x = "abc";
1050   char *y = "aBdef";
1051   clang_analyzer_eval(strcasecmp(x, y) < 0); // expected-warning{{TRUE}}
1052 }
1053 
1054 void strcasecmp_diff_length_2() {
1055   char *x = "aBcDe";
1056   char *y = "abc";
1057   clang_analyzer_eval(strcasecmp(x, y) > 0); // expected-warning{{TRUE}}
1058 }
1059 
1060 void strcasecmp_diff_length_3() {
1061   char *x = "aBc";
1062   char *y = "abcde";
1063   clang_analyzer_eval(strcasecmp(x, y) < 0); // expected-warning{{TRUE}}
1064 }
1065 
1066 void strcasecmp_embedded_null () {
1067 	clang_analyzer_eval(strcasecmp("ab\0zz", "ab\0yy") == 0); // expected-warning{{TRUE}}
1068 }
1069 
1070 //===----------------------------------------------------------------------===
1071 // strncasecmp()
1072 //===----------------------------------------------------------------------===
1073 
1074 #define strncasecmp BUILTIN(strncasecmp)
1075 int strncasecmp(const char *s1, const char *s2, size_t n);
1076 
1077 void strncasecmp_check_modelling() {
1078   char *x = "aa";
1079   char *y = "a";
1080   clang_analyzer_eval(strncasecmp(x, y, 2) > 0); // expected-warning{{TRUE}}
1081   clang_analyzer_eval(strncasecmp(x, y, 2) <= 0); // expected-warning{{FALSE}}
1082   clang_analyzer_eval(strncasecmp(x, y, 2) > 1); // expected-warning{{UNKNOWN}}
1083 
1084   clang_analyzer_eval(strncasecmp(y, x, 2) < 0); // expected-warning{{TRUE}}
1085   clang_analyzer_eval(strncasecmp(y, x, 2) >= 0); // expected-warning{{FALSE}}
1086   clang_analyzer_eval(strncasecmp(y, x, 2) < -1); // expected-warning{{UNKNOWN}}
1087 }
1088 
1089 void strncasecmp_constant0() {
1090   clang_analyzer_eval(strncasecmp("abc", "Abc", 3) == 0); // expected-warning{{TRUE}}
1091 }
1092 
1093 void strncasecmp_constant_and_var_0() {
1094   char *x = "abc";
1095   clang_analyzer_eval(strncasecmp(x, "Abc", 3) == 0); // expected-warning{{TRUE}}
1096 }
1097 
1098 void strncasecmp_constant_and_var_1() {
1099   char *x = "abc";
1100   clang_analyzer_eval(strncasecmp("Abc", x, 3) == 0); // expected-warning{{TRUE}}
1101 }
1102 
1103 void strncasecmp_0() {
1104   char *x = "abc";
1105   char *y = "Abc";
1106   clang_analyzer_eval(strncasecmp(x, y, 3) == 0); // expected-warning{{TRUE}}
1107 }
1108 
1109 void strncasecmp_1() {
1110   char *x = "Bcd";
1111   char *y = "abc";
1112   clang_analyzer_eval(strncasecmp(x, y, 3) > 0); // expected-warning{{TRUE}}
1113 }
1114 
1115 void strncasecmp_2() {
1116   char *x = "abc";
1117   char *y = "Bcd";
1118   clang_analyzer_eval(strncasecmp(x, y, 3) < 0); // expected-warning{{TRUE}}
1119 }
1120 
1121 void strncasecmp_null_0() {
1122   char *x = NULL;
1123   char *y = "123";
1124   strncasecmp(x, y, 3); // expected-warning{{Null pointer argument in call to string comparison function}}
1125 }
1126 
1127 void strncasecmp_null_1() {
1128   char *x = "123";
1129   char *y = NULL;
1130   strncasecmp(x, y, 3); // expected-warning{{Null pointer argument in call to string comparison function}}
1131 }
1132 
1133 void strncasecmp_diff_length_0() {
1134   char *x = "abcde";
1135   char *y = "aBd";
1136   clang_analyzer_eval(strncasecmp(x, y, 5) < 0); // expected-warning{{TRUE}}
1137 }
1138 
1139 void strncasecmp_diff_length_1() {
1140   char *x = "abc";
1141   char *y = "aBdef";
1142   clang_analyzer_eval(strncasecmp(x, y, 5) < 0); // expected-warning{{TRUE}}
1143 }
1144 
1145 void strncasecmp_diff_length_2() {
1146   char *x = "aBcDe";
1147   char *y = "abc";
1148   clang_analyzer_eval(strncasecmp(x, y, 5) > 0); // expected-warning{{TRUE}}
1149 }
1150 
1151 void strncasecmp_diff_length_3() {
1152   char *x = "aBc";
1153   char *y = "abcde";
1154   clang_analyzer_eval(strncasecmp(x, y, 5) < 0); // expected-warning{{TRUE}}
1155 }
1156 
1157 void strncasecmp_diff_length_4() {
1158   char *x = "abcde";
1159   char *y = "aBc";
1160   clang_analyzer_eval(strncasecmp(x, y, 3) == 0); // expected-warning{{TRUE}}
1161 }
1162 
1163 void strncasecmp_diff_length_5() {
1164   char *x = "abcde";
1165   char *y = "aBd";
1166   clang_analyzer_eval(strncasecmp(x, y, 3) < 0); // expected-warning{{TRUE}}
1167 }
1168 
1169 void strncasecmp_diff_length_6() {
1170   char *x = "aBDe";
1171   char *y = "abc";
1172   clang_analyzer_eval(strncasecmp(x, y, 3) > 0); // expected-warning{{TRUE}}
1173 }
1174 
1175 void strncasecmp_embedded_null () {
1176 	clang_analyzer_eval(strncasecmp("ab\0zz", "ab\0yy", 4) == 0); // expected-warning{{TRUE}}
1177 }
1178 
1179 //===----------------------------------------------------------------------===
1180 // strsep()
1181 //===----------------------------------------------------------------------===
1182 
1183 char *strsep(char **stringp, const char *delim);
1184 
1185 void strsep_null_delim(char *s) {
1186   strsep(&s, NULL); // expected-warning{{Null pointer argument in call to strsep()}}
1187 }
1188 
1189 void strsep_null_search() {
1190   strsep(NULL, ""); // expected-warning{{Null pointer argument in call to strsep()}}
1191 }
1192 
1193 void strsep_return_original_pointer(char *s) {
1194   char *original = s;
1195   char *result = strsep(&s, ""); // no-warning
1196   clang_analyzer_eval(original == result); // expected-warning{{TRUE}}
1197 }
1198 
1199 void strsep_null_string() {
1200   char *s = NULL;
1201   char *result = strsep(&s, ""); // no-warning
1202   clang_analyzer_eval(result == NULL); // expected-warning{{TRUE}}
1203 }
1204 
1205 void strsep_changes_input_pointer(char *s) {
1206   char *original = s;
1207   strsep(&s, ""); // no-warning
1208   clang_analyzer_eval(s == original); // expected-warning{{UNKNOWN}}
1209   clang_analyzer_eval(s == NULL); // expected-warning{{UNKNOWN}}
1210 
1211   // Check that the value is symbolic.
1212   if (s == NULL) {
1213     clang_analyzer_eval(s == NULL); // expected-warning{{TRUE}}
1214   }
1215 }
1216 
1217 void strsep_changes_input_string() {
1218   char str[] = "abc";
1219 
1220   clang_analyzer_eval(str[1] == 'b'); // expected-warning{{TRUE}}
1221 
1222   char *s = str;
1223   strsep(&s, "b"); // no-warning
1224 
1225   // The real strsep will change the first delimiter it finds into a NUL
1226   // character. For now, we just model the invalidation.
1227   clang_analyzer_eval(str[1] == 'b'); // expected-warning{{UNKNOWN}}
1228 }
1229 
1230 //===----------------------------------------------------------------------===
1231 // memset() / explicit_bzero() / bzero()
1232 //===----------------------------------------------------------------------===
1233 
1234 void *memset(void *dest, int ch, size_t count);
1235 
1236 void bzero(void *dst, size_t count);
1237 void explicit_bzero(void *dest, size_t count);
1238 
1239 void *malloc(size_t size);
1240 void free(void *);
1241 
1242 void memset1_char_array_null() {
1243   char str[] = "abcd";
1244   clang_analyzer_eval(strlen(str) == 4); // expected-warning{{TRUE}}
1245   memset(str, '\0', 2);
1246   clang_analyzer_eval(strlen(str) == 0); // expected-warning{{TRUE}}
1247 }
1248 
1249 void memset2_char_array_null() {
1250   char str[] = "abcd";
1251   clang_analyzer_eval(strlen(str) == 4); // expected-warning{{TRUE}}
1252   memset(str, '\0', strlen(str) + 1);
1253   clang_analyzer_eval(strlen(str) == 0); // expected-warning{{TRUE}}
1254   clang_analyzer_eval(str[2] == 0);      // expected-warning{{TRUE}}
1255 }
1256 
1257 void memset3_char_malloc_null() {
1258   char *str = (char *)malloc(10 * sizeof(char));
1259   memset(str + 1, '\0', 8);
1260   clang_analyzer_eval(str[1] == 0); // expected-warning{{UNKNOWN}}
1261   free(str);
1262 }
1263 
1264 void memset4_char_malloc_null() {
1265   char *str = (char *)malloc(10 * sizeof(char));
1266   //void *str = malloc(10 * sizeof(char));
1267   memset(str, '\0', 10);
1268   clang_analyzer_eval(str[1] == 0);      // expected-warning{{TRUE}}
1269   clang_analyzer_eval(strlen(str) == 0); // expected-warning{{TRUE}}
1270   free(str);
1271 }
1272 
1273 #ifdef SUPPRESS_OUT_OF_BOUND
1274 void memset5_char_malloc_overflow_null() {
1275   char *str = (char *)malloc(10 * sizeof(char));
1276   memset(str, '\0', 12);
1277   clang_analyzer_eval(str[1] == 0); // expected-warning{{UNKNOWN}}
1278   free(str);
1279 }
1280 #endif
1281 
1282 void memset6_char_array_nonnull() {
1283   char str[] = "abcd";
1284   clang_analyzer_eval(strlen(str) == 4); // expected-warning{{TRUE}}
1285   memset(str, '0', 2);
1286   clang_analyzer_eval(str[0] == 'a');    // expected-warning{{UNKNOWN}}
1287   clang_analyzer_eval(strlen(str) == 4); // expected-warning{{UNKNOWN}}
1288 }
1289 
1290 #ifdef SUPPRESS_OUT_OF_BOUND
1291 void memset8_char_array_nonnull() {
1292   char str[5] = "abcd";
1293   clang_analyzer_eval(strlen(str) == 4); // expected-warning{{TRUE}}
1294   memset(str, '0', 10); // expected-warning{{'memset' will always overflow; destination buffer has size 5, but size argument is 10}}
1295   clang_analyzer_eval(str[0] != '0');     // expected-warning{{UNKNOWN}}
1296   clang_analyzer_eval(strlen(str) >= 10); // expected-warning{{TRUE}}
1297   clang_analyzer_eval(strlen(str) < 10);  // expected-warning{{FALSE}}
1298 }
1299 #endif
1300 
1301 struct POD_memset {
1302   int num;
1303   char c;
1304 };
1305 
1306 void memset10_struct() {
1307   struct POD_memset pod;
1308   char *str = (char *)&pod;
1309   pod.num = 1;
1310   pod.c = 1;
1311   clang_analyzer_eval(pod.num == 0); // expected-warning{{FALSE}}
1312   memset(str, 0, sizeof(struct POD_memset));
1313   clang_analyzer_eval(pod.num == 0); // expected-warning{{TRUE}}
1314 }
1315 
1316 #ifdef SUPPRESS_OUT_OF_BOUND
1317 void memset11_struct_field() {
1318   struct POD_memset pod;
1319   pod.num = 1;
1320   pod.c = '1';
1321   memset(&pod.num, 0, sizeof(struct POD_memset));
1322 
1323   clang_analyzer_eval(pod.num == 0);  // expected-warning{{TRUE}}
1324   clang_analyzer_eval(pod.c == '\0'); // expected-warning{{TRUE}}
1325 }
1326 
1327 void memset12_struct_field() {
1328   struct POD_memset pod;
1329   pod.num = 1;
1330   pod.c = '1';
1331   memset(&pod.c, 0, sizeof(struct POD_memset)); // expected-warning {{'memset' will always overflow; destination buffer has size 4, but size argument is 8}}
1332   clang_analyzer_eval(pod.num == 0); // expected-warning{{UNKNOWN}}
1333   clang_analyzer_eval(pod.c == 0);   // expected-warning{{UNKNOWN}}
1334 }
1335 
1336 union U_memset {
1337   int i;
1338   double d;
1339   char c;
1340 };
1341 
1342 void memset13_union_field() {
1343   union U_memset u;
1344   u.i = 5;
1345   memset(&u.i, '\0', sizeof(union U_memset));
1346   // Note: This should be TRUE, analyzer can't handle union perfectly now.
1347   clang_analyzer_eval(u.d == 0); // expected-warning{{UNKNOWN}}
1348 }
1349 #endif
1350 
1351 void memset14_region_cast() {
1352   char *str = (char *)malloc(10 * sizeof(int));
1353   int *array = (int *)str;
1354   memset(array, 0, 10 * sizeof(int));
1355   clang_analyzer_eval(str[10] == '\0');            // expected-warning{{TRUE}}
1356   clang_analyzer_eval(strlen((char *)array) == 0); // expected-warning{{TRUE}}
1357   clang_analyzer_eval(strlen(str) == 0);           // expected-warning{{TRUE}}
1358   free(str);
1359 }
1360 
1361 void memset15_region_cast() {
1362   char *str = (char *)malloc(10 * sizeof(int));
1363   int *array = (int *)str;
1364   memset(array, 0, 5 * sizeof(int));
1365   clang_analyzer_eval(str[10] == '\0');            // expected-warning{{UNKNOWN}}
1366   clang_analyzer_eval(strlen((char *)array) == 0); // expected-warning{{TRUE}}
1367   clang_analyzer_eval(strlen(str) == 0);           // expected-warning{{TRUE}}
1368   free(str);
1369 }
1370 
1371 int memset20_scalar() {
1372   int *x = malloc(sizeof(int));
1373   *x = 10;
1374   memset(x, 0, sizeof(int));
1375   int num = 1 / *x; // expected-warning{{Division by zero}}
1376   free(x);
1377   return num;
1378 }
1379 
1380 int memset21_scalar() {
1381   int *x = malloc(sizeof(int));
1382   memset(x, 0, 1);
1383   int num = 1 / *x;
1384   free(x);
1385   return num;
1386 }
1387 
1388 void memset22_array() {
1389   int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
1390   clang_analyzer_eval(array[1] == 2); // expected-warning{{TRUE}}
1391   memset(array, 0, sizeof(array));
1392   clang_analyzer_eval(array[1] == 0); // expected-warning{{TRUE}}
1393 }
1394 
1395 void memset23_array_pod_object() {
1396   struct POD_memset array[10];
1397   array[1].num = 10;
1398   array[1].c = 'c';
1399   clang_analyzer_eval(array[1].num == 10); // expected-warning{{TRUE}}
1400   memset(&array[1], 0, sizeof(struct POD_memset));
1401   clang_analyzer_eval(array[1].num == 0); // expected-warning{{UNKNOWN}}
1402 }
1403 
1404 void memset24_array_pod_object() {
1405   struct POD_memset array[10];
1406   array[1].num = 10;
1407   array[1].c = 'c';
1408   clang_analyzer_eval(array[1].num == 10); // expected-warning{{TRUE}}
1409   memset(array, 0, sizeof(array));
1410   clang_analyzer_eval(array[1].num == 0); // expected-warning{{TRUE}}
1411 }
1412 
1413 void memset25_symbol(char c) {
1414   char array[10] = {1};
1415   if (c != 0)
1416     return;
1417 
1418   memset(array, c, 10);
1419 
1420   clang_analyzer_eval(strlen(array) == 0); // expected-warning{{TRUE}}
1421   clang_analyzer_eval(array[4] == 0); // expected-warning{{TRUE}}
1422 }
1423 
1424 void memset26_upper_UCHAR_MAX() {
1425   char array[10] = {1};
1426 
1427   memset(array, 1024, 10);
1428 
1429   clang_analyzer_eval(strlen(array) == 0); // expected-warning{{TRUE}}
1430   clang_analyzer_eval(array[4] == 0); // expected-warning{{TRUE}}
1431 }
1432 
1433 void bzero1_null() {
1434   char *a = NULL;
1435 
1436   bzero(a, 10); // expected-warning{{Null pointer argument in call to memory clearance function}}
1437 }
1438 
1439 void bzero2_char_array_null() {
1440   char str[] = "abcd";
1441   clang_analyzer_eval(strlen(str) == 4); // expected-warning{{TRUE}}
1442   bzero(str, 2);
1443   clang_analyzer_eval(strlen(str) == 0); // expected-warning{{TRUE}}
1444 }
1445 
1446 void bzero3_char_ptr_null() {
1447   char *str = "abcd";
1448   clang_analyzer_eval(strlen(str) == 4); // expected-warning{{TRUE}}
1449   bzero(str + 2, 2);
1450   clang_analyzer_eval(strlen(str) == 0); // expected-warning{{FALSE}}
1451 }
1452 
1453 void explicit_bzero1_null() {
1454   char *a = NULL;
1455 
1456   explicit_bzero(a, 10); // expected-warning{{Null pointer argument in call to memory clearance function}}
1457 }
1458 
1459 void explicit_bzero2_clear_mypassword() {
1460   char passwd[7] = "passwd";
1461 
1462   explicit_bzero(passwd, sizeof(passwd)); // no-warning
1463 
1464   clang_analyzer_eval(strlen(passwd) == 0); // expected-warning{{TRUE}}
1465   clang_analyzer_eval(passwd[0] == '\0'); // expected-warning{{TRUE}}
1466 }
1467 
1468 void explicit_bzero3_out_ofbound() {
1469   char *privkey = (char *)malloc(7);
1470   const char newprivkey[10] = "mysafekey";
1471 
1472   strcpy(privkey, "random");
1473   explicit_bzero(privkey, sizeof(newprivkey));
1474 #ifndef SUPPRESS_OUT_OF_BOUND
1475   // expected-warning@-2 {{Memory clearance function accesses out-of-bound array element}}
1476 #endif
1477   clang_analyzer_eval(privkey[0] == '\0');
1478 #ifdef SUPPRESS_OUT_OF_BOUND
1479   // expected-warning@-2 {{UNKNOWN}}
1480 #endif
1481   free(privkey);
1482 }
1483 
1484 //===----------------------------------------------------------------------===
1485 // FIXMEs
1486 //===----------------------------------------------------------------------===
1487 
1488 // The analyzer_eval call below should evaluate to true. We are being too
1489 // aggressive in marking the (length of) src symbol dead. The length of dst
1490 // depends on src. This could be explicitly specified in the checker or the
1491 // logic for handling MetadataSymbol in SymbolManager needs to change.
1492 void strcat_symbolic_src_length(char *src) {
1493 	char dst[8] = "1234";
1494 	strcat(dst, src);
1495   clang_analyzer_eval(strlen(dst) >= 4); // expected-warning{{UNKNOWN}}
1496 }
1497 
1498 
1499 // The analyzer_eval call below should evaluate to true. Most likely the same
1500 // issue as the test above.
1501 void strncpy_exactly_matching_buffer2(char *y) {
1502 	if (strlen(y) >= 4)
1503 		return;
1504 
1505 	char x[4];
1506 	strncpy(x, y, 4); // no-warning
1507 
1508 	// This time, we know that y fits in x anyway.
1509   clang_analyzer_eval(strlen(x) <= 3); // expected-warning{{UNKNOWN}}
1510 }
1511 
1512 void memset7_char_array_nonnull() {
1513   char str[5] = "abcd";
1514   clang_analyzer_eval(strlen(str) == 4); // expected-warning{{TRUE}}
1515   memset(str, '0', 5);
1516   // FIXME: This should be TRUE.
1517   clang_analyzer_eval(str[0] == '0');    // expected-warning{{UNKNOWN}}
1518   clang_analyzer_eval(strlen(str) >= 5); // expected-warning{{TRUE}}
1519 }
1520 
1521 void memset16_region_cast() {
1522   char *str = (char *)malloc(10 * sizeof(int));
1523   int *array = (int *)str;
1524   memset(array, '0', 10 * sizeof(int));
1525   // FIXME: This should be TRUE.
1526   clang_analyzer_eval(str[10] == '0');                            // expected-warning{{UNKNOWN}}
1527   clang_analyzer_eval(strlen((char *)array) >= 10 * sizeof(int)); // expected-warning{{TRUE}}
1528   clang_analyzer_eval(strlen(str) >= 10 * sizeof(int));           // expected-warning{{TRUE}}
1529   free(str);
1530 }
1531 
1532 #ifdef SUPPRESS_OUT_OF_BOUND
1533 void memset17_region_cast() {
1534   char *str = (char *)malloc(10 * sizeof(int));
1535   int *array = (int *)str;
1536   memset(array, '0', 12 * sizeof(int));
1537   clang_analyzer_eval(str[10] == '0');                            // expected-warning{{UNKNOWN}}
1538   clang_analyzer_eval(strlen((char *)array) >= 12 * sizeof(int)); // expected-warning{{TRUE}}
1539   clang_analyzer_eval(strlen(str) >= 12 * sizeof(int));           // expected-warning{{TRUE}}
1540   free(str);
1541 }
1542 
1543 void memset18_memset_multiple_times() {
1544   char *str = (char *)malloc(10 * sizeof(char));
1545   clang_analyzer_eval(strlen(str) == 0); // expected-warning{{UNKNOWN}}
1546 
1547   memset(str + 2, '\0', 10 * sizeof(char));
1548   clang_analyzer_eval(strlen(str) == 0); // expected-warning{{UNKNOWN}}
1549   clang_analyzer_eval(str[1] == '\0');   // expected-warning{{UNKNOWN}}
1550 
1551   memset(str, '0', 10 * sizeof(char));
1552   clang_analyzer_eval(strlen(str) >= 10); // expected-warning{{TRUE}}
1553   // FIXME: This should be TRUE.
1554   clang_analyzer_eval(str[1] == '0');     // expected-warning{{UNKNOWN}}
1555 
1556   free(str);
1557 }
1558 
1559 void memset19_memset_multiple_times() {
1560   char *str = (char *)malloc(10 * sizeof(char));
1561   clang_analyzer_eval(strlen(str) == 0); // expected-warning{{UNKNOWN}}
1562 
1563   memset(str, '0', 10 * sizeof(char));
1564   clang_analyzer_eval(strlen(str) >= 10); // expected-warning{{TRUE}}
1565   // FIXME: This should be TRUE.
1566   clang_analyzer_eval(str[1] == '0');     // expected-warning{{UNKNOWN}}
1567 
1568   memset(str + 2, '\0', 10 * sizeof(char));
1569   clang_analyzer_eval(strlen(str) >= 10); // expected-warning{{UNKNOWN}}
1570   clang_analyzer_eval(str[1] == '0');     // expected-warning{{UNKNOWN}}
1571 
1572   free(str);
1573 }
1574 #endif
1575 
1576 // The analyzer does not support binding a symbol with default binding.
1577 void memset27_symbol(char c) {
1578   char array[10] = {0};
1579   if (c < 10)
1580     return;
1581 
1582   memset(array, c, 10);
1583 
1584   clang_analyzer_eval(strlen(array) >= 10); // expected-warning{{TRUE}}
1585   // FIXME: This should be TRUE.
1586   clang_analyzer_eval(array[4] >= 10); // expected-warning{{UNKNOWN}}
1587 }
1588 
1589 void memset28() {
1590   short x;
1591   memset(&x, 1, sizeof(short));
1592   // This should be true.
1593   clang_analyzer_eval(x == 0x101); // expected-warning{{UNKNOWN}}
1594 }
1595 
1596 void memset29_plain_int_zero() {
1597   short x;
1598   memset(&x, 0, sizeof(short));
1599   clang_analyzer_eval(x == 0); // expected-warning{{TRUE}}
1600 }
1601 
1602 void test_memset_chk() {
1603   int x;
1604   __builtin___memset_chk(&x, 0, sizeof(x), __builtin_object_size(&x, 0));
1605   clang_analyzer_eval(x == 0); // expected-warning{{TRUE}}
1606 }
1607