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
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 //===----------------------------------------------------------------------===
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.
61 # define BUILTIN(f) __builtin_ ## f
62 #else /* USE_BUILTINS */
64 #endif /* USE_BUILTINS */
67 typedef typeof(sizeof(int)) size_t;
69 void clang_analyzer_eval(int);
71 int scanf(const char *restrict format
, ...);
74 void *memcpy(void *restrict dest
, const void *restrict src
, size_t n
);
76 //===----------------------------------------------------------------------===
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
) {
94 clang_analyzer_eval(strlen(a
) == 3); // expected-warning{{TRUE}}
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) {
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
;
143 size_t a
= strlen(z
.a
);
145 size_t b
= strlen(z
.a
);
147 clang_analyzer_eval(b
== 0); // expected-warning{{TRUE}}
151 size_t c
= strlen(z
.a
);
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
);
161 clang_analyzer_eval(b
== 0); // expected-warning{{TRUE}}
165 size_t c
= strlen(x
);
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
);
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.
183 size_t c
= strlen(global_str
);
185 clang_analyzer_eval(c
== 0); // expected-warning{{UNKNOWN}}
188 void strlen_indirect(char *x
) {
189 size_t a
= strlen(x
);
192 size_t b
= strlen(x
);
194 clang_analyzer_eval(b
== 0); // expected-warning{{TRUE}}
196 extern void use_string_ptr(char*const*);
199 size_t c
= strlen(x
);
201 clang_analyzer_eval(c
== 0); // expected-warning{{UNKNOWN}}
204 void strlen_indirect2(char *x
) {
205 size_t a
= strlen(x
);
208 extern void use_string_ptr2(char**);
211 size_t c
= strlen(x
);
213 clang_analyzer_eval(c
== 0); // expected-warning{{UNKNOWN}}
216 void strlen_liveness(const char *x
) {
219 clang_analyzer_eval(strlen(x
) < 5); // expected-warning{{FALSE}}
223 size_t strlenWrapper(const char *str
) {
227 extern void invalidate(char *s
);
229 void testStrlenCallee(void) {
232 size_t lenBefore
= strlenWrapper(str
);
234 size_t lenAfter
= strlenWrapper(str
);
235 clang_analyzer_eval(lenBefore
== lenAfter
); // expected-warning{{UNKNOWN}}
239 //===----------------------------------------------------------------------===
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
) {
256 clang_analyzer_eval(strnlen(a
, 10) == 3); // expected-warning{{TRUE}}
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
) {
272 clang_analyzer_eval(strnlen(a
, 3) == 3); // expected-warning{{TRUE}}
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) {
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.
322 clang_analyzer_eval(len
== 0); // expected-warning{{TRUE}}
324 clang_analyzer_eval(len
== 3); // expected-warning{{UNKNOWN}}
325 clang_analyzer_eval(len
< 3); // expected-warning{{UNKNOWN}}
329 //===----------------------------------------------------------------------===
331 //===----------------------------------------------------------------------===
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)
342 #define strcpy BUILTIN(strcpy)
343 char *strcpy(char *restrict s1
, const char *restrict s2
);
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
) {
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
) {
380 strcpy(x
, y
); // expected-warning{{String copy function overflows the destination buffer}}
384 void strcpy_no_overflow(char *y
) {
387 strcpy(x
, y
); // no-warning
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
399 char f(char ***c
, int *i
) {
401 return (**c
)[0]; // no-crash
404 //===----------------------------------------------------------------------===
406 //===----------------------------------------------------------------------===
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)
417 #define stpcpy BUILTIN(stpcpy)
418 char *stpcpy(char *restrict s1
, const char *restrict s2
);
423 void stpcpy_effect(char *x
, char *y
) {
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
) {
435 stpcpy(x
, y
); // expected-warning{{String copy function overflows the destination buffer}}
439 void stpcpy_no_overflow(char *y
) {
442 stpcpy(x
, y
); // no-warning
445 //===----------------------------------------------------------------------===
447 //===----------------------------------------------------------------------===
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)
458 #define strcat BUILTIN(strcat)
459 char *strcat(char *restrict s1
, const char *restrict s2
);
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
) {
478 size_t orig_len
= strlen(x
);
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
) {
492 strcat(x
, y
); // expected-warning{{String concatenation function overflows the destination buffer}}
495 void strcat_overflow_1(char *y
) {
498 strcat(x
, y
); // expected-warning{{String concatenation function overflows the destination buffer}}
501 void strcat_overflow_2(char *y
) {
504 strcat(x
, y
); // expected-warning{{String concatenation function overflows the destination buffer}}
508 void strcat_no_overflow(char *y
) {
511 strcat(x
, y
); // no-warning
514 void strcat_symbolic_dst_length(char *dst
) {
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.
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))
538 if (strlen(src
) != 2)
544 //===----------------------------------------------------------------------===
546 //===----------------------------------------------------------------------===
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)
557 #define strncpy BUILTIN(strncpy)
558 char *strncpy(char *restrict s1
, const char *restrict s2
, size_t n
);
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
) {
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) {
588 strncpy(p
, "AAA", sizeof("AAA"));
589 // expected-warning@-1 {{String copy function overflows the destination buffer}}
593 void strncpy_overflow(char *y
) {
597 // expected-warning@-1 {{String copy function overflows the destination buffer}}
599 // expected-warning@-3 {{size argument is too large; destination buffer has size 4, but size argument is 5}}
603 void strncpy_no_overflow(char *y
) {
607 // expected-warning@-1 {{String copy function overflows the destination buffer}}
609 // expected-warning@-3 {{size argument is too large; destination buffer has size 4, but size argument is 5}}
613 void strncpy_no_overflow2(char *y
, int n
) {
620 // expected-warning@-1 {{String copy function overflows the destination buffer}}
624 void strncpy_truncate(char *y
) {
627 strncpy(x
, y
, 3); // no-warning
630 void strncpy_no_truncate(char *y
) {
633 strncpy(x
, y
, 3); // no-warning
636 void strncpy_exactly_matching_buffer(char *y
) {
638 strncpy(x
, y
, 4); // no-warning
640 // strncpy does not null-terminate, so we have no idea what the strlen is
642 clang_analyzer_eval(strlen(x
) > 4); // expected-warning{{UNKNOWN}}
645 void strncpy_zero(char *src
) {
647 strncpy(dst
, src
, 0); // no-warning
650 void strncpy_empty(void) {
653 strncpy(dst
, src
, 4); // no-warning
656 //===----------------------------------------------------------------------===
658 //===----------------------------------------------------------------------===
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)
669 #define strncat BUILTIN(strncat)
670 char *strncat(char *restrict s1
, const char *restrict s2
, size_t n
);
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
) {
689 size_t orig_len
= strlen(x
);
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
) {
703 strncat(x
, y
, strlen(y
));
704 // expected-warning@-1 {{String concatenation function overflows the destination buffer}}
707 void strncat_overflow_1(char *y
) {
710 strncat(x
, y
, strlen(y
));
711 // expected-warning@-1 {{String concatenation function overflows the destination buffer}}
714 void strncat_overflow_2(char *y
) {
717 strncat(x
, y
, strlen(y
));
718 // expected-warning@-1 {{String concatenation function overflows the destination buffer}}
721 void strncat_overflow_3(char *y
) {
725 // expected-warning@-1 {{String concatenation function overflows the destination buffer}}
729 void strncat_no_overflow_1(char *y
) {
732 strncat(x
, y
, strlen(y
)); // no-warning
735 void strncat_no_overflow_2(char *y
) {
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}}
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";
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";
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))
793 if (strlen(src
) != 2)
795 strncat(dst
, src
, 2);
798 void strncat_zero(char *src
) {
800 strncat(dst
, src
, 0); // no-warning
803 void strncat_empty(void) {
806 strncat(dst
, src
, 4); // no-warning
809 //===----------------------------------------------------------------------===
811 //===----------------------------------------------------------------------===
813 #define strcmp BUILTIN(strcmp)
814 int strcmp(const char * s1
, const char * s2
);
816 void strcmp_check_modelling(void) {
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) {
834 clang_analyzer_eval(strcmp(x
, "123") == 0); // expected-warning{{TRUE}}
837 void strcmp_constant_and_var_1(void) {
839 clang_analyzer_eval(strcmp("123", x
) == 0); // expected-warning{{TRUE}}
842 void strcmp_0(void) {
845 clang_analyzer_eval(strcmp(x
, y
) == 0); // expected-warning{{TRUE}}
848 void strcmp_1(void) {
851 clang_analyzer_eval(strcmp(x
, y
) > 0); // expected-warning{{TRUE}}
854 void strcmp_2(void) {
857 clang_analyzer_eval(strcmp(x
, y
) < 0); // expected-warning{{TRUE}}
860 void strcmp_null_0(void) {
863 strcmp(x
, y
); // expected-warning{{Null pointer passed as 1st argument to string comparison function}}
866 void strcmp_null_1(void) {
869 strcmp(x
, y
); // expected-warning{{Null pointer passed as 2nd argument to string comparison function}}
872 void strcmp_diff_length_0(void) {
875 clang_analyzer_eval(strcmp(x
, y
) < 0); // expected-warning{{TRUE}}
878 void strcmp_diff_length_1(void) {
881 clang_analyzer_eval(strcmp(x
, y
) < 0); // expected-warning{{TRUE}}
884 void strcmp_diff_length_2(void) {
887 clang_analyzer_eval(strcmp(x
, y
) > 0); // expected-warning{{TRUE}}
890 void strcmp_diff_length_3(void) {
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}}
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
;
918 int strcmp_null_argument(char *a
) {
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 //===----------------------------------------------------------------------===
926 //===----------------------------------------------------------------------===
928 #define strncmp BUILTIN(strncmp)
929 int strncmp(const char *s1
, const char *s2
, size_t n
);
931 void strncmp_check_modelling(void) {
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) {
949 clang_analyzer_eval(strncmp(x
, "123", 3) == 0); // expected-warning{{TRUE}}
952 void strncmp_constant_and_var_1(void) {
954 clang_analyzer_eval(strncmp("123", x
, 3) == 0); // expected-warning{{TRUE}}
957 void strncmp_0(void) {
960 clang_analyzer_eval(strncmp(x
, y
, 3) == 0); // expected-warning{{TRUE}}
963 void strncmp_1(void) {
966 clang_analyzer_eval(strncmp(x
, y
, 3) > 0); // expected-warning{{TRUE}}
969 void strncmp_2(void) {
972 clang_analyzer_eval(strncmp(x
, y
, 3) < 0); // expected-warning{{TRUE}}
975 void strncmp_null_0(void) {
978 strncmp(x
, y
, 3); // expected-warning{{Null pointer passed as 1st argument to string comparison function}}
981 void strncmp_null_1(void) {
984 strncmp(x
, y
, 3); // expected-warning{{Null pointer passed as 2nd argument to string comparison function}}
987 void strncmp_diff_length_0(void) {
990 clang_analyzer_eval(strncmp(x
, y
, 5) < 0); // expected-warning{{TRUE}}
993 void strncmp_diff_length_1(void) {
996 clang_analyzer_eval(strncmp(x
, y
, 5) < 0); // expected-warning{{TRUE}}
999 void strncmp_diff_length_2(void) {
1002 clang_analyzer_eval(strncmp(x
, y
, 5) > 0); // expected-warning{{TRUE}}
1005 void strncmp_diff_length_3(void) {
1008 clang_analyzer_eval(strncmp(x
, y
, 5) < 0); // expected-warning{{TRUE}}
1011 void strncmp_diff_length_4(void) {
1014 clang_analyzer_eval(strncmp(x
, y
, 3) == 0); // expected-warning{{TRUE}}
1017 void strncmp_diff_length_5(void) {
1020 clang_analyzer_eval(strncmp(x
, y
, 3) < 0); // expected-warning{{TRUE}}
1023 void strncmp_diff_length_6(void) {
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
) {
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 //===----------------------------------------------------------------------===
1041 //===----------------------------------------------------------------------===
1043 #define strcasecmp BUILTIN(strcasecmp)
1044 int strcasecmp(const char *s1
, const char *s2
);
1046 void strcasecmp_check_modelling(void) {
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) {
1064 clang_analyzer_eval(strcasecmp(x
, "Abc") == 0); // expected-warning{{TRUE}}
1067 void strcasecmp_constant_and_var_1(void) {
1069 clang_analyzer_eval(strcasecmp("Abc", x
) == 0); // expected-warning{{TRUE}}
1072 void strcasecmp_0(void) {
1075 clang_analyzer_eval(strcasecmp(x
, y
) == 0); // expected-warning{{TRUE}}
1078 void strcasecmp_1(void) {
1081 clang_analyzer_eval(strcasecmp(x
, y
) > 0); // expected-warning{{TRUE}}
1084 void strcasecmp_2(void) {
1087 clang_analyzer_eval(strcasecmp(x
, y
) < 0); // expected-warning{{TRUE}}
1090 void strcasecmp_null_0(void) {
1093 strcasecmp(x
, y
); // expected-warning{{Null pointer passed as 1st argument to string comparison function}}
1096 void strcasecmp_null_1(void) {
1099 strcasecmp(x
, y
); // expected-warning{{Null pointer passed as 2nd argument to string comparison function}}
1102 void strcasecmp_diff_length_0(void) {
1105 clang_analyzer_eval(strcasecmp(x
, y
) < 0); // expected-warning{{TRUE}}
1108 void strcasecmp_diff_length_1(void) {
1111 clang_analyzer_eval(strcasecmp(x
, y
) < 0); // expected-warning{{TRUE}}
1114 void strcasecmp_diff_length_2(void) {
1117 clang_analyzer_eval(strcasecmp(x
, y
) > 0); // expected-warning{{TRUE}}
1120 void strcasecmp_diff_length_3(void) {
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
) {
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 //===----------------------------------------------------------------------===
1138 //===----------------------------------------------------------------------===
1140 #define strncasecmp BUILTIN(strncasecmp)
1141 int strncasecmp(const char *s1
, const char *s2
, size_t n
);
1143 void strncasecmp_check_modelling(void) {
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) {
1161 clang_analyzer_eval(strncasecmp(x
, "Abc", 3) == 0); // expected-warning{{TRUE}}
1164 void strncasecmp_constant_and_var_1(void) {
1166 clang_analyzer_eval(strncasecmp("Abc", x
, 3) == 0); // expected-warning{{TRUE}}
1169 void strncasecmp_0(void) {
1172 clang_analyzer_eval(strncasecmp(x
, y
, 3) == 0); // expected-warning{{TRUE}}
1175 void strncasecmp_1(void) {
1178 clang_analyzer_eval(strncasecmp(x
, y
, 3) > 0); // expected-warning{{TRUE}}
1181 void strncasecmp_2(void) {
1184 clang_analyzer_eval(strncasecmp(x
, y
, 3) < 0); // expected-warning{{TRUE}}
1187 void strncasecmp_null_0(void) {
1190 strncasecmp(x
, y
, 3); // expected-warning{{Null pointer passed as 1st argument to string comparison function}}
1193 void strncasecmp_null_1(void) {
1196 strncasecmp(x
, y
, 3); // expected-warning{{Null pointer passed as 2nd argument to string comparison function}}
1199 void strncasecmp_diff_length_0(void) {
1202 clang_analyzer_eval(strncasecmp(x
, y
, 5) < 0); // expected-warning{{TRUE}}
1205 void strncasecmp_diff_length_1(void) {
1208 clang_analyzer_eval(strncasecmp(x
, y
, 5) < 0); // expected-warning{{TRUE}}
1211 void strncasecmp_diff_length_2(void) {
1214 clang_analyzer_eval(strncasecmp(x
, y
, 5) > 0); // expected-warning{{TRUE}}
1217 void strncasecmp_diff_length_3(void) {
1220 clang_analyzer_eval(strncasecmp(x
, y
, 5) < 0); // expected-warning{{TRUE}}
1223 void strncasecmp_diff_length_4(void) {
1226 clang_analyzer_eval(strncasecmp(x
, y
, 3) == 0); // expected-warning{{TRUE}}
1229 void strncasecmp_diff_length_5(void) {
1232 clang_analyzer_eval(strncasecmp(x
, y
, 3) < 0); // expected-warning{{TRUE}}
1235 void strncasecmp_diff_length_6(void) {
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
) {
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 //===----------------------------------------------------------------------===
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
) {
1267 char *result
= strsep(&s
, ""); // no-warning
1268 clang_analyzer_eval(original
== result
); // expected-warning{{TRUE}}
1271 void strsep_null_string(void) {
1273 char *result
= strsep(&s
, ""); // no-warning
1274 clang_analyzer_eval(result
== NULL
); // expected-warning{{TRUE}}
1277 void strsep_changes_input_pointer(char *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.
1285 clang_analyzer_eval(s
== NULL
); // expected-warning{{TRUE}}
1289 void strsep_changes_input_string(void) {
1292 clang_analyzer_eval(str
[1] == 'b'); // expected-warning{{TRUE}}
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
);
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}}
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}}
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}}
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}}
1378 void memset10_struct(void) {
1379 struct POD_memset pod
;
1380 char *str
= (char *)&pod
;
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
;
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
;
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}}
1414 void memset13_union_field(void) {
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}}
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}}
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}}
1443 int memset20_scalar(void) {
1444 int *x
= malloc(sizeof(int));
1446 memset(x
, 0, sizeof(int));
1447 int num
= 1 / *x
; // expected-warning{{Division by zero}}
1452 int memset21_scalar(void) {
1453 int *x
= malloc(sizeof(int));
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];
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];
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};
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) {
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}}
1515 clang_analyzer_eval(strlen(str
) == 0); // expected-warning{{TRUE}}
1518 void bzero3_char_ptr_null(void) {
1520 clang_analyzer_eval(strlen(str
) == 4); // expected-warning{{TRUE}}
1522 clang_analyzer_eval(strlen(str
) == 0); // expected-warning{{FALSE}}
1525 void explicit_bzero1_null(void) {
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}}
1549 clang_analyzer_eval(privkey
[0] == '\0');
1550 #ifdef SUPPRESS_OUT_OF_BOUND
1551 // expected-warning@-2 {{UNKNOWN}}
1556 //===----------------------------------------------------------------------===
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";
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
) {
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}}
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}}
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}}
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}}
1648 // The analyzer does not support binding a symbol with default binding.
1649 void memset27_symbol(char c
) {
1650 char array
[10] = {0};
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) {
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) {
1670 memset(&x
, 0, sizeof(short));
1671 clang_analyzer_eval(x
== 0); // expected-warning{{TRUE}}
1674 void test_memset_chk(void) {
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
) {
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}}
1689 void strcpy_no_overflow_2(char *y
) {
1695 #ifndef SUPPRESS_OUT_OF_BOUND
1696 void testStrcpyDestinationWritableFirstByte(void) {
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;
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}}
1716 #ifndef SUPPRESS_OUT_OF_BOUND
1717 void testStrncpyDestinationWritableFirstByte(void) {
1719 use_string(source
); // escape
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;
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
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;
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}}