PR middle-end/85602 - -Wsizeof-pointer-memaccess for strncat with size of source
[official-gcc.git] / gcc / testsuite / gcc.dg / attr-nonstring-2.c
blobef2144d62075fdafbd5568a804e0c45844afa119
1 /* PR middle-end/81384 - built-in form of strnlen missing
2 { dg-do compile }
3 { dg-options "-O2 -Wall -ftrack-macro-expansion=0" } */
5 #include "range.h"
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
25 T (strnlen (ns3, 0));
26 T (strnlen (ns3, 1));
27 T (strnlen (ns3, 2));
28 T (strnlen (ns3, 3));
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];
34 sink (0, ns5);
36 T (strnlen (ns5, 0));
37 T (strnlen (ns5, 1));
38 T (strnlen (ns5, 2));
39 T (strnlen (ns5, 3));
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 " } */
60 #undef T
61 #define T(N, init, nelts, bound) \
62 do { \
63 extern NONSTRING char UNIQ (arr)[N]; \
64 memcpy (UNIQ (arr), init, nelts); \
65 sink (strnlen (UNIQ (arr), bound), UNIQ (arr)); \
66 } while (0)
68 void test_strnlen_string_cst (void)
70 T (3, "1", 2, 1);
71 T (3, "1", 2, 2);
72 T (3, "1", 2, 3);
73 T (3, "12", 3, 1);
74 T (3, "12", 3, 9);
75 T (3, "123", 3, 1);
76 T (3, "123", 3, 4); /* { dg-warning "argument 1 declared attribute .nonstring. is smaller than the specified bound 4" } */
77 T (3, "123", 3, 9); /* { dg-warning "argument 1 declared attribute .nonstring. is smaller than the specified bound 9" } */
79 T (5, "1", 2, 1);
80 T (5, "1", 2, 2);
81 T (5, "1", 2, 9);
83 T (5, "12", 3, 1);
84 T (5, "12", 3, 9);
85 T (5, "123", 3, 1);
86 T (5, "123", 3, 5);
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
91 detected). */
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);
101 size_t n = DIFF_MAX;
102 T (/* [] */, "123", 3, n);
103 T (/* [] */, "123", 3, n + 1); /* { dg-warning "specified bound \[0-9\]+ exceeds maximum object size " } */
104 n = SIZE_MAX;
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]" } */
114 T (3, "123", 3, UR (5, 9)); /* { dg-warning "argument 1 declared attribute .nonstring. is smaller than the specified bound \\\[5, 9]" } */