Debian Codegraph @

clang/test/Analysis/string.c

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