Debian Codegraph @

gcc/testsuite/gcc.dg/analyzer/strlen-1.c

raw
1 /* See e.g. https://en.cppreference.com/w/c/string/byte/strlen */
2
3 #include "analyzer-decls.h"
4
5 typedef __SIZE_TYPE__ size_t;
6
7 static size_t __attribute__((noinline))
8 call_strlen_1 (const char *p)
9 {
10 return __builtin_strlen (p);
11 }
12
13 void test_string (void)
14 {
15 __analyzer_eval (call_strlen_1 ("abc") == 3); /* { dg-warning "TRUE" } */
16 }
17
18 static size_t __attribute__((noinline))
19 call_strlen_2 (const char *p)
20 {
21 return __builtin_strlen (p); /* { dg-warning "stack-based buffer over-read" } */
22 }
23
24 void test_unterminated (void)
25 {
26 const char buf[3] = "abc";
27 __analyzer_eval (call_strlen_2 (buf) == 3); /* { dg-warning "UNKNOWN" } */
28 }
29
30 void test_uninitialized (void)
31 {
32 char buf[16];
33 __builtin_strlen (buf); /* { dg-warning "use of uninitialized value 'buf\\\[0\\\]'" } */
34 }
35
36 void test_partially_initialized (void)
37 {
38 char buf[16];
39 buf[0] = 'a';
40 __builtin_strlen (buf); /* { dg-warning "use of uninitialized value 'buf\\\[1\\\]'" } */
41 }
42
43 extern size_t strlen (const char *str);
44
45 size_t
46 test_passthrough (const char *str)
47 {
48 return strlen (str);
49 }
50
51 /* TODO
52 - complain if NULL str
53 - should it be tainted if str's bytes are tainted?
54 */
55
56 static size_t __attribute__((noinline))
57 call_strlen_3 (const char *p)
58 {
59 return __builtin_strlen (p);
60 }
61
62 void test_array_initialization_from_shorter_literal (void)
63 {
64 const char buf[10] = "abc";
65 __analyzer_eval (call_strlen_3 (buf) == 3); /* { dg-warning "TRUE" } */
66 __analyzer_eval (call_strlen_3 (buf + 5) == 0); /* { dg-warning "TRUE" } */
67 __analyzer_eval (buf[5] == 0); /* { dg-warning "TRUE" } */
68 }
69
70 static size_t __attribute__((noinline))
71 call_strlen_4 (const char *p)
72 {
73 return __builtin_strlen (p); /* { dg-warning "stack-based buffer over-read" } */
74 }
75
76 char test_array_initialization_from_longer_literal (void)
77 {
78 const char buf[3] = "abcdefg"; /* { dg-warning "initializer-string for array of 'char' is too long" } */
79 __analyzer_eval (call_strlen_4 (buf) == 3); /* { dg-warning "UNKNOWN" } */
80 return buf[5]; /* { dg-warning "stack-based buffer over-read" } */
81 }
82
83 static size_t __attribute__((noinline))
84 call_strlen_5 (const char *p)
85 {
86 return __builtin_strlen (p);
87 }
88
89 static size_t __attribute__((noinline))
90 call_strlen_5a (const char *p)
91 {
92 return __builtin_strlen (p); /* { dg-warning "stack-based buffer over-read" } */
93 }
94
95 char test_array_initialization_implicit_length (void)
96 {
97 const char buf[] = "abc";
98 __analyzer_eval (call_strlen_5 (buf) == 3); /* { dg-warning "TRUE" } */
99 __analyzer_eval (call_strlen_5 (buf + 2) == 1); /* { dg-warning "TRUE" } */
100 __analyzer_eval (call_strlen_5 (buf + 3) == 0); /* { dg-warning "TRUE" } */
101 __analyzer_eval (buf[0] == 'a'); /* { dg-warning "TRUE" } */
102 __analyzer_eval (buf[3] == 0); /* { dg-warning "TRUE" } */
103 __analyzer_eval (call_strlen_5a (buf + 4) == 0); /* { dg-warning "UNKNOWN" } */
104 return buf[4]; /* { dg-warning "stack-based buffer over-read" } */
105 }
106