[RISCV][FMV] Support target_clones (#85786)
[llvm-project.git] / clang / test / Analysis / string.c
blob85232624160c062874469f00ad41d0598bca1826
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
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
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
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
45 //===----------------------------------------------------------------------===
46 // Declarations
47 //===----------------------------------------------------------------------===
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.
54 // Some functions are implemented as builtins. These should be #defined as
55 // BUILTIN(f), which will prepend "__builtin_" if USE_BUILTINS is defined.
57 // Functions that have variants and are also available as builtins should be
58 // declared carefully! See memcpy() for an example.
60 #ifdef USE_BUILTINS
61 # define BUILTIN(f) __builtin_ ## f
62 #else /* USE_BUILTINS */
63 # define BUILTIN(f) f
64 #endif /* USE_BUILTINS */
66 #define NULL 0
67 typedef typeof(sizeof(int)) size_t;
69 void clang_analyzer_eval(int);
71 int scanf(const char *restrict format, ...);
72 void *malloc(size_t);
73 void free(void *);
74 void *memcpy(void *restrict dest, const void *restrict src, size_t n);
76 //===----------------------------------------------------------------------===
77 // strlen()
78 //===----------------------------------------------------------------------===
80 #define strlen BUILTIN(strlen)
81 size_t strlen(const char *s);
83 void strlen_constant0(void) {
84 clang_analyzer_eval(strlen("123") == 3); // expected-warning{{TRUE}}
87 void strlen_constant1(void) {
88 const char *a = "123";
89 clang_analyzer_eval(strlen(a) == 3); // expected-warning{{TRUE}}
92 void strlen_constant2(char x) {
93 char a[] = "123";
94 clang_analyzer_eval(strlen(a) == 3); // expected-warning{{TRUE}}
96 a[0] = x;
97 clang_analyzer_eval(strlen(a) == 3); // expected-warning{{UNKNOWN}}
100 const char *const global_str_ptr = "abcd";
101 const char global_str_arr[] = "efgh";
102 const char *global_non_const_ptr1 = "ijk";
103 char *global_non_const_ptr2 = "lmn";
104 char global_non_const_arr[] = "op";
106 void strlen_global_constant_ptr(void) {
107 clang_analyzer_eval(strlen(global_str_ptr) == 4); // expected-warning{{TRUE}}
110 void strlen_global_constant_arr(void) {
111 clang_analyzer_eval(strlen(global_str_arr) == 4); // expected-warning{{TRUE}}
114 void strlen_global_non_const_ptr(void) {
115 clang_analyzer_eval(strlen(global_non_const_ptr1) == 3); // expected-warning{{UNKNOWN}}
116 clang_analyzer_eval(strlen(global_non_const_ptr2) == 3); // expected-warning{{UNKNOWN}}
119 void strlen_global_non_const_arr(void) {
120 clang_analyzer_eval(strlen(global_non_const_arr) == 2); // expected-warning{{UNKNOWN}}
123 size_t strlen_null(void) {
124 return strlen(0); // expected-warning{{Null pointer passed as 1st argument to string length function}}
127 size_t strlen_fn(void) {
128 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}}
131 size_t strlen_nonloc(void) {
132 label:
133 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}}
136 void strlen_subregion(void) {
137 struct two_strings { char a[2], b[2]; };
138 extern void use_two_strings(struct two_strings *);
140 struct two_strings z;
141 use_two_strings(&z);
143 size_t a = strlen(z.a);
144 z.b[0] = 5;
145 size_t b = strlen(z.a);
146 if (a == 0)
147 clang_analyzer_eval(b == 0); // expected-warning{{TRUE}}
149 use_two_strings(&z);
151 size_t c = strlen(z.a);
152 if (a == 0)
153 clang_analyzer_eval(c == 0); // expected-warning{{UNKNOWN}}
156 extern void use_string(char *);
157 void strlen_argument(char *x) {
158 size_t a = strlen(x);
159 size_t b = strlen(x);
160 if (a == 0)
161 clang_analyzer_eval(b == 0); // expected-warning{{TRUE}}
163 use_string(x);
165 size_t c = strlen(x);
166 if (a == 0)
167 clang_analyzer_eval(c == 0); // expected-warning{{UNKNOWN}}
170 extern char global_str[];
171 void strlen_global(void) {
172 size_t a = strlen(global_str);
173 size_t b = strlen(global_str);
174 if (a == 0) {
175 clang_analyzer_eval(b == 0); // expected-warning{{TRUE}}
176 // Make sure clang_analyzer_eval does not invalidate globals.
177 clang_analyzer_eval(strlen(global_str) == 0); // expected-warning{{TRUE}}
180 // Call a function with unknown effects, which should invalidate globals.
181 use_string(0);
183 size_t c = strlen(global_str);
184 if (a == 0)
185 clang_analyzer_eval(c == 0); // expected-warning{{UNKNOWN}}
188 void strlen_indirect(char *x) {
189 size_t a = strlen(x);
190 char *p = x;
191 char **p2 = &p;
192 size_t b = strlen(x);
193 if (a == 0)
194 clang_analyzer_eval(b == 0); // expected-warning{{TRUE}}
196 extern void use_string_ptr(char*const*);
197 use_string_ptr(p2);
199 size_t c = strlen(x);
200 if (a == 0)
201 clang_analyzer_eval(c == 0); // expected-warning{{UNKNOWN}}
204 void strlen_indirect2(char *x) {
205 size_t a = strlen(x);
206 char *p = x;
207 char **p2 = &p;
208 extern void use_string_ptr2(char**);
209 use_string_ptr2(p2);
211 size_t c = strlen(x);
212 if (a == 0)
213 clang_analyzer_eval(c == 0); // expected-warning{{UNKNOWN}}
216 void strlen_liveness(const char *x) {
217 if (strlen(x) < 5)
218 return;
219 clang_analyzer_eval(strlen(x) < 5); // expected-warning{{FALSE}}
223 size_t strlenWrapper(const char *str) {
224 return strlen(str);
227 extern void invalidate(char *s);
229 void testStrlenCallee(void) {
230 char str[42];
231 invalidate(str);
232 size_t lenBefore = strlenWrapper(str);
233 invalidate(str);
234 size_t lenAfter = strlenWrapper(str);
235 clang_analyzer_eval(lenBefore == lenAfter); // expected-warning{{UNKNOWN}}
239 //===----------------------------------------------------------------------===
240 // strnlen()
241 //===----------------------------------------------------------------------===
243 size_t strnlen(const char *s, size_t maxlen);
245 void strnlen_constant0(void) {
246 clang_analyzer_eval(strnlen("123", 10) == 3); // expected-warning{{TRUE}}
249 void strnlen_constant1(void) {
250 const char *a = "123";
251 clang_analyzer_eval(strnlen(a, 10) == 3); // expected-warning{{TRUE}}
254 void strnlen_constant2(char x) {
255 char a[] = "123";
256 clang_analyzer_eval(strnlen(a, 10) == 3); // expected-warning{{TRUE}}
257 a[0] = x;
258 clang_analyzer_eval(strnlen(a, 10) == 3); // expected-warning{{UNKNOWN}}
261 void strnlen_constant4(void) {
262 clang_analyzer_eval(strnlen("123456", 3) == 3); // expected-warning{{TRUE}}
265 void strnlen_constant5(void) {
266 const char *a = "123456";
267 clang_analyzer_eval(strnlen(a, 3) == 3); // expected-warning{{TRUE}}
270 void strnlen_constant6(char x) {
271 char a[] = "123456";
272 clang_analyzer_eval(strnlen(a, 3) == 3); // expected-warning{{TRUE}}
273 a[0] = x;
274 clang_analyzer_eval(strnlen(a, 3) == 3); // expected-warning{{UNKNOWN}}
277 size_t strnlen_null(void) {
278 return strnlen(0, 3); // expected-warning{{Null pointer passed as 1st argument to string length function}}
281 size_t strnlen_fn(void) {
282 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}}
285 size_t strnlen_nonloc(void) {
286 label:
287 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}}
290 void strnlen_zero(void) {
291 clang_analyzer_eval(strnlen("abc", 0) == 0); // expected-warning{{TRUE}}
292 clang_analyzer_eval(strnlen(NULL, 0) == 0); // expected-warning{{TRUE}}
295 size_t strnlen_compound_literal(void) {
296 // This used to crash because we don't model the string lengths of
297 // compound literals.
298 return strnlen((char[]) { 'a', 'b', 0 }, 1);
301 size_t strnlen_unknown_limit(float f) {
302 // This used to crash because we don't model the integer values of floats.
303 return strnlen("abc", (int)f);
306 void strnlen_is_not_strlen(char *x) {
307 clang_analyzer_eval(strnlen(x, 10) == strlen(x)); // expected-warning{{UNKNOWN}}
310 void strnlen_at_limit(char *x) {
311 size_t len = strnlen(x, 10);
312 clang_analyzer_eval(len <= 10); // expected-warning{{TRUE}}
313 clang_analyzer_eval(len == 10); // expected-warning{{UNKNOWN}}
314 clang_analyzer_eval(len < 10); // expected-warning{{UNKNOWN}}
317 void strnlen_at_actual(size_t limit) {
318 size_t len = strnlen("abc", limit);
319 clang_analyzer_eval(len <= 3); // expected-warning{{TRUE}}
320 // This is due to eager assertion in strnlen.
321 if (limit == 0) {
322 clang_analyzer_eval(len == 0); // expected-warning{{TRUE}}
323 } else {
324 clang_analyzer_eval(len == 3); // expected-warning{{UNKNOWN}}
325 clang_analyzer_eval(len < 3); // expected-warning{{UNKNOWN}}
329 //===----------------------------------------------------------------------===
330 // strcpy()
331 //===----------------------------------------------------------------------===
333 #ifdef VARIANT
335 #define __strcpy_chk BUILTIN(__strcpy_chk)
336 char *__strcpy_chk(char *restrict s1, const char *restrict s2, size_t destlen);
338 #define strcpy(a,b) __strcpy_chk(a,b,(size_t)-1)
340 #else /* VARIANT */
342 #define strcpy BUILTIN(strcpy)
343 char *strcpy(char *restrict s1, const char *restrict s2);
345 #endif /* VARIANT */
348 void strcpy_null_dst(char *x) {
349 strcpy(NULL, x); // expected-warning{{Null pointer passed as 1st argument to string copy function}}
352 void strcpy_null_src(char *x) {
353 strcpy(x, NULL); // expected-warning{{Null pointer passed as 2nd argument to string copy function}}
356 void strcpy_fn(char *x) {
357 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}}
360 void strcpy_fn_const(char *x) {
361 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}}
364 extern int globalInt;
365 void strcpy_effects(char *x, char *y) {
366 char a = x[0];
367 if (globalInt != 42)
368 return;
370 clang_analyzer_eval(strcpy(x, y) == x); // expected-warning{{TRUE}}
371 clang_analyzer_eval(strlen(x) == strlen(y)); // expected-warning{{TRUE}}
372 clang_analyzer_eval(a == x[0]); // expected-warning{{UNKNOWN}}
373 clang_analyzer_eval(globalInt == 42); // expected-warning{{TRUE}}
376 #ifndef SUPPRESS_OUT_OF_BOUND
377 void strcpy_overflow(char *y) {
378 char x[4];
379 if (strlen(y) == 4)
380 strcpy(x, y); // expected-warning{{String copy function overflows the destination buffer}}
382 #endif
384 void strcpy_no_overflow(char *y) {
385 char x[4];
386 if (strlen(y) == 3)
387 strcpy(x, y); // no-warning
390 // PR37503
391 void *get_void_ptr(void);
392 char ***type_punned_ptr;
393 void strcpy_no_assertion(char c) {
394 *(unsigned char **)type_punned_ptr = (unsigned char *)(get_void_ptr());
395 strcpy(**type_punned_ptr, &c); // no-crash
398 // PR49007
399 char f(char ***c, int *i) {
400 *(void **)c = i + 1;
401 return (**c)[0]; // no-crash
404 //===----------------------------------------------------------------------===
405 // stpcpy()
406 //===----------------------------------------------------------------------===
408 #ifdef VARIANT
410 #define __stpcpy_chk BUILTIN(__stpcpy_chk)
411 char *__stpcpy_chk(char *restrict s1, const char *restrict s2, size_t destlen);
413 #define stpcpy(a,b) __stpcpy_chk(a,b,(size_t)-1)
415 #else /* VARIANT */
417 #define stpcpy BUILTIN(stpcpy)
418 char *stpcpy(char *restrict s1, const char *restrict s2);
420 #endif /* VARIANT */
423 void stpcpy_effect(char *x, char *y) {
424 char a = x[0];
426 clang_analyzer_eval(stpcpy(x, y) == &x[strlen(y)]); // expected-warning{{TRUE}}
427 clang_analyzer_eval(strlen(x) == strlen(y)); // expected-warning{{TRUE}}
428 clang_analyzer_eval(a == x[0]); // expected-warning{{UNKNOWN}}
431 #ifndef SUPPRESS_OUT_OF_BOUND
432 void stpcpy_overflow(char *y) {
433 char x[4];
434 if (strlen(y) == 4)
435 stpcpy(x, y); // expected-warning{{String copy function overflows the destination buffer}}
437 #endif
439 void stpcpy_no_overflow(char *y) {
440 char x[4];
441 if (strlen(y) == 3)
442 stpcpy(x, y); // no-warning
445 //===----------------------------------------------------------------------===
446 // strcat()
447 //===----------------------------------------------------------------------===
449 #ifdef VARIANT
451 #define __strcat_chk BUILTIN(__strcat_chk)
452 char *__strcat_chk(char *restrict s1, const char *restrict s2, size_t destlen);
454 #define strcat(a,b) __strcat_chk(a,b,(size_t)-1)
456 #else /* VARIANT */
458 #define strcat BUILTIN(strcat)
459 char *strcat(char *restrict s1, const char *restrict s2);
461 #endif /* VARIANT */
464 void strcat_null_dst(char *x) {
465 strcat(NULL, x); // expected-warning{{Null pointer passed as 1st argument to string concatenation function}}
468 void strcat_null_src(char *x) {
469 strcat(x, NULL); // expected-warning{{Null pointer passed as 2nd argument to string concatenation function}}
472 void strcat_fn(char *x) {
473 strcat(x, (char*)&strcat_fn); // expected-warning{{Argument to string concatenation function is the address of the function 'strcat_fn', which is not a null-terminated string}}
476 void strcat_effects(char *y) {
477 char x[8] = "123";
478 size_t orig_len = strlen(x);
479 char a = x[0];
481 if (strlen(y) != 4)
482 return;
484 clang_analyzer_eval(strcat(x, y) == x); // expected-warning{{TRUE}}
485 clang_analyzer_eval((int)strlen(x) == (orig_len + strlen(y))); // expected-warning{{TRUE}}
488 #ifndef SUPPRESS_OUT_OF_BOUND
489 void strcat_overflow_0(char *y) {
490 char x[4] = "12";
491 if (strlen(y) == 4)
492 strcat(x, y); // expected-warning{{String concatenation function overflows the destination buffer}}
495 void strcat_overflow_1(char *y) {
496 char x[4] = "12";
497 if (strlen(y) == 3)
498 strcat(x, y); // expected-warning{{String concatenation function overflows the destination buffer}}
501 void strcat_overflow_2(char *y) {
502 char x[4] = "12";
503 if (strlen(y) == 2)
504 strcat(x, y); // expected-warning{{String concatenation function overflows the destination buffer}}
506 #endif
508 void strcat_no_overflow(char *y) {
509 char x[5] = "12";
510 if (strlen(y) == 2)
511 strcat(x, y); // no-warning
514 void strcat_symbolic_dst_length(char *dst) {
515 strcat(dst, "1234");
516 clang_analyzer_eval(strlen(dst) >= 4); // expected-warning{{TRUE}}
519 void strcat_symbolic_dst_length_taint(char *dst) {
520 scanf("%s", dst); // Taint data.
521 strcat(dst, "1234");
522 clang_analyzer_eval(strlen(dst) >= 4); // expected-warning{{TRUE}}
525 void strcat_unknown_src_length(char *src, int offset) {
526 char dst[8] = "1234";
527 strcat(dst, &src[offset]);
528 clang_analyzer_eval(strlen(dst) >= 4); // expected-warning{{TRUE}}
531 // There is no strcat_unknown_dst_length because if we can't get a symbolic
532 // length for the "before" strlen, we won't be able to set one for "after".
534 void strcat_too_big(char *dst, char *src) {
535 // We assume this can never actually happen, so we don't get a warning.
536 if (strlen(dst) != (((size_t)0) - 2))
537 return;
538 if (strlen(src) != 2)
539 return;
540 strcat(dst, src);
544 //===----------------------------------------------------------------------===
545 // strncpy()
546 //===----------------------------------------------------------------------===
548 #ifdef VARIANT
550 #define __strncpy_chk BUILTIN(__strncpy_chk)
551 char *__strncpy_chk(char *restrict s1, const char *restrict s2, size_t n, size_t destlen);
553 #define strncpy(a,b,n) __strncpy_chk(a,b,n,(size_t)-1)
555 #else /* VARIANT */
557 #define strncpy BUILTIN(strncpy)
558 char *strncpy(char *restrict s1, const char *restrict s2, size_t n);
560 #endif /* VARIANT */
563 void strncpy_null_dst(char *x) {
564 strncpy(NULL, x, 5); // expected-warning{{Null pointer passed as 1st argument to string copy function}}
567 void strncpy_null_src(char *x) {
568 strncpy(x, NULL, 5); // expected-warning{{Null pointer passed as 2nd argument to string copy function}}
571 void strncpy_fn(char *x) {
572 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}}
575 void strncpy_effects(char *x, char *y) {
576 char a = x[0];
578 clang_analyzer_eval(strncpy(x, y, 5) == x); // expected-warning{{TRUE}}
579 clang_analyzer_eval(strlen(x) == strlen(y)); // expected-warning{{UNKNOWN}}
580 clang_analyzer_eval(a == x[0]); // expected-warning{{UNKNOWN}}
583 #ifndef SUPPRESS_OUT_OF_BOUND
584 // Enabling the malloc checker enables some of the buffer-checking portions
585 // of the C-string checker.
586 void cstringchecker_bounds_nocrash(void) {
587 char *p = malloc(2);
588 strncpy(p, "AAA", sizeof("AAA"));
589 // expected-warning@-1 {{String copy function overflows the destination buffer}}
590 free(p);
593 void strncpy_overflow(char *y) {
594 char x[4];
595 if (strlen(y) == 4)
596 strncpy(x, y, 5);
597 // expected-warning@-1 {{String copy function overflows the destination buffer}}
598 #ifndef VARIANT
599 // expected-warning@-3 {{size argument is too large; destination buffer has size 4, but size argument is 5}}
600 #endif
603 void strncpy_no_overflow(char *y) {
604 char x[4];
605 if (strlen(y) == 3)
606 strncpy(x, y, 5);
607 // expected-warning@-1 {{String copy function overflows the destination buffer}}
608 #ifndef VARIANT
609 // expected-warning@-3 {{size argument is too large; destination buffer has size 4, but size argument is 5}}
610 #endif
613 void strncpy_no_overflow2(char *y, int n) {
614 if (n <= 4)
615 return;
617 char x[4];
618 if (strlen(y) == 3)
619 strncpy(x, y, n);
620 // expected-warning@-1 {{String copy function overflows the destination buffer}}
622 #endif
624 void strncpy_truncate(char *y) {
625 char x[4];
626 if (strlen(y) == 4)
627 strncpy(x, y, 3); // no-warning
630 void strncpy_no_truncate(char *y) {
631 char x[4];
632 if (strlen(y) == 3)
633 strncpy(x, y, 3); // no-warning
636 void strncpy_exactly_matching_buffer(char *y) {
637 char x[4];
638 strncpy(x, y, 4); // no-warning
640 // strncpy does not null-terminate, so we have no idea what the strlen is
641 // after this.
642 clang_analyzer_eval(strlen(x) > 4); // expected-warning{{UNKNOWN}}
645 void strncpy_zero(char *src) {
646 char dst[] = "123";
647 strncpy(dst, src, 0); // no-warning
650 void strncpy_empty(void) {
651 char dst[] = "123";
652 char src[] = "";
653 strncpy(dst, src, 4); // no-warning
656 //===----------------------------------------------------------------------===
657 // strncat()
658 //===----------------------------------------------------------------------===
660 #ifdef VARIANT
662 #define __strncat_chk BUILTIN(__strncat_chk)
663 char *__strncat_chk(char *restrict s1, const char *restrict s2, size_t n, size_t destlen);
665 #define strncat(a,b,c) __strncat_chk(a,b,c, (size_t)-1)
667 #else /* VARIANT */
669 #define strncat BUILTIN(strncat)
670 char *strncat(char *restrict s1, const char *restrict s2, size_t n);
672 #endif /* VARIANT */
675 void strncat_null_dst(char *x) {
676 strncat(NULL, x, 4); // expected-warning{{Null pointer passed as 1st argument to string concatenation function}}
679 void strncat_null_src(char *x) {
680 strncat(x, NULL, 4); // expected-warning{{Null pointer passed as 2nd argument to string concatenation function}}
683 void strncat_fn(char *x) {
684 strncat(x, (char*)&strncat_fn, 4); // expected-warning{{Argument to string concatenation function is the address of the function 'strncat_fn', which is not a null-terminated string}}
687 void strncat_effects(char *y) {
688 char x[8] = "123";
689 size_t orig_len = strlen(x);
690 char a = x[0];
692 if (strlen(y) != 4)
693 return;
695 clang_analyzer_eval(strncat(x, y, strlen(y)) == x); // expected-warning{{TRUE}}
696 clang_analyzer_eval(strlen(x) == (orig_len + strlen(y))); // expected-warning{{TRUE}}
699 #ifndef SUPPRESS_OUT_OF_BOUND
700 void strncat_overflow_0(char *y) {
701 char x[4] = "12";
702 if (strlen(y) == 4)
703 strncat(x, y, strlen(y));
704 // expected-warning@-1 {{String concatenation function overflows the destination buffer}}
707 void strncat_overflow_1(char *y) {
708 char x[4] = "12";
709 if (strlen(y) == 3)
710 strncat(x, y, strlen(y));
711 // expected-warning@-1 {{String concatenation function overflows the destination buffer}}
714 void strncat_overflow_2(char *y) {
715 char x[4] = "12";
716 if (strlen(y) == 2)
717 strncat(x, y, strlen(y));
718 // expected-warning@-1 {{String concatenation function overflows the destination buffer}}
721 void strncat_overflow_3(char *y) {
722 char x[4] = "12";
723 if (strlen(y) == 4)
724 strncat(x, y, 2);
725 // expected-warning@-1 {{String concatenation function overflows the destination buffer}}
727 #endif
729 void strncat_no_overflow_1(char *y) {
730 char x[5] = "12";
731 if (strlen(y) == 2)
732 strncat(x, y, strlen(y)); // no-warning
735 void strncat_no_overflow_2(char *y) {
736 char x[4] = "12";
737 if (strlen(y) == 4)
738 strncat(x, y, 1); // no-warning
741 void strncat_symbolic_dst_length(char *dst) {
742 strncat(dst, "1234", 5);
743 clang_analyzer_eval(strlen(dst) >= 4); // expected-warning{{TRUE}}
746 #ifndef SUPPRESS_OUT_OF_BOUND
747 void strncat_symbolic_src_length(char *src) {
748 char dst[8] = "1234";
749 strncat(dst, src, 3);
750 clang_analyzer_eval(strlen(dst) >= 4); // expected-warning{{TRUE}}
752 char dst2[8] = "1234";
753 strncat(dst2, src, 4);
754 // expected-warning@-1 {{String concatenation function overflows the destination buffer}}
757 void strncat_unknown_src_length(char *src, int offset) {
758 char dst[8] = "1234";
759 strncat(dst, &src[offset], 3);
760 clang_analyzer_eval(strlen(dst) >= 4); // expected-warning{{TRUE}}
762 char dst2[8] = "1234";
763 strncat(dst2, &src[offset], 4);
764 // expected-warning@-1 {{String concatenation function overflows the destination buffer}}
766 #endif
768 // There is no strncat_unknown_dst_length because if we can't get a symbolic
769 // length for the "before" strlen, we won't be able to set one for "after".
771 void strncat_symbolic_limit(unsigned limit) {
772 char dst[6] = "1234";
773 char src[] = "567";
774 strncat(dst, src, limit); // no-warning
776 clang_analyzer_eval(strlen(dst) >= 4); // expected-warning{{TRUE}}
777 clang_analyzer_eval(strlen(dst) == 4); // expected-warning{{UNKNOWN}}
780 void strncat_unknown_limit(float limit) {
781 char dst[6] = "1234";
782 char src[] = "567";
783 strncat(dst, src, (size_t)limit); // no-warning
785 clang_analyzer_eval(strlen(dst) >= 4); // expected-warning{{TRUE}}
786 clang_analyzer_eval(strlen(dst) == 4); // expected-warning{{UNKNOWN}}
789 void strncat_too_big(char *dst, char *src) {
790 // We assume this will never actually happen, so we don't get a warning.
791 if (strlen(dst) != (((size_t)0) - 2))
792 return;
793 if (strlen(src) != 2)
794 return;
795 strncat(dst, src, 2);
798 void strncat_zero(char *src) {
799 char dst[] = "123";
800 strncat(dst, src, 0); // no-warning
803 void strncat_empty(void) {
804 char dst[8] = "123";
805 char src[] = "";
806 strncat(dst, src, 4); // no-warning
809 //===----------------------------------------------------------------------===
810 // strcmp()
811 //===----------------------------------------------------------------------===
813 #define strcmp BUILTIN(strcmp)
814 int strcmp(const char * s1, const char * s2);
816 void strcmp_check_modelling(void) {
817 char *x = "aa";
818 char *y = "a";
819 clang_analyzer_eval(strcmp(x, y) > 0); // expected-warning{{TRUE}}
820 clang_analyzer_eval(strcmp(x, y) <= 0); // expected-warning{{FALSE}}
821 clang_analyzer_eval(strcmp(x, y) > 1); // expected-warning{{UNKNOWN}}
823 clang_analyzer_eval(strcmp(y, x) < 0); // expected-warning{{TRUE}}
824 clang_analyzer_eval(strcmp(y, x) >= 0); // expected-warning{{FALSE}}
825 clang_analyzer_eval(strcmp(y, x) < -1); // expected-warning{{UNKNOWN}}
828 void strcmp_constant0(void) {
829 clang_analyzer_eval(strcmp("123", "123") == 0); // expected-warning{{TRUE}}
832 void strcmp_constant_and_var_0(void) {
833 char *x = "123";
834 clang_analyzer_eval(strcmp(x, "123") == 0); // expected-warning{{TRUE}}
837 void strcmp_constant_and_var_1(void) {
838 char *x = "123";
839 clang_analyzer_eval(strcmp("123", x) == 0); // expected-warning{{TRUE}}
842 void strcmp_0(void) {
843 char *x = "123";
844 char *y = "123";
845 clang_analyzer_eval(strcmp(x, y) == 0); // expected-warning{{TRUE}}
848 void strcmp_1(void) {
849 char *x = "234";
850 char *y = "123";
851 clang_analyzer_eval(strcmp(x, y) > 0); // expected-warning{{TRUE}}
854 void strcmp_2(void) {
855 char *x = "123";
856 char *y = "234";
857 clang_analyzer_eval(strcmp(x, y) < 0); // expected-warning{{TRUE}}
860 void strcmp_null_0(void) {
861 char *x = NULL;
862 char *y = "123";
863 strcmp(x, y); // expected-warning{{Null pointer passed as 1st argument to string comparison function}}
866 void strcmp_null_1(void) {
867 char *x = "123";
868 char *y = NULL;
869 strcmp(x, y); // expected-warning{{Null pointer passed as 2nd argument to string comparison function}}
872 void strcmp_diff_length_0(void) {
873 char *x = "12345";
874 char *y = "234";
875 clang_analyzer_eval(strcmp(x, y) < 0); // expected-warning{{TRUE}}
878 void strcmp_diff_length_1(void) {
879 char *x = "123";
880 char *y = "23456";
881 clang_analyzer_eval(strcmp(x, y) < 0); // expected-warning{{TRUE}}
884 void strcmp_diff_length_2(void) {
885 char *x = "12345";
886 char *y = "123";
887 clang_analyzer_eval(strcmp(x, y) > 0); // expected-warning{{TRUE}}
890 void strcmp_diff_length_3(void) {
891 char *x = "123";
892 char *y = "12345";
893 clang_analyzer_eval(strcmp(x, y) < 0); // expected-warning{{TRUE}}
896 void strcmp_embedded_null (void) {
897 clang_analyzer_eval(strcmp("\0z", "\0y") == 0); // expected-warning{{TRUE}}
900 void strcmp_unknown_arg (char *unknown) {
901 clang_analyzer_eval(strcmp(unknown, unknown) == 0); // expected-warning{{TRUE}}
904 union argument {
905 char *f;
908 void function_pointer_cast_helper(char **a) {
909 strcmp("Hi", *a); // PR24951 crash
912 void strcmp_union_function_pointer_cast(union argument a) {
913 void (*fPtr)(union argument *) = (void (*)(union argument *))function_pointer_cast_helper;
915 fPtr(&a);
918 int strcmp_null_argument(char *a) {
919 char *b = 0;
920 // Do not warn about the first argument!
921 return strcmp(a, b); // expected-warning{{Null pointer passed as 2nd argument to string comparison function}}
924 //===----------------------------------------------------------------------===
925 // strncmp()
926 //===----------------------------------------------------------------------===
928 #define strncmp BUILTIN(strncmp)
929 int strncmp(const char *s1, const char *s2, size_t n);
931 void strncmp_check_modelling(void) {
932 char *x = "aa";
933 char *y = "a";
934 clang_analyzer_eval(strncmp(x, y, 2) > 0); // expected-warning{{TRUE}}
935 clang_analyzer_eval(strncmp(x, y, 2) <= 0); // expected-warning{{FALSE}}
936 clang_analyzer_eval(strncmp(x, y, 2) > 1); // expected-warning{{UNKNOWN}}
938 clang_analyzer_eval(strncmp(y, x, 2) < 0); // expected-warning{{TRUE}}
939 clang_analyzer_eval(strncmp(y, x, 2) >= 0); // expected-warning{{FALSE}}
940 clang_analyzer_eval(strncmp(y, x, 2) < -1); // expected-warning{{UNKNOWN}}
943 void strncmp_constant0(void) {
944 clang_analyzer_eval(strncmp("123", "123", 3) == 0); // expected-warning{{TRUE}}
947 void strncmp_constant_and_var_0(void) {
948 char *x = "123";
949 clang_analyzer_eval(strncmp(x, "123", 3) == 0); // expected-warning{{TRUE}}
952 void strncmp_constant_and_var_1(void) {
953 char *x = "123";
954 clang_analyzer_eval(strncmp("123", x, 3) == 0); // expected-warning{{TRUE}}
957 void strncmp_0(void) {
958 char *x = "123";
959 char *y = "123";
960 clang_analyzer_eval(strncmp(x, y, 3) == 0); // expected-warning{{TRUE}}
963 void strncmp_1(void) {
964 char *x = "234";
965 char *y = "123";
966 clang_analyzer_eval(strncmp(x, y, 3) > 0); // expected-warning{{TRUE}}
969 void strncmp_2(void) {
970 char *x = "123";
971 char *y = "234";
972 clang_analyzer_eval(strncmp(x, y, 3) < 0); // expected-warning{{TRUE}}
975 void strncmp_null_0(void) {
976 char *x = NULL;
977 char *y = "123";
978 strncmp(x, y, 3); // expected-warning{{Null pointer passed as 1st argument to string comparison function}}
981 void strncmp_null_1(void) {
982 char *x = "123";
983 char *y = NULL;
984 strncmp(x, y, 3); // expected-warning{{Null pointer passed as 2nd argument to string comparison function}}
987 void strncmp_diff_length_0(void) {
988 char *x = "12345";
989 char *y = "234";
990 clang_analyzer_eval(strncmp(x, y, 5) < 0); // expected-warning{{TRUE}}
993 void strncmp_diff_length_1(void) {
994 char *x = "123";
995 char *y = "23456";
996 clang_analyzer_eval(strncmp(x, y, 5) < 0); // expected-warning{{TRUE}}
999 void strncmp_diff_length_2(void) {
1000 char *x = "12345";
1001 char *y = "123";
1002 clang_analyzer_eval(strncmp(x, y, 5) > 0); // expected-warning{{TRUE}}
1005 void strncmp_diff_length_3(void) {
1006 char *x = "123";
1007 char *y = "12345";
1008 clang_analyzer_eval(strncmp(x, y, 5) < 0); // expected-warning{{TRUE}}
1011 void strncmp_diff_length_4(void) {
1012 char *x = "123";
1013 char *y = "12345";
1014 clang_analyzer_eval(strncmp(x, y, 3) == 0); // expected-warning{{TRUE}}
1017 void strncmp_diff_length_5(void) {
1018 char *x = "012";
1019 char *y = "12345";
1020 clang_analyzer_eval(strncmp(x, y, 3) < 0); // expected-warning{{TRUE}}
1023 void strncmp_diff_length_6(void) {
1024 char *x = "234";
1025 char *y = "12345";
1026 clang_analyzer_eval(strncmp(x, y, 3) > 0); // expected-warning{{TRUE}}
1029 void strncmp_embedded_null (void) {
1030 clang_analyzer_eval(strncmp("ab\0zz", "ab\0yy", 4) == 0); // expected-warning{{TRUE}}
1033 int strncmp_null_argument(char *a, size_t n) {
1034 char *b = 0;
1035 // Do not warn about the first argument!
1036 return strncmp(a, b, n); // expected-warning{{Null pointer passed as 2nd argument to string comparison function}}
1039 //===----------------------------------------------------------------------===
1040 // strcasecmp()
1041 //===----------------------------------------------------------------------===
1043 #define strcasecmp BUILTIN(strcasecmp)
1044 int strcasecmp(const char *s1, const char *s2);
1046 void strcasecmp_check_modelling(void) {
1047 char *x = "aa";
1048 char *y = "a";
1049 clang_analyzer_eval(strcasecmp(x, y) > 0); // expected-warning{{TRUE}}
1050 clang_analyzer_eval(strcasecmp(x, y) <= 0); // expected-warning{{FALSE}}
1051 clang_analyzer_eval(strcasecmp(x, y) > 1); // expected-warning{{UNKNOWN}}
1053 clang_analyzer_eval(strcasecmp(y, x) < 0); // expected-warning{{TRUE}}
1054 clang_analyzer_eval(strcasecmp(y, x) >= 0); // expected-warning{{FALSE}}
1055 clang_analyzer_eval(strcasecmp(y, x) < -1); // expected-warning{{UNKNOWN}}
1058 void strcasecmp_constant0(void) {
1059 clang_analyzer_eval(strcasecmp("abc", "Abc") == 0); // expected-warning{{TRUE}}
1062 void strcasecmp_constant_and_var_0(void) {
1063 char *x = "abc";
1064 clang_analyzer_eval(strcasecmp(x, "Abc") == 0); // expected-warning{{TRUE}}
1067 void strcasecmp_constant_and_var_1(void) {
1068 char *x = "abc";
1069 clang_analyzer_eval(strcasecmp("Abc", x) == 0); // expected-warning{{TRUE}}
1072 void strcasecmp_0(void) {
1073 char *x = "abc";
1074 char *y = "Abc";
1075 clang_analyzer_eval(strcasecmp(x, y) == 0); // expected-warning{{TRUE}}
1078 void strcasecmp_1(void) {
1079 char *x = "Bcd";
1080 char *y = "abc";
1081 clang_analyzer_eval(strcasecmp(x, y) > 0); // expected-warning{{TRUE}}
1084 void strcasecmp_2(void) {
1085 char *x = "abc";
1086 char *y = "Bcd";
1087 clang_analyzer_eval(strcasecmp(x, y) < 0); // expected-warning{{TRUE}}
1090 void strcasecmp_null_0(void) {
1091 char *x = NULL;
1092 char *y = "123";
1093 strcasecmp(x, y); // expected-warning{{Null pointer passed as 1st argument to string comparison function}}
1096 void strcasecmp_null_1(void) {
1097 char *x = "123";
1098 char *y = NULL;
1099 strcasecmp(x, y); // expected-warning{{Null pointer passed as 2nd argument to string comparison function}}
1102 void strcasecmp_diff_length_0(void) {
1103 char *x = "abcde";
1104 char *y = "aBd";
1105 clang_analyzer_eval(strcasecmp(x, y) < 0); // expected-warning{{TRUE}}
1108 void strcasecmp_diff_length_1(void) {
1109 char *x = "abc";
1110 char *y = "aBdef";
1111 clang_analyzer_eval(strcasecmp(x, y) < 0); // expected-warning{{TRUE}}
1114 void strcasecmp_diff_length_2(void) {
1115 char *x = "aBcDe";
1116 char *y = "abc";
1117 clang_analyzer_eval(strcasecmp(x, y) > 0); // expected-warning{{TRUE}}
1120 void strcasecmp_diff_length_3(void) {
1121 char *x = "aBc";
1122 char *y = "abcde";
1123 clang_analyzer_eval(strcasecmp(x, y) < 0); // expected-warning{{TRUE}}
1126 void strcasecmp_embedded_null (void) {
1127 clang_analyzer_eval(strcasecmp("ab\0zz", "ab\0yy") == 0); // expected-warning{{TRUE}}
1130 int strcasecmp_null_argument(char *a) {
1131 char *b = 0;
1132 // Do not warn about the first argument!
1133 return strcasecmp(a, b); // expected-warning{{Null pointer passed as 2nd argument to string comparison function}}
1136 //===----------------------------------------------------------------------===
1137 // strncasecmp()
1138 //===----------------------------------------------------------------------===
1140 #define strncasecmp BUILTIN(strncasecmp)
1141 int strncasecmp(const char *s1, const char *s2, size_t n);
1143 void strncasecmp_check_modelling(void) {
1144 char *x = "aa";
1145 char *y = "a";
1146 clang_analyzer_eval(strncasecmp(x, y, 2) > 0); // expected-warning{{TRUE}}
1147 clang_analyzer_eval(strncasecmp(x, y, 2) <= 0); // expected-warning{{FALSE}}
1148 clang_analyzer_eval(strncasecmp(x, y, 2) > 1); // expected-warning{{UNKNOWN}}
1150 clang_analyzer_eval(strncasecmp(y, x, 2) < 0); // expected-warning{{TRUE}}
1151 clang_analyzer_eval(strncasecmp(y, x, 2) >= 0); // expected-warning{{FALSE}}
1152 clang_analyzer_eval(strncasecmp(y, x, 2) < -1); // expected-warning{{UNKNOWN}}
1155 void strncasecmp_constant0(void) {
1156 clang_analyzer_eval(strncasecmp("abc", "Abc", 3) == 0); // expected-warning{{TRUE}}
1159 void strncasecmp_constant_and_var_0(void) {
1160 char *x = "abc";
1161 clang_analyzer_eval(strncasecmp(x, "Abc", 3) == 0); // expected-warning{{TRUE}}
1164 void strncasecmp_constant_and_var_1(void) {
1165 char *x = "abc";
1166 clang_analyzer_eval(strncasecmp("Abc", x, 3) == 0); // expected-warning{{TRUE}}
1169 void strncasecmp_0(void) {
1170 char *x = "abc";
1171 char *y = "Abc";
1172 clang_analyzer_eval(strncasecmp(x, y, 3) == 0); // expected-warning{{TRUE}}
1175 void strncasecmp_1(void) {
1176 char *x = "Bcd";
1177 char *y = "abc";
1178 clang_analyzer_eval(strncasecmp(x, y, 3) > 0); // expected-warning{{TRUE}}
1181 void strncasecmp_2(void) {
1182 char *x = "abc";
1183 char *y = "Bcd";
1184 clang_analyzer_eval(strncasecmp(x, y, 3) < 0); // expected-warning{{TRUE}}
1187 void strncasecmp_null_0(void) {
1188 char *x = NULL;
1189 char *y = "123";
1190 strncasecmp(x, y, 3); // expected-warning{{Null pointer passed as 1st argument to string comparison function}}
1193 void strncasecmp_null_1(void) {
1194 char *x = "123";
1195 char *y = NULL;
1196 strncasecmp(x, y, 3); // expected-warning{{Null pointer passed as 2nd argument to string comparison function}}
1199 void strncasecmp_diff_length_0(void) {
1200 char *x = "abcde";
1201 char *y = "aBd";
1202 clang_analyzer_eval(strncasecmp(x, y, 5) < 0); // expected-warning{{TRUE}}
1205 void strncasecmp_diff_length_1(void) {
1206 char *x = "abc";
1207 char *y = "aBdef";
1208 clang_analyzer_eval(strncasecmp(x, y, 5) < 0); // expected-warning{{TRUE}}
1211 void strncasecmp_diff_length_2(void) {
1212 char *x = "aBcDe";
1213 char *y = "abc";
1214 clang_analyzer_eval(strncasecmp(x, y, 5) > 0); // expected-warning{{TRUE}}
1217 void strncasecmp_diff_length_3(void) {
1218 char *x = "aBc";
1219 char *y = "abcde";
1220 clang_analyzer_eval(strncasecmp(x, y, 5) < 0); // expected-warning{{TRUE}}
1223 void strncasecmp_diff_length_4(void) {
1224 char *x = "abcde";
1225 char *y = "aBc";
1226 clang_analyzer_eval(strncasecmp(x, y, 3) == 0); // expected-warning{{TRUE}}
1229 void strncasecmp_diff_length_5(void) {
1230 char *x = "abcde";
1231 char *y = "aBd";
1232 clang_analyzer_eval(strncasecmp(x, y, 3) < 0); // expected-warning{{TRUE}}
1235 void strncasecmp_diff_length_6(void) {
1236 char *x = "aBDe";
1237 char *y = "abc";
1238 clang_analyzer_eval(strncasecmp(x, y, 3) > 0); // expected-warning{{TRUE}}
1241 void strncasecmp_embedded_null (void) {
1242 clang_analyzer_eval(strncasecmp("ab\0zz", "ab\0yy", 4) == 0); // expected-warning{{TRUE}}
1245 int strncasecmp_null_argument(char *a, size_t n) {
1246 char *b = 0;
1247 // Do not warn about the first argument!
1248 return strncasecmp(a, b, n); // expected-warning{{Null pointer passed as 2nd argument to string comparison function}}
1251 //===----------------------------------------------------------------------===
1252 // strsep()
1253 //===----------------------------------------------------------------------===
1255 char *strsep(char ** restrict stringp, const char * restrict delim);
1257 void strsep_null_delim(char *s) {
1258 strsep(&s, NULL); // expected-warning{{Null pointer passed as 2nd argument to strsep()}}
1261 void strsep_null_search(void) {
1262 strsep(NULL, ""); // expected-warning{{Null pointer passed as 1st argument to strsep()}}
1265 void strsep_return_original_pointer(char *s) {
1266 char *original = s;
1267 char *result = strsep(&s, ""); // no-warning
1268 clang_analyzer_eval(original == result); // expected-warning{{TRUE}}
1271 void strsep_null_string(void) {
1272 char *s = NULL;
1273 char *result = strsep(&s, ""); // no-warning
1274 clang_analyzer_eval(result == NULL); // expected-warning{{TRUE}}
1277 void strsep_changes_input_pointer(char *s) {
1278 char *original = s;
1279 strsep(&s, ""); // no-warning
1280 clang_analyzer_eval(s == original); // expected-warning{{UNKNOWN}}
1281 clang_analyzer_eval(s == NULL); // expected-warning{{UNKNOWN}}
1283 // Check that the value is symbolic.
1284 if (s == NULL) {
1285 clang_analyzer_eval(s == NULL); // expected-warning{{TRUE}}
1289 void strsep_changes_input_string(void) {
1290 char str[] = "abc";
1292 clang_analyzer_eval(str[1] == 'b'); // expected-warning{{TRUE}}
1294 char *s = str;
1295 strsep(&s, "b"); // no-warning
1297 // The real strsep will change the first delimiter it finds into a NUL
1298 // character. For now, we just model the invalidation.
1299 clang_analyzer_eval(str[1] == 'b'); // expected-warning{{UNKNOWN}}
1302 //===----------------------------------------------------------------------===
1303 // memset() / explicit_bzero() / bzero()
1304 //===----------------------------------------------------------------------===
1306 void *memset(void *dest, int ch, size_t count);
1308 void bzero(void *dst, size_t count);
1309 void explicit_bzero(void *dest, size_t count);
1311 void *malloc(size_t size);
1312 void free(void *);
1314 void memset1_char_array_null(void) {
1315 char str[] = "abcd";
1316 clang_analyzer_eval(strlen(str) == 4); // expected-warning{{TRUE}}
1317 memset(str, '\0', 2);
1318 clang_analyzer_eval(strlen(str) == 0); // expected-warning{{TRUE}}
1321 void memset2_char_array_null(void) {
1322 char str[] = "abcd";
1323 clang_analyzer_eval(strlen(str) == 4); // expected-warning{{TRUE}}
1324 memset(str, '\0', strlen(str) + 1);
1325 clang_analyzer_eval(strlen(str) == 0); // expected-warning{{TRUE}}
1326 clang_analyzer_eval(str[2] == 0); // expected-warning{{TRUE}}
1329 void memset3_char_malloc_null(void) {
1330 char *str = (char *)malloc(10 * sizeof(char));
1331 memset(str + 1, '\0', 8);
1332 clang_analyzer_eval(str[1] == 0); // expected-warning{{UNKNOWN}}
1333 free(str);
1336 void memset4_char_malloc_null(void) {
1337 char *str = (char *)malloc(10 * sizeof(char));
1338 //void *str = malloc(10 * sizeof(char));
1339 memset(str, '\0', 10);
1340 clang_analyzer_eval(str[1] == 0); // expected-warning{{TRUE}}
1341 clang_analyzer_eval(strlen(str) == 0); // expected-warning{{TRUE}}
1342 free(str);
1345 #ifdef SUPPRESS_OUT_OF_BOUND
1346 void memset5_char_malloc_overflow_null(void) {
1347 char *str = (char *)malloc(10 * sizeof(char));
1348 memset(str, '\0', 12);
1349 clang_analyzer_eval(str[1] == 0); // expected-warning{{UNKNOWN}}
1350 free(str);
1352 #endif
1354 void memset6_char_array_nonnull(void) {
1355 char str[] = "abcd";
1356 clang_analyzer_eval(strlen(str) == 4); // expected-warning{{TRUE}}
1357 memset(str, '0', 2);
1358 clang_analyzer_eval(str[0] == 'a'); // expected-warning{{UNKNOWN}}
1359 clang_analyzer_eval(strlen(str) == 4); // expected-warning{{UNKNOWN}}
1362 #ifdef SUPPRESS_OUT_OF_BOUND
1363 void memset8_char_array_nonnull(void) {
1364 char str[5] = "abcd";
1365 clang_analyzer_eval(strlen(str) == 4); // expected-warning{{TRUE}}
1366 memset(str, '0', 10); // expected-warning{{'memset' will always overflow; destination buffer has size 5, but size argument is 10}}
1367 clang_analyzer_eval(str[0] != '0'); // expected-warning{{UNKNOWN}}
1368 clang_analyzer_eval(strlen(str) >= 10); // expected-warning{{TRUE}}
1369 clang_analyzer_eval(strlen(str) < 10); // expected-warning{{FALSE}}
1371 #endif
1373 struct POD_memset {
1374 int num;
1375 char c;
1378 void memset10_struct(void) {
1379 struct POD_memset pod;
1380 char *str = (char *)&pod;
1381 pod.num = 1;
1382 pod.c = 1;
1383 clang_analyzer_eval(pod.num == 0); // expected-warning{{FALSE}}
1384 memset(str, 0, sizeof(struct POD_memset));
1385 clang_analyzer_eval(pod.num == 0); // expected-warning{{TRUE}}
1388 #ifdef SUPPRESS_OUT_OF_BOUND
1389 void memset11_struct_field(void) {
1390 struct POD_memset pod;
1391 pod.num = 1;
1392 pod.c = '1';
1393 memset(&pod.num, 0, sizeof(struct POD_memset));
1395 clang_analyzer_eval(pod.num == 0); // expected-warning{{TRUE}}
1396 clang_analyzer_eval(pod.c == '\0'); // expected-warning{{TRUE}}
1399 void memset12_struct_field(void) {
1400 struct POD_memset pod;
1401 pod.num = 1;
1402 pod.c = '1';
1403 memset(&pod.c, 0, sizeof(struct POD_memset)); // expected-warning {{'memset' will always overflow; destination buffer has size 4, but size argument is 8}}
1404 clang_analyzer_eval(pod.num == 0); // expected-warning{{UNKNOWN}}
1405 clang_analyzer_eval(pod.c == 0); // expected-warning{{UNKNOWN}}
1408 union U_memset {
1409 int i;
1410 double d;
1411 char c;
1414 void memset13_union_field(void) {
1415 union U_memset u;
1416 u.i = 5;
1417 memset(&u.i, '\0', sizeof(union U_memset));
1418 // Note: This should be TRUE, analyzer can't handle union perfectly now.
1419 clang_analyzer_eval(u.d == 0); // expected-warning{{UNKNOWN}}
1421 #endif
1423 void memset14_region_cast(void) {
1424 char *str = (char *)malloc(10 * sizeof(int));
1425 int *array = (int *)str;
1426 memset(array, 0, 10 * sizeof(int));
1427 clang_analyzer_eval(str[10] == '\0'); // expected-warning{{TRUE}}
1428 clang_analyzer_eval(strlen((char *)array) == 0); // expected-warning{{TRUE}}
1429 clang_analyzer_eval(strlen(str) == 0); // expected-warning{{TRUE}}
1430 free(str);
1433 void memset15_region_cast(void) {
1434 char *str = (char *)malloc(10 * sizeof(int));
1435 int *array = (int *)str;
1436 memset(array, 0, 5 * sizeof(int));
1437 clang_analyzer_eval(str[10] == '\0'); // expected-warning{{UNKNOWN}}
1438 clang_analyzer_eval(strlen((char *)array) == 0); // expected-warning{{TRUE}}
1439 clang_analyzer_eval(strlen(str) == 0); // expected-warning{{TRUE}}
1440 free(str);
1443 int memset20_scalar(void) {
1444 int *x = malloc(sizeof(int));
1445 *x = 10;
1446 memset(x, 0, sizeof(int));
1447 int num = 1 / *x; // expected-warning{{Division by zero}}
1448 free(x);
1449 return num;
1452 int memset21_scalar(void) {
1453 int *x = malloc(sizeof(int));
1454 memset(x, 0, 1);
1455 int num = 1 / *x;
1456 free(x);
1457 return num;
1460 void memset22_array(void) {
1461 int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
1462 clang_analyzer_eval(array[1] == 2); // expected-warning{{TRUE}}
1463 memset(array, 0, sizeof(array));
1464 clang_analyzer_eval(array[1] == 0); // expected-warning{{TRUE}}
1467 void memset23_array_pod_object(void) {
1468 struct POD_memset array[10];
1469 array[1].num = 10;
1470 array[1].c = 'c';
1471 clang_analyzer_eval(array[1].num == 10); // expected-warning{{TRUE}}
1472 memset(&array[1], 0, sizeof(struct POD_memset));
1473 clang_analyzer_eval(array[1].num == 0); // expected-warning{{UNKNOWN}}
1476 void memset24_array_pod_object(void) {
1477 struct POD_memset array[10];
1478 array[1].num = 10;
1479 array[1].c = 'c';
1480 clang_analyzer_eval(array[1].num == 10); // expected-warning{{TRUE}}
1481 memset(array, 0, sizeof(array));
1482 clang_analyzer_eval(array[1].num == 0); // expected-warning{{TRUE}}
1485 void memset25_symbol(char c) {
1486 char array[10] = {1};
1487 if (c != 0)
1488 return;
1490 memset(array, c, 10);
1492 clang_analyzer_eval(strlen(array) == 0); // expected-warning{{TRUE}}
1493 clang_analyzer_eval(array[4] == 0); // expected-warning{{TRUE}}
1496 void memset26_upper_UCHAR_MAX(void) {
1497 char array[10] = {1};
1499 memset(array, 1024, 10);
1501 clang_analyzer_eval(strlen(array) == 0); // expected-warning{{TRUE}}
1502 clang_analyzer_eval(array[4] == 0); // expected-warning{{TRUE}}
1505 void bzero1_null(void) {
1506 char *a = NULL;
1508 bzero(a, 10); // expected-warning{{Null pointer passed as 1st argument to memory clearance function}}
1511 void bzero2_char_array_null(void) {
1512 char str[] = "abcd";
1513 clang_analyzer_eval(strlen(str) == 4); // expected-warning{{TRUE}}
1514 bzero(str, 2);
1515 clang_analyzer_eval(strlen(str) == 0); // expected-warning{{TRUE}}
1518 void bzero3_char_ptr_null(void) {
1519 char *str = "abcd";
1520 clang_analyzer_eval(strlen(str) == 4); // expected-warning{{TRUE}}
1521 bzero(str + 2, 2);
1522 clang_analyzer_eval(strlen(str) == 0); // expected-warning{{FALSE}}
1525 void explicit_bzero1_null(void) {
1526 char *a = NULL;
1528 explicit_bzero(a, 10); // expected-warning{{Null pointer passed as 1st argument to memory clearance function}}
1531 void explicit_bzero2_clear_mypassword(void) {
1532 char passwd[7] = "passwd";
1534 explicit_bzero(passwd, sizeof(passwd)); // no-warning
1536 clang_analyzer_eval(strlen(passwd) == 0); // expected-warning{{TRUE}}
1537 clang_analyzer_eval(passwd[0] == '\0'); // expected-warning{{TRUE}}
1540 void explicit_bzero3_out_ofbound(void) {
1541 char *privkey = (char *)malloc(7);
1542 const char newprivkey[10] = "mysafekey";
1544 strcpy(privkey, "random");
1545 explicit_bzero(privkey, sizeof(newprivkey));
1546 #ifndef SUPPRESS_OUT_OF_BOUND
1547 // expected-warning@-2 {{Memory clearance function overflows the destination buffer}}
1548 #endif
1549 clang_analyzer_eval(privkey[0] == '\0');
1550 #ifdef SUPPRESS_OUT_OF_BOUND
1551 // expected-warning@-2 {{UNKNOWN}}
1552 #endif
1553 free(privkey);
1556 //===----------------------------------------------------------------------===
1557 // FIXMEs
1558 //===----------------------------------------------------------------------===
1560 // The analyzer_eval call below should evaluate to true. We are being too
1561 // aggressive in marking the (length of) src symbol dead. The length of dst
1562 // depends on src. This could be explicitly specified in the checker or the
1563 // logic for handling MetadataSymbol in SymbolManager needs to change.
1564 void strcat_symbolic_src_length(char *src) {
1565 char dst[8] = "1234";
1566 strcat(dst, src);
1567 clang_analyzer_eval(strlen(dst) >= 4); // expected-warning{{UNKNOWN}}
1571 // The analyzer_eval call below should evaluate to true. Most likely the same
1572 // issue as the test above.
1573 void strncpy_exactly_matching_buffer2(char *y) {
1574 if (strlen(y) >= 4)
1575 return;
1577 char x[4];
1578 strncpy(x, y, 4); // no-warning
1580 // This time, we know that y fits in x anyway.
1581 clang_analyzer_eval(strlen(x) <= 3); // expected-warning{{UNKNOWN}}
1584 void memset7_char_array_nonnull(void) {
1585 char str[5] = "abcd";
1586 clang_analyzer_eval(strlen(str) == 4); // expected-warning{{TRUE}}
1587 memset(str, '0', 5);
1588 // FIXME: This should be TRUE.
1589 clang_analyzer_eval(str[0] == '0'); // expected-warning{{UNKNOWN}}
1590 clang_analyzer_eval(strlen(str) >= 5); // expected-warning{{TRUE}}
1593 void memset16_region_cast(void) {
1594 char *str = (char *)malloc(10 * sizeof(int));
1595 int *array = (int *)str;
1596 memset(array, '0', 10 * sizeof(int));
1597 // FIXME: This should be TRUE.
1598 clang_analyzer_eval(str[10] == '0'); // expected-warning{{UNKNOWN}}
1599 clang_analyzer_eval(strlen((char *)array) >= 10 * sizeof(int)); // expected-warning{{TRUE}}
1600 clang_analyzer_eval(strlen(str) >= 10 * sizeof(int)); // expected-warning{{TRUE}}
1601 free(str);
1604 #ifdef SUPPRESS_OUT_OF_BOUND
1605 void memset17_region_cast(void) {
1606 char *str = (char *)malloc(10 * sizeof(int));
1607 int *array = (int *)str;
1608 memset(array, '0', 12 * sizeof(int));
1609 clang_analyzer_eval(str[10] == '0'); // expected-warning{{UNKNOWN}}
1610 clang_analyzer_eval(strlen((char *)array) >= 12 * sizeof(int)); // expected-warning{{TRUE}}
1611 clang_analyzer_eval(strlen(str) >= 12 * sizeof(int)); // expected-warning{{TRUE}}
1612 free(str);
1615 void memset18_memset_multiple_times(void) {
1616 char *str = (char *)malloc(10 * sizeof(char));
1617 clang_analyzer_eval(strlen(str) == 0); // expected-warning{{UNKNOWN}}
1619 memset(str + 2, '\0', 10 * sizeof(char));
1620 clang_analyzer_eval(strlen(str) == 0); // expected-warning{{UNKNOWN}}
1621 clang_analyzer_eval(str[1] == '\0'); // expected-warning{{UNKNOWN}}
1623 memset(str, '0', 10 * sizeof(char));
1624 clang_analyzer_eval(strlen(str) >= 10); // expected-warning{{TRUE}}
1625 // FIXME: This should be TRUE.
1626 clang_analyzer_eval(str[1] == '0'); // expected-warning{{UNKNOWN}}
1628 free(str);
1631 void memset19_memset_multiple_times(void) {
1632 char *str = (char *)malloc(10 * sizeof(char));
1633 clang_analyzer_eval(strlen(str) == 0); // expected-warning{{UNKNOWN}}
1635 memset(str, '0', 10 * sizeof(char));
1636 clang_analyzer_eval(strlen(str) >= 10); // expected-warning{{TRUE}}
1637 // FIXME: This should be TRUE.
1638 clang_analyzer_eval(str[1] == '0'); // expected-warning{{UNKNOWN}}
1640 memset(str + 2, '\0', 10 * sizeof(char));
1641 clang_analyzer_eval(strlen(str) >= 10); // expected-warning{{UNKNOWN}}
1642 clang_analyzer_eval(str[1] == '0'); // expected-warning{{UNKNOWN}}
1644 free(str);
1646 #endif
1648 // The analyzer does not support binding a symbol with default binding.
1649 void memset27_symbol(char c) {
1650 char array[10] = {0};
1651 if (c < 10)
1652 return;
1654 memset(array, c, 10);
1656 clang_analyzer_eval(strlen(array) >= 10); // expected-warning{{TRUE}}
1657 // FIXME: This should be TRUE.
1658 clang_analyzer_eval(array[4] >= 10); // expected-warning{{UNKNOWN}}
1661 void memset28(void) {
1662 short x;
1663 memset(&x, 1, sizeof(short));
1664 // This should be true.
1665 clang_analyzer_eval(x == 0x101); // expected-warning{{UNKNOWN}}
1668 void memset29_plain_int_zero(void) {
1669 short x;
1670 memset(&x, 0, sizeof(short));
1671 clang_analyzer_eval(x == 0); // expected-warning{{TRUE}}
1674 void test_memset_chk(void) {
1675 int x;
1676 __builtin___memset_chk(&x, 0, sizeof(x), __builtin_object_size(&x, 0));
1677 clang_analyzer_eval(x == 0); // expected-warning{{TRUE}}
1680 #ifndef SUPPRESS_OUT_OF_BOUND
1681 void strcpy_no_overflow_2(char *y) {
1682 char x[3];
1683 // FIXME: string literal modeling does not account for embedded NULLs.
1684 // This case should not elicit a warning, but does.
1685 // See discussion at https://reviews.llvm.org/D129269
1686 strcpy(x, "12\0"); // expected-warning{{String copy function overflows the destination buffer}}
1688 #else
1689 void strcpy_no_overflow_2(char *y) {
1690 char x[3];
1691 strcpy(x, "12\0");
1693 #endif
1695 #ifndef SUPPRESS_OUT_OF_BOUND
1696 void testStrcpyDestinationWritableFirstByte(void) {
1697 char dst[10];
1698 char *p = dst - 8;
1699 strcpy(p, "src"); // expected-warning {{String copy function overflows the destination buffer}}
1702 void CWE124_Buffer_Underwrite__malloc_char_cpy() {
1703 char * dataBuffer = (char *)malloc(100*sizeof(char));
1704 if (dataBuffer == NULL) return;
1705 memset(dataBuffer, 'A', 100-1);
1706 dataBuffer[100-1] = '\0';
1707 char * data = dataBuffer - 8;
1708 char source[100];
1709 memset(source, 'C', 100-1); // fill with 'C's
1710 source[100-1] = '\0'; // null terminate
1711 strcpy(data, source); // expected-warning {{String copy function overflows the destination buffer}}
1712 free(dataBuffer);
1714 #endif
1716 #ifndef SUPPRESS_OUT_OF_BOUND
1717 void testStrncpyDestinationWritableFirstByte(void) {
1718 char source[100];
1719 use_string(source); // escape
1720 char buf[100];
1721 char *p = buf - 8;
1722 strncpy(p, source, 100-1); // expected-warning {{String copy function overflows the destination buffer}}
1725 void CWE124_Buffer_Underwrite__malloc_char_ncpy() {
1726 char * dataBuffer = (char *)malloc(100*sizeof(char));
1727 if (dataBuffer == 0) return;
1728 memset(dataBuffer, 'A', 100-1);
1729 dataBuffer[100-1] = '\0';
1730 char *data = dataBuffer - 8;
1732 char source[100];
1733 memset(source, 'C', 100-1); // fill with 'C's
1734 source[100-1] = '\0'; // null terminate
1735 strncpy(data, source, 100-1); // expected-warning {{String copy function overflows the destination buffer}}
1736 data[100-1] = '\0'; // null terminate
1737 free(dataBuffer);
1739 #endif
1741 #ifndef SUPPRESS_OUT_OF_BOUND
1742 void CWE124_Buffer_Underwrite__malloc_char_memcpy() {
1743 char * dataBuffer = (char *)malloc(100*sizeof(char));
1744 if (dataBuffer == NULL) return;
1745 memset(dataBuffer, 'A', 100-1);
1746 dataBuffer[100-1] = '\0';
1747 char *data = dataBuffer - 8;
1749 char source[100];
1750 memset(source, 'C', 100-1); // fill with 'C's
1751 source[100-1] = '\0'; // null terminate
1753 memcpy(data, source, 100*sizeof(char)); // expected-warning {{Memory copy function overflows the destination buffer}}
1754 data[100-1] = '\0';
1755 free(dataBuffer);
1757 #endif