Debian Codegraph @

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

raw
1 #include <string.h>
2 #include "analyzer-decls.h"
3
4 const char* test_literal (int x)
5 {
6 char *p = __builtin_strchr ("123", x); /* { dg-message "when '__builtin_strchr' returns non-NULL" } */
7 if (p)
8 {
9 __analyzer_eval (*p == x); /* { dg-message "UNKNOWN" } */
10 /* TODO: this ought to be TRUE, but it's unclear that it's
11 worth stashing this constraint. */
12 *p = 'A'; /* { dg-warning "write to string literal" } */
13 }
14 return p;
15 }
16
17 void test_2 (const char *s, int c)
18 {
19 char *p = __builtin_strchr (s, c); /* { dg-message "when '__builtin_strchr' returns NULL" } */
20 *p = 'A'; /* { dg-warning "dereference of NULL 'p'" "null deref" } */
21 }
22
23 void test_3 (const char *s, int c)
24 {
25 char *p = strchr (s, c); /* { dg-message "when 'strchr' returns NULL" } */
26 *p = 'A'; /* { dg-warning "dereference of NULL 'p'" "null deref" } */
27 }
28
29 void test_unterminated (int c)
30 {
31 char buf[3] = "abc";
32 strchr (buf, c); /* { dg-warning "stack-based buffer over-read" } */
33 /* { dg-message "while looking for null terminator for argument 1 \\('&buf'\\) of 'strchr'..." "event" { target *-*-* } .-1 } */
34 }
35
36 void test_uninitialized (int c)
37 {
38 char buf[16];
39 strchr (buf, c); /* { dg-warning "use of uninitialized value 'buf\\\[0\\\]'" } */
40 /* { dg-message "while looking for null terminator for argument 1 \\('&buf'\\) of 'strchr'..." "event" { target *-*-* } .-1 } */
41 }