1 /* PR middle-end/81384 - built-in form of strnlen missing
2
3 Since the strnlen patch affects the handling for strncmp and other
4 bounded functions, verify that a bound in excess of the maximum
5 object size specified for strncmp is diagnosed regardless of
6 attribute nonstring. Also verify that a bound that's greater than
7 the size of a non-string array is diagnosed, even if it's not in
8 excess of the maximum object size.
9
10 { dg-do compile }
11 { dg-options "-O2 -Wall -ftrack-macro-expansion=0" } */
12
13 #include "range.h"
14
15 extern int strncmp (const char*, const char*, size_t);
16
17 #define STR /* not non-string */
18 #define NS __attribute__ ((nonstring))
19
20 #define _CAT(s, n) s ## n
21 #define CAT(s, n) _CAT (s, n)
22 #define UNIQ(n) CAT (n, __LINE__)
23
24 void sink (int);
25
26 #define T(at1, N1, at2, N2, bound) \
27 do { \
28 extern at1 char UNIQ (a)[N1]; \
29 extern at2 char UNIQ (b)[N2]; \
30 sink (strncmp (UNIQ (a), UNIQ (b), bound)); \
31 } while (0)
32
strncmp_cst(void)33 void strncmp_cst (void)
34 {
35 size_t n = DIFF_MAX;
36
37 T (STR, /* [] */, STR, /* [] */, n);
38 T (STR, /* [] */, STR, /* [] */, n + 1); /* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size \[0-9\]+" } */
39
40 T (STR, 1, STR, /* [] */, n);
41 T (STR, 2, STR, /* [] */, n + 1); /* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size \[0-9\]+" } */
42
43 T (STR, /* [] */, STR, 3, n);
44 T (STR, /* [] */, STR, 4, n + 1); /* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size \[0-9\]+" } */
45
46 T (STR, /* [] */, NS, /* [] */, n);
47 T (STR, /* [] */, NS, /* [] */, n + 1); /* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size \[0-9\]+" } */
48
49 T (STR, 5, NS, /* [] */, n);
50 T (STR, 6, NS, /* [] */, n + 1); /* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size \[0-9\]+" } */
51
52 T (STR, /* [] */, NS, 7, n); /* { dg-warning "argument 2 declared attribute .nonstring. is smaller than the specified bound" } */
53
54 T (STR, /* [] */, NS, 8, n + 1); /* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size \[0-9\]+" } */
55
56 T (NS, /* [] */, STR, /* [] */, n);
57 T (NS, /* [] */, STR, /* [] */, n + 1); /* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size \[0-9\]+" } */
58
59 T (NS, 9, STR, /* [] */, n); /* { dg-warning "argument 1 declared attribute .nonstring. is smaller than the specified bound" } */
60 T (NS, 10, STR, /* [] */, n + 1); /* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size \[0-9\]+" } */
61
62 T (NS, /* [] */, STR, 11, n);
63 T (NS, /* [] */, STR, 12, n + 1); /* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size \[0-9\]+" } */
64
65 T (NS, /* [] */, NS, /* [] */, n);
66 T (NS, /* [] */, NS, /* [] */, n + 1); /* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size \[0-9\]+" } */
67
68 T (NS, 13, NS, /* [] */, n); /* { dg-warning "argument 1 declared attribute .nonstring. is smaller than the specified bound" } */
69 T (NS, 14, NS, /* [] */, n + 1); /* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size \[0-9\]+" } */
70
71 T (NS, /* [] */, NS, 15, n); /* { dg-warning "argument 2 declared attribute .nonstring. is smaller than the specified bound" } */
72 T (NS, /* [] */, NS, 16, n + 1); /* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size \[0-9\]+" } */
73 }
74
75
strncmp_range(void)76 void strncmp_range (void)
77 {
78 size_t n = DIFF_MAX;
79 n = UR (n, n + 1);
80
81 T (STR, /* [] */, STR, /* [] */, n);
82 T (STR, /* [] */, STR, /* [] */, n + 1); /* { dg-warning "specified bound \\\[\[0-9\]+, \[0-9\]+] exceeds maximum object size \[0-9\]+" } */
83
84 T (STR, 1, STR, /* [] */, n);
85 T (STR, 2, STR, /* [] */, n + 1); /* { dg-warning "specified bound \\\[\[0-9\]+, \[0-9\]+] exceeds maximum object size \[0-9\]+" } */
86
87 T (STR, /* [] */, STR, 3, n);
88 T (STR, /* [] */, STR, 4, n + 1); /* { dg-warning "specified bound \\\[\[0-9\]+, \[0-9\]+] exceeds maximum object size \[0-9\]+" } */
89
90 T (STR, /* [] */, NS, /* [] */, n);
91 T (STR, /* [] */, NS, /* [] */, n + 1); /* { dg-warning "specified bound \\\[\[0-9\]+, \[0-9\]+] exceeds maximum object size \[0-9\]+" } */
92
93 T (STR, 5, NS, /* [] */, n);
94 T (STR, 6, NS, /* [] */, n + 1); /* { dg-warning "specified bound \\\[\[0-9\]+, \[0-9\]+] exceeds maximum object size \[0-9\]+" } */
95
96 T (STR, /* [] */, NS, 7, n); /* { dg-warning "argument 2 declared attribute .nonstring. is smaller than the specified bound" } */
97
98 T (STR, /* [] */, NS, 8, n + 1); /* { dg-warning "specified bound \\\[\[0-9\]+, \[0-9\]+] exceeds maximum object size \[0-9\]+" } */
99
100 T (NS, /* [] */, STR, /* [] */, n);
101 T (NS, /* [] */, STR, /* [] */, n + 1); /* { dg-warning "specified bound \\\[\[0-9\]+, \[0-9\]+] exceeds maximum object size \[0-9\]+" } */
102
103 T (NS, 9, STR, /* [] */, n); /* { dg-warning "argument 1 declared attribute .nonstring. is smaller than the specified bound" } */
104 T (NS, 10, STR, /* [] */, n + 1); /* { dg-warning "specified bound \\\[\[0-9\]+, \[0-9\]+] exceeds maximum object size \[0-9\]+" } */
105
106 T (NS, /* [] */, STR, 11, n);
107 T (NS, /* [] */, STR, 12, n + 1); /* { dg-warning "specified bound \\\[\[0-9\]+, \[0-9\]+] exceeds maximum object size \[0-9\]+" } */
108
109 T (NS, /* [] */, NS, /* [] */, n);
110 T (NS, /* [] */, NS, /* [] */, n + 1); /* { dg-warning "specified bound \\\[\[0-9\]+, \[0-9\]+] exceeds maximum object size \[0-9\]+" } */
111
112 T (NS, 13, NS, /* [] */, n); /* { dg-warning "argument 1 declared attribute .nonstring. is smaller than the specified bound" } */
113 T (NS, 14, NS, /* [] */, n + 1); /* { dg-warning "specified bound \\\[\[0-9\]+, \[0-9\]+] exceeds maximum object size \[0-9\]+" } */
114
115 T (NS, /* [] */, NS, 15, n); /* { dg-warning "argument 2 declared attribute .nonstring. is smaller than the specified bound" } */
116 T (NS, /* [] */, NS, 16, n + 1); /* { dg-warning "specified bound \\\[\[0-9\]+, \[0-9\]+] exceeds maximum object size \[0-9\]+" } */
117 }
118