1 /* 78696 - -fprintf-return-value misoptimizes %.Ng where N is greater than 10
2    Test to verify the correctness of ranges of output computed for floating
3    point directives.
4    { dg-do compile }
5    { dg-require-effective-target double64plus }
6    { dg-skip-if "not IEEE float layout" { "pdp11-*-*" } }
7    { dg-options "-O2 -Wformat -Wformat-overflow -ftrack-macro-expansion=0" } */
8 
9 typedef __builtin_va_list va_list;
10 
11 char dst[1];
12 
13 extern void sink (int, void*);
14 
15 /* Macro to test either width or precision specified by the asterisk
16    (but not both).  */
17 #define T1(fmt, a)    sink (__builtin_sprintf (dst + 1, fmt, a, x), dst)
18 
19 /* Macro to test both width and precision specified by the asterisk.  */
20 #define T2(fmt, w, p) sink (__builtin_sprintf (dst + 1, fmt, w, p, x), dst)
21 
22 /* Macro to test vsprintf with both width and precision specified by
23    the asterisk.  */
24 #define T(fmt) sink (__builtin_vsprintf (dst + 1, fmt, va), dst)
25 
26 /* Exercise %a.  */
test_a(int w,int p,double x)27 void test_a (int w, int p, double x)
28 {
29   T1 ("%.*a", 0);     /* { dg-warning "between 3 and 10 bytes" } */
30   T1 ("%.*a", 1);     /* { dg-warning "between 3 and 12 bytes" } */
31   T1 ("%.*a", 2);     /* { dg-warning "between 3 and 13 bytes" } */
32   T1 ("%.*a", 99);    /* { dg-warning "between 3 and 110 bytes" } */
33   T1 ("%.*a", 199);   /* { dg-warning "between 3 and 210 bytes" } */
34   T1 ("%.*a", 1099);  /* { dg-warning "between 3 and 1110 bytes" } */
35 
36   T1 ("%*.a", 0);     /* { dg-warning "between 3 and 10 bytes" } */
37   T1 ("%*.a", 1);     /* { dg-warning "between 3 and 10 bytes" } */
38   T1 ("%*.a", 3);     /* { dg-warning "between 3 and 10 bytes" } */
39   T1 ("%*.a", 6);     /* { dg-warning "between 6 and 10 bytes" } */
40   T1 ("%*.a", 7);     /* { dg-warning "between 7 and 10 bytes" } */
41 
42   T1 ("%*.a", w);     /* { dg-warning "writing between 3 and (2147483648|32768) bytes" } */
43   T1 ("%*.0a", w);    /* { dg-warning "writing between 3 and (2147483648|32768) bytes" } */
44   T1 ("%*.1a", w);    /* { dg-warning "writing between 3 and (2147483648|32768) bytes" } */
45   T1 ("%*.2a", w);    /* { dg-warning "writing between 3 and (2147483648|32768) bytes" } */
46 
47   T1 ("%.*a",  p);    /* { dg-warning "writing between 3 and (2147483658|32778) bytes" } */
48   T1 ("%1.*a", p);    /* { dg-warning "writing between 3 and (2147483658|32778) bytes" } */
49   T1 ("%2.*a", p);    /* { dg-warning "writing between 3 and (2147483658|32778) bytes" } */
50   T1 ("%3.*a", p);    /* { dg-warning "writing between 3 and (2147483658|32778) bytes" } */
51 
52   T2 ("%*.*a", w, p); /* { dg-warning "writing between 3 and (2147483658|32778) bytes" } */
53 }
54 
55 /* Exercise %e.  */
test_e(int w,int p,double x)56 void test_e (int w, int p, double x)
57 {
58   T1 ("%.*e", 0);     /* { dg-warning "between 3 and 7 bytes" } */
59   T1 ("%.*e", 1);     /* { dg-warning "between 3 and 9 bytes" } */
60   T1 ("%.*e", 2);     /* { dg-warning "between 3 and 10 bytes" } */
61   T1 ("%.*e", 99);    /* { dg-warning "between 3 and 107 bytes" } */
62   T1 ("%.*e", 199);   /* { dg-warning "between 3 and 207 bytes" } */
63   T1 ("%.*e", 1099);  /* { dg-warning "between 3 and 1107 bytes" } */
64 
65   T1 ("%*.e", 0);     /* { dg-warning "between 3 and 7 bytes" } */
66   T1 ("%*.e", 1);     /* { dg-warning "between 3 and 7 bytes" } */
67   T1 ("%*.e", 1);     /* { dg-warning "between 3 and 7 bytes" } */
68   T1 ("%*.e", 3);     /* { dg-warning "between 3 and 7 bytes" } */
69   T1 ("%*.e", 6);     /* { dg-warning "between 6 and 7 bytes" } */
70   T1 ("%*.e", 7);     /* { dg-warning "writing 7 bytes" } */
71 
72   T1 ("%*.e", w);     /* { dg-warning "writing between 3 and (2147483648|32768) bytes" } */
73   T1 ("%*.0e", w);    /* { dg-warning "writing between 3 and (2147483648|32768) bytes" } */
74   T1 ("%*.1e", w);    /* { dg-warning "writing between 3 and (2147483648|32768) bytes" } */
75   T1 ("%*.2e", w);    /* { dg-warning "writing between 3 and (2147483648|32768) bytes" } */
76 
77   T1 ("%.*e",  p);    /* { dg-warning "writing between 3 and (2147483655|32775) bytes" } */
78   T1 ("%1.*e", p);    /* { dg-warning "writing between 3 and (2147483655|32775) bytes" } */
79   T1 ("%2.*e", p);    /* { dg-warning "writing between 3 and (2147483655|32775) bytes" } */
80   T1 ("%3.*e", p);    /* { dg-warning "writing between 3 and (2147483655|32775) bytes" } */
81 
82   T2 ("%*.*e", w, p); /* { dg-warning "writing between 3 and (2147483655|32775) bytes" } */
83 }
84 
85 /* Exercise %f.  */
test_f(int w,int p,double x)86 void test_f (int w, int p, double x)
87 {
88   T1 ("%.*f", 0);           /* { dg-warning "between 1 and 310 bytes" } */
89   T1 ("%.*f", 1);           /* { dg-warning "between 3 and 312 bytes" } */
90   T1 ("%.*f", 2);           /* { dg-warning "between 3 and 313 bytes" } */
91   T1 ("%.*f", 99);          /* { dg-warning "between 3 and 410 bytes" } */
92   T1 ("%.*f", 199);         /* { dg-warning "between 3 and 510 bytes" } */
93   T1 ("%.*f", 1099);        /* { dg-warning "between 3 and 1410 bytes" } */
94 
95   T2 ("%*.*f", 0, 0);       /* { dg-warning "between 1 and 310 bytes" } */
96   T2 ("%*.*f", 1, 0);       /* { dg-warning "between 1 and 310 bytes" } */
97   T2 ("%*.*f", 2, 0);       /* { dg-warning "between 2 and 310 bytes" } */
98   T2 ("%*.*f", 3, 0);       /* { dg-warning "between 3 and 310 bytes" } */
99   T2 ("%*.*f", 310, 0);     /* { dg-warning "writing 310 bytes" } */
100   T2 ("%*.*f", 311, 0);     /* { dg-warning "writing 311 bytes" } */
101   T2 ("%*.*f", 312, 312);   /* { dg-warning "between 312 and 623 bytes" } */
102   T2 ("%*.*f", 312, 313);   /* { dg-warning "between 312 and 624 bytes" } */
103 
104   T1 ("%*.f", w);           /* { dg-warning "writing between 1 and (2147483648|32768) bytes" } */
105   T1 ("%*.0f", w);          /* { dg-warning "writing between 1 and (2147483648|32768) bytes" } */
106   T1 ("%*.1f", w);          /* { dg-warning "writing between 3 and (2147483648|32768) bytes" } */
107   T1 ("%*.2f", w);          /* { dg-warning "writing between 3 and (2147483648|32768) bytes" } */
108 
109   T1 ("%.*f",  p);          /* { dg-warning "writing between 1 and (2147483958|33078) bytes" } */
110   T1 ("%1.*f", p);          /* { dg-warning "writing between 1 and (2147483958|33078) bytes" } */
111   T1 ("%2.*f", p);          /* { dg-warning "writing between 2 and (2147483958|33078) bytes" } */
112   T1 ("%3.*f", p);          /* { dg-warning "writing between 3 and (2147483958|33078) bytes" } */
113 
114   T2 ("%*.*f", w, p);       /* { dg-warning "writing between 1 and (2147483958|33078) bytes" } */
115 }
116 
117 /* Exercise %g.  The expected output is the lesser of %e and %f.  */
test_g(double x)118 void test_g (double x)
119 {
120   T1 ("%.*g", 0);           /* { dg-warning "between 1 and 7 bytes" } */
121   T1 ("%.*g", 1);           /* { dg-warning "between 1 and 7 bytes" } */
122   T1 ("%.*g", 2);           /* { dg-warning "between 1 and 9 bytes" } */
123   T1 ("%.*g", 99);          /* { dg-warning "between 1 and 106 bytes" } */
124   T1 ("%.*g", 199);         /* { dg-warning "between 1 and 206 bytes" } */
125   T1 ("%.*g", 1099);        /* { dg-warning "between 1 and 310 bytes" } */
126 
127   T2 ("%*.*g", 0, 0);       /* { dg-warning "between 1 and 7 bytes" } */
128   T2 ("%*.*g", 1, 0);       /* { dg-warning "between 1 and 7 bytes" } */
129   T2 ("%*.*g", 2, 0);       /* { dg-warning "between 2 and 7 bytes" } */
130   T2 ("%*.*g", 3, 0);       /* { dg-warning "between 3 and 7 bytes" } */
131   T2 ("%*.*g", 7, 0);       /* { dg-warning "writing 7 bytes" } */
132   T2 ("%*.*g", 310, 0);     /* { dg-warning "writing 310 bytes" } */
133   T2 ("%*.*g", 311, 0);     /* { dg-warning "writing 311 bytes" } */
134   T2 ("%*.*g", 312, 312);   /* { dg-warning "writing 312 bytes" } */
135   T2 ("%*.*g", 312, 313);   /* { dg-warning "writing 312 bytes" } */
136   T2 ("%*.*g", 333, 999);   /* { dg-warning "writing 333 bytes" } */
137 }
138 
139 /* Exercise %a.  */
test_a_va(va_list va)140 void test_a_va (va_list va)
141 {
142   T ("%.0a");       /* { dg-warning "between 3 and 10 bytes" } */
143   T ("%.1a");       /* { dg-warning "between 3 and 12 bytes" } */
144   T ("%.2a");       /* { dg-warning "between 3 and 13 bytes" } */
145   T ("%.99a");      /* { dg-warning "between 3 and 110 bytes" } */
146   T ("%.199a");     /* { dg-warning "between 3 and 210 bytes" } */
147   T ("%.1099a");    /* { dg-warning "between 3 and 1110 bytes" } */
148 
149   T ("%0.a");       /* { dg-warning "between 3 and 10 bytes" } */
150   T ("%1.a");       /* { dg-warning "between 3 and 10 bytes" } */
151   T ("%3.a");       /* { dg-warning "between 3 and 10 bytes" } */
152   T ("%6.a");       /* { dg-warning "between 6 and 10 bytes" } */
153   T ("%7.a");       /* { dg-warning "between 7 and 10 bytes" } */
154 
155   T ("%*.a");       /* { dg-warning "writing between 3 and (2147483648|32768) bytes" } */
156   T ("%*.0a");      /* { dg-warning "writing between 3 and (2147483648|32768) bytes" } */
157   T ("%*.1a");      /* { dg-warning "writing between 3 and (2147483648|32768) bytes" } */
158   T ("%*.2a");      /* { dg-warning "writing between 3 and (2147483648|32768) bytes" } */
159 
160   T ("%.*a");       /* { dg-warning "writing between 3 and (2147483658|32778) bytes" } */
161   T ("%1.*a");      /* { dg-warning "writing between 3 and (2147483658|32778) bytes" } */
162   T ("%2.*a");      /* { dg-warning "writing between 3 and (2147483658|32778) bytes" } */
163   T ("%6.*a");      /* { dg-warning "writing between 6 and (2147483658|32778) bytes" } */
164   T ("%9.*a");      /* { dg-warning "writing between 9 and (2147483658|32778) bytes" } */
165 
166   T ("%*.*a");      /* { dg-warning "writing between 3 and (2147483658|32778) bytes" } */
167 }
168 
169 /* Exercise %e.  */
test_e_va(va_list va)170 void test_e_va (va_list va)
171 {
172   T ("%e");         /* { dg-warning "between 3 and 14 bytes" } */
173   T ("%+e");        /* { dg-warning "between 4 and 14 bytes" } */
174   T ("% e");        /* { dg-warning "between 4 and 14 bytes" } */
175   T ("%#e");        /* { dg-warning "between 3 and 14 bytes" } */
176   T ("%#+e");       /* { dg-warning "between 4 and 14 bytes" } */
177   T ("%# e");       /* { dg-warning "between 4 and 14 bytes" } */
178 
179   T ("%.e");        /* { dg-warning "between 3 and 7 bytes" } */
180   T ("%.0e");       /* { dg-warning "between 3 and 7 bytes" } */
181   T ("%.1e");       /* { dg-warning "between 3 and 9 bytes" } */
182   T ("%.2e");       /* { dg-warning "between 3 and 10 bytes" } */
183   T ("%.99e");      /* { dg-warning "between 3 and 107 bytes" } */
184   T ("%.199e");     /* { dg-warning "between 3 and 207 bytes" } */
185   T ("%.1099e");    /* { dg-warning "between 3 and 1107 bytes" } */
186 
187   T ("%0.e");       /* { dg-warning "between 3 and 7 bytes" } */
188   T ("%1.e");       /* { dg-warning "between 3 and 7 bytes" } */
189   T ("%1.e");       /* { dg-warning "between 3 and 7 bytes" } */
190   T ("%3.e");       /* { dg-warning "between 3 and 7 bytes" } */
191   T ("%6.e");       /* { dg-warning "between 6 and 7 bytes" } */
192   T ("%7.e");       /* { dg-warning "writing 7 bytes" } */
193 
194   T ("%.*e");       /* { dg-warning "writing between 3 and (2147483655|32775) bytes" } */
195   T ("%1.*e");      /* { dg-warning "writing between 3 and (2147483655|32775) bytes" } */
196   T ("%6.*e");      /* { dg-warning "writing between 6 and (2147483655|32775) bytes" } */
197   T ("%9.*e");      /* { dg-warning "writing between 9 and (2147483655|32775) bytes" } */
198 
199   T ("%*.*e");      /* { dg-warning "writing between 3 and (2147483655|32775) bytes" } */
200 }
201 
202 /* Exercise %f.  */
test_f_va(va_list va)203 void test_f_va (va_list va)
204 {
205   T ("%f");         /* { dg-warning "between 3 and 317 bytes" } */
206   T ("%+f");        /* { dg-warning "between 4 and 317 bytes" } */
207   T ("% f");        /* { dg-warning "between 4 and 317 bytes" } */
208   T ("%#f");        /* { dg-warning "between 3 and 317 bytes" } */
209   T ("%+f");        /* { dg-warning "between 4 and 317 bytes" } */
210   T ("% f");        /* { dg-warning "between 4 and 317 bytes" } */
211   T ("%#+f");       /* { dg-warning "between 4 and 317 bytes" } */
212   T ("%# f");       /* { dg-warning "between 4 and 317 bytes" } */
213 
214   T ("%.f");        /* { dg-warning "between 1 and 310 bytes" } */
215   T ("%.0f");       /* { dg-warning "between 1 and 310 bytes" } */
216   T ("%.1f");       /* { dg-warning "between 3 and 312 bytes" } */
217   T ("%.2f");       /* { dg-warning "between 3 and 313 bytes" } */
218   T ("%.99f");      /* { dg-warning "between 3 and 410 bytes" } */
219   T ("%.199f");     /* { dg-warning "between 3 and 510 bytes" } */
220   T ("%.1099f");    /* { dg-warning "between 3 and 1410 bytes" } */
221 
222   T ("%0.0f");      /* { dg-warning "between 1 and 310 bytes" } */
223   T ("%1.0f");      /* { dg-warning "between 1 and 310 bytes" } */
224   T ("%2.0f");      /* { dg-warning "between 2 and 310 bytes" } */
225   T ("%3.0f");      /* { dg-warning "between 3 and 310 bytes" } */
226   T ("%310.0f");    /* { dg-warning "writing 310 bytes" } */
227   T ("%311.0f");    /* { dg-warning "writing 311 bytes" } */
228   T ("%312.312f");  /* { dg-warning "between 312 and 623 bytes" } */
229   T ("%312.313f");  /* { dg-warning "between 312 and 624 bytes" } */
230 
231   T ("%.*f");       /* { dg-warning "writing between 1 and (2147483958|33078) bytes" } */
232   T ("%1.*f");      /* { dg-warning "writing between 1 and (2147483958|33078) bytes" } */
233   T ("%3.*f");      /* { dg-warning "writing between 3 and (2147483958|33078) bytes" } */
234 
235   T ("%*.*f");      /* { dg-warning "writing between 1 and (2147483958|33078) bytes" } */
236 }
237 
238 /* Exercise %g.  The expected output is the lesser of %e and %f.  */
test_g_va(va_list va)239 void test_g_va (va_list va)
240 {
241   T ("%g");         /* { dg-warning "between 1 and 13 bytes" } */
242   T ("%+g");        /* { dg-warning "between 2 and 13 bytes" } */
243   T ("% g");        /* { dg-warning "between 2 and 13 bytes" } */
244 
245   /* The pound flag means the radix character is always present.  */
246   T ("%#g");        /* { dg-warning "between 2 and 13 bytes" } */
247   T ("%#+g");       /* { dg-warning "between 3 and 13 bytes" } */
248   T ("%# g");       /* { dg-warning "between 3 and 13 bytes" } */
249 
250   T ("%.g");        /* { dg-warning "between 1 and 7 bytes" } */
251   T ("%.0g");       /* { dg-warning "between 1 and 7 bytes" } */
252   T ("%.1g");       /* { dg-warning "between 1 and 7 bytes" } */
253   T ("%.2g");       /* { dg-warning "between 1 and 9 bytes" } */
254   T ("%.99g");      /* { dg-warning "between 1 and 106 bytes" } */
255   T ("%.199g");     /* { dg-warning "between 1 and 206 bytes" } */
256   T ("%.1099g");    /* { dg-warning "between 1 and 310 bytes" } */
257 
258   T ("%0.0g");      /* { dg-warning "between 1 and 7 bytes" } */
259   T ("%1.0g");      /* { dg-warning "between 1 and 7 bytes" } */
260   T ("%2.0g");      /* { dg-warning "between 2 and 7 bytes" } */
261   T ("%3.0g");      /* { dg-warning "between 3 and 7 bytes" } */
262   T ("%7.0g");      /* { dg-warning "writing 7 bytes" } */
263   T ("%310.0g");    /* { dg-warning "writing 310 bytes" } */
264   T ("%311.0g");    /* { dg-warning "writing 311 bytes" } */
265   T ("%312.312g");  /* { dg-warning "writing 312 bytes" } */
266   T ("%312.313g");  /* { dg-warning "writing 312 bytes" } */
267   T ("%333.999g");  /* { dg-warning "writing 333 bytes" } */
268 
269   T ("%.*g");       /* { dg-warning "writing between 1 and 310 bytes" } */
270   T ("%1.*g");      /* { dg-warning "writing between 1 and 310 bytes" } */
271   T ("%4.*g");      /* { dg-warning "writing between 4 and 310 bytes" } */
272 
273   T ("%*.*g");      /* { dg-warning "writing between 1 and (2147483648|32768) bytes" } */
274 }
275