1 /* PR middle-end/81384 - built-in form of strnlen missing
3 { dg-options "-O2 -Wall -ftrack-macro-expansion=0" } */
7 extern void* memcpy (void*, const void*, size_t);
8 extern size_t strnlen (const char*, size_t);
10 #define NONSTRING __attribute__ ((nonstring))
12 #define _CAT(s, n) s ## n
13 #define CAT(s, n) _CAT (s, n)
14 #define UNIQ(n) CAT (n, __LINE__)
16 void sink (size_t, ...);
18 #define T(expr) sink (expr)
20 void test_strnlen_array_cst (void)
22 NONSTRING
char ns3
[3];
23 sink (0, ns3
); // "initialize" ns3
29 T (strnlen (ns3
, 4)); /* { dg-warning "argument 1 declared attribute .nonstring. is smaller than the specified bound 4" } */
30 T (strnlen (ns3
, DIFF_MAX
)); /* { dg-warning "argument 1 declared attribute .nonstring. is smaller than the specified bound \[0-9\]+" } */
31 T (strnlen (ns3
, SIZE_MAX
)); /* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size \[0-9\]+" } */
33 NONSTRING
char ns5
[5];
40 T (strnlen (ns5
, 6)); /* { dg-warning "argument 1 declared attribute .nonstring. is smaller than the specified bound 6" } */
41 T (strnlen (ns5
, DIFF_MAX
)); /* { dg-warning "argument 1 declared attribute .nonstring. is smaller than the specified bound \[0-9\]+" } */
42 T (strnlen (ns5
, SIZE_MAX
)); /* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size \[0-9\]+" } */
46 void test_strnlen_array_range (void)
48 NONSTRING
char ns3
[3];
49 sink (0, ns3
); // "initialize" ns3
51 T (strnlen (ns3
, UR (0, 3)));
52 T (strnlen (ns3
, UR (0, 9)));
53 T (strnlen (ns3
, UR (3, 4)));
54 T (strnlen (ns3
, UR (3, DIFF_MAX
)));
55 T (strnlen (ns3
, UR (4, 5))); /* { dg-warning "argument 1 declared attribute .nonstring. is smaller than the specified bound \\\[4, 5]" } */
56 T (strnlen (ns3
, UR (DIFF_MAX
, SIZE_MAX
))); /* { dg-warning "argument 1 declared attribute .nonstring. is smaller " } */
61 #define T(N, init, nelts, bound) \
63 extern NONSTRING char UNIQ (arr)[N]; \
64 memcpy (UNIQ (arr), init, nelts); \
65 sink (strnlen (UNIQ (arr), bound), UNIQ (arr)); \
68 void test_strnlen_string_cst (void)
76 T (3, "123", 3, 4); /* { dg-warning "argument 1 declared attribute .nonstring. is smaller than the specified bound 4" "bug 86688" { xfail *-*-* } } */
77 T (3, "123", 3, 9); /* { dg-warning "argument 1 declared attribute .nonstring. is smaller than the specified bound 9" "bug 86688" { xfail *-*-* } } */
87 T (5, "123", 3, 6); /* { dg-warning "argument 1 declared attribute .nonstring. is smaller than the specified bound 6" } */
89 /* Strnlen shouldn't trigger a warning for arrays of unknown size
90 (except for accesses to uninitialized elements when those are
92 T (/* [] */, "1", 1, 1);
93 T (/* [] */, "1", 1, 2);
94 T (/* [] */, "1", 2, 1);
95 T (/* [] */, "1", 2, 2);
96 T (/* [] */, "1", 2, 3);
97 T (/* [] */, "1", 2, 9);
98 T (/* [] */, "1", 2, DIFF_MAX
);
99 T (/* [] */, "1", 2, SIZE_MAX
);
102 T (/* [] */, "123", 3, n
);
103 T (/* [] */, "123", 3, n
+ 1); /* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size " } */
105 T (/* [] */, "123", 3, n
); /* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size " } */
109 void test_strnlen_string_range (void)
111 T (3, "1", 2, UR (0, 1));
112 T (3, "1", 2, UR (3, 9));
113 T (3, "123", 3, UR (4, 5)); /* { dg-warning "argument 1 declared attribute .nonstring. is smaller than the specified bound \\\[4, 5]" "bug 86688" { xfail *-*-* } } */
114 T (3, "123", 3, UR (5, 9)); /* { dg-warning "argument 1 declared attribute .nonstring. is smaller than the specified bound \\\[5, 9]" "bug 86688" { xfail *-*-* } } */