Debian Codegraph @

gcc/testsuite/c-c++-common/analyzer/attr-null_terminated_string_arg-nullable-sized.c

raw
1 /* { dg-additional-options "-Wno-stringop-overread" } */
2 /* { dg-additional-options "-fpermissive" { target c++ } } */
3
4 #include "../../gcc.dg/analyzer/analyzer-decls.h"
5
6 char *example_fn (const char *p, __SIZE_TYPE__ n) /* { dg-message "argument 1 of '\[^\n\r\]*' must be a pointer to a null-terminated string" } */
7 __attribute__((null_terminated_string_arg (1)))
8 __attribute__((access (read_only, 1, 2)));
9 // can be NULL
10
11 char *
12 test_passthrough (const char* str, __SIZE_TYPE__ n)
13 {
14 return example_fn (str, n);
15 }
16
17 char *
18 test_NULL_str_a (void)
19 {
20 return example_fn (NULL, 0); /* { dg-bogus "use of NULL where non-null expected" } */
21 }
22
23 char *
24 test_NULL_str_b (void)
25 {
26 return example_fn (NULL, 4); /* { dg-bogus "use of NULL where non-null expected" } */
27 }
28
29 char *
30 test_unterminated_str (void)
31 {
32 char str[3] = "abc"; /* { dg-warning "initializer-string for '\[^\n\]*' is too long" "" { target c++ } } */
33 return example_fn (str, 4); /* { dg-warning "stack-based buffer over-read" } */
34 }
35
36 char *
37 test_unterminated_str_truncated (void)
38 {
39 char str[3] = "abc"; /* { dg-warning "initializer-string for '\[^\n\]*' is too long" "" { target c++ } } */
40 return example_fn (str, 3); /* { dg-bogus "stack-based buffer over-read" } */
41 }
42
43 char *
44 test_uninitialized_str_a (void)
45 {
46 char str[16];
47 return example_fn (str, 1); /* { dg-warning "use of uninitialized value 'str\\\[0\\\]'" } */
48 }
49
50 char *
51 test_uninitialized_str_b (void)
52 {
53 char str[16];
54 return example_fn (str, 16); /* { dg-warning "use of uninitialized value 'str\\\[0\\\]'" } */
55 }
56
57 char *
58 test_uninitialized_str_c (void)
59 {
60 char str[16];
61 return example_fn (str, 17); /* { dg-warning "use of uninitialized value 'str\\\[0\\\]'" } */
62 }
63
64 char *
65 test_uninitialized_str_truncated (void)
66 {
67 char str[16];
68 return example_fn (str, 0); /* { dg-bogus "use of uninitialized value" } */
69 }