1 /* PR tree-optimization/80669 - Bad -Wstringop-overflow warnings for stpncpy
3 { dg-options "-O2 -Wall -Wno-array-bounds -Wno-restrict -Wno-stringop-truncation" } */
5 #define SIZE_MAX __SIZE_MAX__
7 typedef __SIZE_TYPE__
size_t;
11 #define stpncpy (d, s, n) sink (__builtin_stpncpy (d, s, n))
15 static size_t range (size_t min
, size_t max
)
17 size_t val
= value ();
18 return val
< min
|| max
< val
? min
: val
;
21 /* Verify that no -Wstringop-overflow warning is issued for stpncpy
22 with constant size. (Some tests cause -Wstringop-truncation and
24 void test_cst (char *d
)
26 __builtin_stpncpy (d
, "123", 0);
27 __builtin_stpncpy (d
, "123", 1);
28 __builtin_stpncpy (d
, "123", 2);
29 __builtin_stpncpy (d
, "123", 3);
30 __builtin_stpncpy (d
, "123", 4);
31 __builtin_stpncpy (d
, "123", 5);
32 __builtin_stpncpy (d
, "123", 999);
34 size_t n
= SIZE_MAX
/ 2;
36 __builtin_stpncpy (d
, "123", n
);
38 __builtin_stpncpy (d
, "123", n
+ 1); /* { dg-warning "specified size \[0-9\]+ exceeds maximum object size \[0-9\]+" } */
42 /* Verify that no -Wstringop-overflow warning is issued for stpncpy
43 with size in some range. */
44 void test_rng (char *d
)
46 #define R(min, max) range (min, max)
48 __builtin_stpncpy (d
, "123", R (0, 1));
49 __builtin_stpncpy (d
, "123", R (0, 2));
50 __builtin_stpncpy (d
, "123", R (0, 3));
51 __builtin_stpncpy (d
, "123", R (0, 4));
52 __builtin_stpncpy (d
, "123", R (0, 5));
54 __builtin_stpncpy (d
, "123", R (1, 2));
55 __builtin_stpncpy (d
, "123", R (1, 3));
56 __builtin_stpncpy (d
, "123", R (1, 4));
57 __builtin_stpncpy (d
, "123", R (1, 5));
59 __builtin_stpncpy (d
, "123", R (2, 3));
60 __builtin_stpncpy (d
, "123", R (2, 4));
61 __builtin_stpncpy (d
, "123", R (2, 5));
63 __builtin_stpncpy (d
, "123", R (3, 4));
64 __builtin_stpncpy (d
, "123", R (3, 5));
66 __builtin_stpncpy (d
, "123", R (4, 5));
68 __builtin_stpncpy (d
, "123", R (5, 6));
70 __builtin_stpncpy (d
, "123", R (12345, 23456));
72 size_t n
= SIZE_MAX
/ 2;
74 __builtin_stpncpy (d
, "123", R (n
- 1, n
+ 1));
76 __builtin_stpncpy (d
, "123", R (n
+ 1, n
+ 2)); /* { dg-warning "specified size between \[0-9\]+ and \[0-9\]+ exceeds maximum object size \[0-9\]+" } */