1 /* PR middle-end/79692 - -Wformat-overflow false positive on an integer
2    directive with unknown width
3    { dg-do compile }
4    { dg-options "-O2 -Wall -Wformat-overflow=1 -ftrack-macro-expansion=0" }
5    { dg-require-effective-target int32plus } */
6 
7 typedef __SIZE_TYPE__  size_t;
8 typedef __WCHAR_TYPE__ wchar_t;
9 
10 #define INT_MAX __INT_MAX__
11 #define INT_MIN (-INT_MAX - 1)
12 
13 /* When debugging, define LINE to the line number of the test case to exercise
14    and avoid exercising any of the others.  The buffer and objsize macros
15    below make use of LINE to avoid warnings for other lines.  */
16 #ifndef LINE
17 # define LINE 0
18 #endif
19 
20 void sink (char*, char*);
21 
22 int dummy_sprintf (char*, const char*, ...);
23 
24 char buffer [1024];
25 extern char *ptr;
26 
int_range(int min,int max)27 static int int_range (int min, int max)
28 {
29   extern int int_value (void);
30   int n = int_value ();
31   return n < min || max < n ? min : n;
32 }
33 
34 /* Evaluate to an array of SIZE characters when non-negative, or to
35    a pointer to an unknown object otherwise.  */
36 #define buffer(size)					\
37   ((0 <= size) ? buffer + sizeof buffer - (size) : ptr)
38 
39 /* Helper to expand function to either __builtin_f or dummy_f to
40    make debugging GCC easy.  */
41 #define FUNC(f)							\
42   ((!LINE || LINE == __LINE__) ? __builtin_ ## f : dummy_ ## f)
43 
44 /* Macro to verify that calls to __builtin_sprintf (i.e., with no size
45    argument) issue diagnostics by correctly determining the size of
46    the destination buffer.  */
47 #define T(size, ...)						\
48   (FUNC (sprintf) (buffer (size),  __VA_ARGS__),		\
49    sink (buffer, ptr))
50 
51 /* Return a signed integer in the range [MIN, MAX].  */
52 #define R(min, max)  int_range (min, max)
53 
test_unknown_width_integer(int w,int i)54 void test_unknown_width_integer (int w, int i)
55 {
56   T (10, "%*d", w, i);
57   T (10, "%*d", w, R (0, 12345));
58 
59   T (10, "%*i", w, i);
60   T (10, "%*i", w, R (0, 12345));
61 
62   T (10, "%*o", w, i);
63   T (10, "%*o", w, R (0, 12345));
64 
65   T (10, "%*i", w, i);
66   T (10, "%*i", w, R (0, 12345));
67 }
68 
test_unknown_width_floating(int w,double d)69 void test_unknown_width_floating (int w, double d)
70 {
71   T ( 7, "%*a", w, d);
72   T (21, "%*a", w, 3.141);
73 
74   T (12, "%*e",  w, d);    /* { dg-warning "may write a terminating nul" } */
75   T (12, "%#*e", w, d);    /* { dg-warning "may write a terminating nul" } */
76   T (13, "%*e",  w, d);
77   T (13, "%#*e", w, d);
78   T (13, "%*e",  w, 3.141);
79 
80   T ( 8, "%*f",  w, d);   /* { dg-warning "may write a terminating nul" } */
81   T ( 8, "%#*f", w, d);   /* { dg-warning "may write a terminating nul" } */
82   T ( 9, "%*f",  w, d);
83   T ( 9, "%#*f", w, d);
84   T ( 9, "%*f",  w, 3.141);
85   T ( 9, "%#*f", w, 3.141);
86 
87   T (12, "%*g", w, d);   /* { dg-warning "may write a terminating nul" } */
88   T (13, "%*g", w, d);
89   T (13, "%*g", w, 3.141);
90 }
91 
test_unknown_precision_integer(int p,int i,double d)92 void test_unknown_precision_integer (int p, int i, double d)
93 {
94   T (10, "%.*d", p, i);
95   T (10, "%.*d", p, R (0, 12345));
96 
97   T (10, "%.*i", p, i);
98   T (10, "%.*i", p, R (0, 12345));
99 
100   T (10, "%.*o", p, i);
101   T (10, "%.*o", p, R (0, 12345));
102 
103   T (10, "%.*i", p, i);
104   T (10, "%.*i", p, R (0, 12345));
105 }
106 
test_unknown_precision_floating(int p,double d)107 void test_unknown_precision_floating (int p, double d)
108 {
109   T ( 0, "%.*a", R (-1, 0), d); /* { dg-warning "between 3 and 24 " } */
110   T ( 6, "%.*a", R (-1, 0), d); /* { dg-warning "may write a terminating nul" } */
111   T ( 7, "%.*a", R (-1, 0), d);
112   T ( 7, "%.*a", p, d);
113   T (21, "%.*a", p, 3.141);
114 
115   T ( 0, "%.*e",  R (-1, 0), d); /* { dg-warning "between 3 and 14 " } */
116   T ( 0, "%.*e",  R (-1, 6), d); /* { dg-warning "between 3 and 14 " } */
117   T ( 5, "%.*e",  R (-1, 6), d); /* { dg-warning "may write a terminating nul" } */
118   T ( 6, "%.*e",  R (-1, 6), d);
119   /* "%.0e", 0.0 results in 3 or 5 bytes: "inf"/"nan" or "0e+00"  */
120   T ( 5, "%.*e",  p, d);      /* { dg-warning "may write a terminating nul" } */
121   /* "%#.0e", 0.0 results in 3 or 6 bytes: "inf"/"nan" or "0.e+00"  */
122   T ( 6, "%#.*e", p, d);      /* { dg-warning "may write a terminating nul" } */
123   T ( 6, "%.*e",  p, d);
124   T ( 6, "%.*e",  p, 3.141);
125   T ( 6, "%#.*e", p, 3.141);  /* { dg-warning "writing a terminating nul" } */
126   T ( 7, "%#.*e", p, 3.141);
127 
128   T ( 0, "%.*f",  R (-1, 0), d); /* { dg-warning "between 1 and 317 " } */
129   T ( 0, "%.*f",  R (-1, 6), d); /* { dg-warning "between 1 and 317 " } */
130   T ( 3, "%.*f",  R (-1, 6), d); /* { dg-warning "may write a terminating nul" } */
131   T ( 4, "%.*f",  R (-1, 6), d);
132   /* "%.0f", 0.0 results in 1 byte: "0" but precision of at least 1
133      is likely, resulting in "0.0".  */
134   T ( 3, "%.*f",  p, d);   /* { dg-warning "may write a terminating nul" } */
135   /* "%#.0f", 0.0 results in 2 bytes: "0." but precision of at least 1
136      is likely, resulting in "0.0".  */
137   T ( 3, "%#.*f", p, d);   /* { dg-warning "may write a terminating nul" } */
138   T ( 4, "%.*f",  p, d);
139   T ( 4, "%#.*f", p, d);
140   T ( 3, "%.*f",  p, 3.141); /* { dg-warning "may write a terminating nul" } */
141   T ( 4, "%.*f",  p, 3.141);
142   T ( 3, "%#.*f", p, 3.141); /* { dg-warning "may write a terminating nul" } */
143   T ( 4, "%#.*f", p, 3.141);
144 
145   T ( 0, "%.*g",  R (-1, 0), d); /* { dg-warning "between 1 and 13 " } */
146   T (12, "%.*g",  R (-1, 0), d); /* { dg-warning "may write a terminating nul" } */
147   T (13, "%.*g",  R (-1, 0), d);
148   T (12, "%.*g",  p, d);   /* { dg-warning "may write a terminating nul" } */
149   T (12, "%#.*g", p, d);   /* { dg-warning "may write a terminating nul" } */
150   T (13, "%.*g",  p, d);
151   T (13, "%#.*g", p, d);
152   T (12, "%#.*g", R (-1, 0), d);/* { dg-warning "may write a terminating nul" } */
153   T (12, "%#.*g", R (-1, 6), d);/* { dg-warning "may write a terminating nul" } */
154   T (13, "%#.*g", R (-1, 0), d);
155   T ( 6, "%#.*g", R ( 0, 0), d);/* { dg-warning "may write a terminating nul" } */
156   T ( 7, "%#.*g", R ( 0, 0), d);
157   T ( 6, "%#.*g", R ( 0, 1), d);/* { dg-warning "may write a terminating nul" } */
158   T ( 7, "%#.*g", R ( 0, 1), d);
159   T ( 3, "%.*g",  p, 3.141); /* { dg-warning "may write a terminating nul" } */
160   T ( 4, "%.*g",  p, 3.141);
161   T ( 3, "%#.*g", p, 3.141); /* { dg-warning "may write a terminating nul" } */
162   T ( 4, "%#.*g", p, 3.141);
163 }
164 
165 
test_unknown_width_and_precision_integer(int w,int p,int i)166 void test_unknown_width_and_precision_integer (int w, int p, int i)
167 {
168   T (10, "%*.*d", w, p, i);
169   T (10, "%*.*d", w, p, R (0, 12345));
170 
171   T (10, "%*.*i", w, p, i);
172   T (10, "%*.*i", w, p, R (0, 12345));
173 
174   T (10, "%*.*o", w, p, i);
175   T (10, "%*.*o", w, p, R (0, 12345));
176 
177   T (10, "%*.*i", w, p, i);
178   T (10, "%*.*i", w, p, R (0, 12345));
179 }
180 
test_unknown_width_and_precision_floating(int w,int p,double d)181 void test_unknown_width_and_precision_floating (int w, int p, double d)
182 {
183   T ( 7, "%*.*a", w, p, d);
184   T (21, "%*.*a", w, p, 3.141);
185 
186   /* "%0.0e", 0.0 results in 3 or 5 bytes: "inf"/"nan" or "0e+00"  */
187   T ( 5, "%*.*e",  w, p, d);   /* { dg-warning "may write a terminating nul" } */
188   /* "%#0.0e", 0.0 results in 3 or 6 bytes: "inf"/"nan" or "0.e+00"  */
189   T ( 6, "%#*.*e", w, p, d);   /* { dg-warning "may write a terminating nul" } */
190   T ( 6, "%*.*e",  w, p, d);
191   T ( 6, "%*.*e",  w, p, 3.141);
192   T ( 6, "%#*.*e", w, p, 3.141);/* { dg-warning "writing a terminating nul" } */
193   T ( 7, "%#*.*e", w, p, 3.141);
194 
195   T ( 3, "%*.*f",  w, p, d);  /* { dg-warning "may write a terminating nul" } */
196   T ( 3, "%#*.*f", w, p, d);  /* { dg-warning "may write a terminating nul" } */
197   T ( 4, "%*.*f",  w, p, d);
198   T ( 4, "%*.*f",  w, p, 3.141);
199   T ( 4, "%#*.*f", w, p, 3.141);
200 
201   T (13, "%*.*g",  w, p, d);
202   T (13, "%#*.*g", w, p, d);
203   T (13, "%*.*g",  w, p, 3.141);
204   T (13, "%#*.*g", w, p, 3.141);
205 }
206