Debian Codegraph @

gcc/testsuite/gcc.dg/analyzer/strcpy-4.c

raw
1 /* { dg-additional-options "-Wno-stringop-overflow" } */
2
3 #include <string.h>
4 #include "analyzer-decls.h"
5
6 void
7 test_fixed_size_stack_1 (void)
8 {
9 char buf[3];
10 strcpy (buf, "abc"); /* { dg-warning "stack-based buffer overflow" } */
11 }
12
13 char *test_fixed_size_heap_1 (void)
14 {
15 char str[] = "abc";
16 char *p = __builtin_malloc (3);
17 if (!p)
18 return NULL;
19 strcpy (p, str); /* { dg-warning "heap-based buffer overflow" } */
20 return p;
21 }
22
23 char *test_fixed_size_heap_2_invalid (void)
24 {
25 char str[] = "abc";
26 char *p = __builtin_malloc (strlen (str));
27 if (!p)
28 return NULL;
29 strcpy (p, str); /* { dg-warning "heap-based buffer overflow" } */
30 return p;
31 }
32
33 char *test_fixed_size_heap_2_valid (void)
34 {
35 char str[] = "abc";
36 char *p = __builtin_malloc (strlen (str) + 1);
37 if (!p)
38 return NULL;
39 strcpy (p, str); /* { dg-bogus "" } */
40 __analyzer_eval (strlen (p) == 3); /* { dg-warning "TRUE" } */
41 return p;
42 }
43
44 char *test_dynamic_size_heap_1 (const char *src)
45 {
46 char *p = __builtin_malloc (strlen (src));
47 if (!p)
48 return NULL;
49 strcpy (p, src); // TODO: write of null terminator is oob
50 return p;
51 }