1 /* PR tree-optimization/86552 - missing warning for reading past the end
3 Exercise non-string detection in sprintf.
5 { dg-options "-O2 -Wno-array-bounds -Wall -ftrack-macro-expansion=0" } */
9 typedef __WCHAR_TYPE__
wchar_t;
11 extern int sprintf (char*, const char*, ...);
20 #define CONCAT(a, b) a ## b
21 #define CAT(a, b) CONCAT(a, b)
24 sink (sprintf (dst, fmt, __VA_ARGS__))
26 const char a
[5] = "12345"; /* { dg-message "declared here" } */
27 const char b
[6] = "123456"; /* { dg-message "declared here" } */
28 const char a2
[][3] = {
29 "", "1", "12", "123", "123\000" /* { dg-warning "initializer-string for array of 'char' is too long" } */
33 void test_narrow (void)
35 /* Verify that precision suppresses the warning when it's less
36 than the size of the array. */
37 T ("%.0s%.1s%.2s%.3s%.4s%.5s", a
, a
, a
, a
, a
, a
);
39 T ("%s", a
); /* { dg-warning ".%s. directive argument is not a nul-terminated string" } */
40 T ("%.6s", a
); /* { dg-warning ".%.6s. directive argument is not a nul-terminated string" } */
42 /* Exercise conditional expressions involving strings and non-strings. */
43 const char *s0
= i0
< 0 ? a2
[0] : a2
[3];
44 T ("%s", s0
); /* { dg-warning ".%s. directive argument is not a nul-terminated string" } */
45 s0
= i0
< 0 ? "123456" : a2
[4];
46 T ("%s", s0
); /* { dg-warning ".%s. directive argument is not a nul-terminated string" } */
48 const char *s1
= i0
< 0 ? a2
[3] : a2
[0];
49 T ("%s", s1
); /* { dg-warning ".%s. directive argument is not a nul-terminated string" } */
51 const char *s2
= i0
< 0 ? a2
[3] : a2
[4];
52 T ("%s", s2
); /* { dg-warning ".%s. directive argument is not a nul-terminated string" } */
57 /* Verify that the warning triggers even if precision prevents
58 reading past the end of one of the non-terminated arrays but
60 T ("%.6s", s0
); /* { dg-warning ".%.6s. directive argument is not a nul-terminated string" } */
63 T ("%.7s", s0
); /* { dg-warning ".%.7s. directive argument is not a nul-terminated string" } */
65 /* Verify that at -Wformat-overflow=1 the lower bound of precision
66 given by a range is used to determine whether or not to warn. */
77 T ("%.*s", r
, a
); /* { dg-warning ".%.\\\*s. directive argument is not a nul-terminated string" } */
82 const wchar_t wa
[5] = L
"12345"; /* { dg-message "declared here" } */
86 T ("%.0ls%.1ls%.2ls%.3ls%.4ls%.5ls", wa
, wa
, wa
, wa
, wa
, wa
);
88 T ("%ls", wa
); /* { dg-warning ".%ls. directive argument is not a nul-terminated string" } */
89 T ("%.6ls", wa
); /* { dg-warning ".%.6ls. directive argument is not a nul-terminated string" } */