fix buffer overflow in printf formatting of denormals with low bit set
[musl] / src / stdio / vfprintf.c
1 #include "stdio_impl.h"
2 #include <errno.h>
3 #include <ctype.h>
4 #include <limits.h>
5 #include <string.h>
6 #include <stdarg.h>
7 #include <wchar.h>
8 #include <inttypes.h>
9 #include <math.h>
10 #include <float.h>
11
12 /* Some useful macros */
13
14 #define MAX(a,b) ((a)>(b) ? (a) : (b))
15 #define MIN(a,b) ((a)<(b) ? (a) : (b))
16 #define CONCAT2(x,y) x ## y
17 #define CONCAT(x,y) CONCAT2(x,y)
18
19 /* Convenient bit representation for modifier flags, which all fall
20  * within 31 codepoints of the space character. */
21
22 #define ALT_FORM   (1U<<'#'-' ')
23 #define ZERO_PAD   (1U<<'0'-' ')
24 #define LEFT_ADJ   (1U<<'-'-' ')
25 #define PAD_POS    (1U<<' '-' ')
26 #define MARK_POS   (1U<<'+'-' ')
27 #define GROUPED    (1U<<'\''-' ')
28
29 #define FLAGMASK (ALT_FORM|ZERO_PAD|LEFT_ADJ|PAD_POS|MARK_POS|GROUPED)
30
31 #if UINT_MAX == ULONG_MAX
32 #define LONG_IS_INT
33 #endif
34
35 #if SIZE_MAX != ULONG_MAX || UINTMAX_MAX != ULLONG_MAX
36 #define ODD_TYPES
37 #endif
38
39 /* State machine to accept length modifiers + conversion specifiers.
40  * Result is 0 on failure, or an argument type to pop on success. */
41
42 enum {
43         BARE, LPRE, LLPRE, HPRE, HHPRE, BIGLPRE,
44         ZTPRE, JPRE,
45         STOP,
46         PTR, INT, UINT, ULLONG,
47 #ifndef LONG_IS_INT
48         LONG, ULONG,
49 #else
50 #define LONG INT
51 #define ULONG UINT
52 #endif
53         SHORT, USHORT, CHAR, UCHAR,
54 #ifdef ODD_TYPES
55         LLONG, SIZET, IMAX, UMAX, PDIFF, UIPTR,
56 #else
57 #define LLONG ULLONG
58 #define SIZET ULONG
59 #define IMAX LLONG
60 #define UMAX ULLONG
61 #define PDIFF LONG
62 #define UIPTR ULONG
63 #endif
64         DBL, LDBL,
65         NOARG,
66         MAXSTATE
67 };
68
69 #define S(x) [(x)-'A']
70
71 static const unsigned char states[]['z'-'A'+1] = {
72         { /* 0: bare types */
73                 S('d') = INT, S('i') = INT,
74                 S('o') = UINT, S('u') = UINT, S('x') = UINT, S('X') = UINT,
75                 S('e') = DBL, S('f') = DBL, S('g') = DBL, S('a') = DBL,
76                 S('E') = DBL, S('F') = DBL, S('G') = DBL, S('A') = DBL,
77                 S('c') = CHAR, S('C') = INT,
78                 S('s') = PTR, S('S') = PTR, S('p') = UIPTR, S('n') = PTR,
79                 S('m') = NOARG,
80                 S('l') = LPRE, S('h') = HPRE, S('L') = BIGLPRE,
81                 S('z') = ZTPRE, S('j') = JPRE, S('t') = ZTPRE,
82         }, { /* 1: l-prefixed */
83                 S('d') = LONG, S('i') = LONG,
84                 S('o') = ULONG, S('u') = ULONG, S('x') = ULONG, S('X') = ULONG,
85                 S('e') = DBL, S('f') = DBL, S('g') = DBL, S('a') = DBL,
86                 S('E') = DBL, S('F') = DBL, S('G') = DBL, S('A') = DBL,
87                 S('c') = INT, S('s') = PTR, S('n') = PTR,
88                 S('l') = LLPRE,
89         }, { /* 2: ll-prefixed */
90                 S('d') = LLONG, S('i') = LLONG,
91                 S('o') = ULLONG, S('u') = ULLONG,
92                 S('x') = ULLONG, S('X') = ULLONG,
93                 S('n') = PTR,
94         }, { /* 3: h-prefixed */
95                 S('d') = SHORT, S('i') = SHORT,
96                 S('o') = USHORT, S('u') = USHORT,
97                 S('x') = USHORT, S('X') = USHORT,
98                 S('n') = PTR,
99                 S('h') = HHPRE,
100         }, { /* 4: hh-prefixed */
101                 S('d') = CHAR, S('i') = CHAR,
102                 S('o') = UCHAR, S('u') = UCHAR,
103                 S('x') = UCHAR, S('X') = UCHAR,
104                 S('n') = PTR,
105         }, { /* 5: L-prefixed */
106                 S('e') = LDBL, S('f') = LDBL, S('g') = LDBL, S('a') = LDBL,
107                 S('E') = LDBL, S('F') = LDBL, S('G') = LDBL, S('A') = LDBL,
108                 S('n') = PTR,
109         }, { /* 6: z- or t-prefixed (assumed to be same size) */
110                 S('d') = PDIFF, S('i') = PDIFF,
111                 S('o') = SIZET, S('u') = SIZET,
112                 S('x') = SIZET, S('X') = SIZET,
113                 S('n') = PTR,
114         }, { /* 7: j-prefixed */
115                 S('d') = IMAX, S('i') = IMAX,
116                 S('o') = UMAX, S('u') = UMAX,
117                 S('x') = UMAX, S('X') = UMAX,
118                 S('n') = PTR,
119         }
120 };
121
122 #define OOB(x) ((unsigned)(x)-'A' > 'z'-'A')
123
124 union arg
125 {
126         uintmax_t i;
127         long double f;
128         void *p;
129 };
130
131 static void pop_arg(union arg *arg, int type, va_list *ap)
132 {
133         /* Give the compiler a hint for optimizing the switch. */
134         if ((unsigned)type > MAXSTATE) return;
135         switch (type) {
136                case PTR:        arg->p = va_arg(*ap, void *);
137         break; case INT:        arg->i = va_arg(*ap, int);
138         break; case UINT:       arg->i = va_arg(*ap, unsigned int);
139 #ifndef LONG_IS_INT
140         break; case LONG:       arg->i = va_arg(*ap, long);
141         break; case ULONG:      arg->i = va_arg(*ap, unsigned long);
142 #endif
143         break; case ULLONG:     arg->i = va_arg(*ap, unsigned long long);
144         break; case SHORT:      arg->i = (short)va_arg(*ap, int);
145         break; case USHORT:     arg->i = (unsigned short)va_arg(*ap, int);
146         break; case CHAR:       arg->i = (signed char)va_arg(*ap, int);
147         break; case UCHAR:      arg->i = (unsigned char)va_arg(*ap, int);
148 #ifdef ODD_TYPES
149         break; case LLONG:      arg->i = va_arg(*ap, long long);
150         break; case SIZET:      arg->i = va_arg(*ap, size_t);
151         break; case IMAX:       arg->i = va_arg(*ap, intmax_t);
152         break; case UMAX:       arg->i = va_arg(*ap, uintmax_t);
153         break; case PDIFF:      arg->i = va_arg(*ap, ptrdiff_t);
154         break; case UIPTR:      arg->i = (uintptr_t)va_arg(*ap, void *);
155 #endif
156         break; case DBL:        arg->f = va_arg(*ap, double);
157         break; case LDBL:       arg->f = va_arg(*ap, long double);
158         }
159 }
160
161 static void out(FILE *f, const char *s, size_t l)
162 {
163         __fwritex((void *)s, l, f);
164 }
165
166 static void pad(FILE *f, char c, int w, int l, int fl)
167 {
168         char pad[256];
169         if (fl & (LEFT_ADJ | ZERO_PAD) || l >= w) return;
170         l = w - l;
171         memset(pad, c, l>sizeof pad ? sizeof pad : l);
172         for (; l >= sizeof pad; l -= sizeof pad)
173                 out(f, pad, sizeof pad);
174         out(f, pad, l);
175 }
176
177 static const char xdigits[16] = {
178         "0123456789ABCDEF"
179 };
180
181 static char *fmt_x(uintmax_t x, char *s, int lower)
182 {
183         for (; x; x>>=4) *--s = xdigits[(x&15)]|lower;
184         return s;
185 }
186
187 static char *fmt_o(uintmax_t x, char *s)
188 {
189         for (; x; x>>=3) *--s = '0' + (x&7);
190         return s;
191 }
192
193 static char *fmt_u(uintmax_t x, char *s)
194 {
195         unsigned long y;
196         for (   ; x>ULONG_MAX; x/=10) *--s = '0' + x%10;
197         for (y=x;           y; y/=10) *--s = '0' + y%10;
198         return s;
199 }
200
201 /* Do not override this check. The floating point printing code below
202  * depends on the float.h constants being right. If they are wrong, it
203  * may overflow the stack. */
204 #if LDBL_MANT_DIG == 53
205 typedef char compiler_defines_long_double_incorrectly[9-(int)sizeof(long double)];
206 #endif
207
208 static int fmt_fp(FILE *f, long double y, int w, int p, int fl, int t)
209 {
210         uint32_t big[(LDBL_MANT_DIG+28)/29 + 1          // mantissa expansion
211                 + (LDBL_MAX_EXP+LDBL_MANT_DIG+28+8)/9]; // exponent expansion
212         uint32_t *a, *d, *r, *z;
213         int e2=0, e, i, j, l;
214         char buf[9+LDBL_MANT_DIG/4], *s;
215         const char *prefix="-0X+0X 0X-0x+0x 0x";
216         int pl;
217         char ebuf0[3*sizeof(int)], *ebuf=&ebuf0[3*sizeof(int)], *estr;
218
219         pl=1;
220         if (signbit(y)) {
221                 y=-y;
222         } else if (fl & MARK_POS) {
223                 prefix+=3;
224         } else if (fl & PAD_POS) {
225                 prefix+=6;
226         } else prefix++, pl=0;
227
228         if (!isfinite(y)) {
229                 char *s = (t&32)?"inf":"INF";
230                 if (y!=y) s=(t&32)?"nan":"NAN", pl=0;
231                 pad(f, ' ', w, 3+pl, fl&~ZERO_PAD);
232                 out(f, prefix, pl);
233                 out(f, s, 3);
234                 pad(f, ' ', w, 3+pl, fl^LEFT_ADJ);
235                 return MAX(w, 3+pl);
236         }
237
238         y = frexpl(y, &e2) * 2;
239         if (y) e2--;
240
241         if ((t|32)=='a') {
242                 long double round = 8.0;
243                 int re;
244
245                 if (t&32) prefix += 9;
246                 pl += 2;
247
248                 if (p<0 || p>=LDBL_MANT_DIG/4-1) re=0;
249                 else re=LDBL_MANT_DIG/4-1-p;
250
251                 if (re) {
252                         while (re--) round*=16;
253                         if (*prefix=='-') {
254                                 y=-y;
255                                 y-=round;
256                                 y+=round;
257                                 y=-y;
258                         } else {
259                                 y+=round;
260                                 y-=round;
261                         }
262                 }
263
264                 estr=fmt_u(e2<0 ? -e2 : e2, ebuf);
265                 if (estr==ebuf) *--estr='0';
266                 *--estr = (e2<0 ? '-' : '+');
267                 *--estr = t+('p'-'a');
268
269                 s=buf;
270                 do {
271                         int x=y;
272                         *s++=xdigits[x]|(t&32);
273                         y=16*(y-x);
274                         if (s-buf==1 && (y||p>0||(fl&ALT_FORM))) *s++='.';
275                 } while (y);
276
277                 if (p && s-buf-2 < p)
278                         l = (p+2) + (ebuf-estr);
279                 else
280                         l = (s-buf) + (ebuf-estr);
281
282                 pad(f, ' ', w, pl+l, fl);
283                 out(f, prefix, pl);
284                 pad(f, '0', w, pl+l, fl^ZERO_PAD);
285                 out(f, buf, s-buf);
286                 pad(f, '0', l-(ebuf-estr)-(s-buf), 0, 0);
287                 out(f, estr, ebuf-estr);
288                 pad(f, ' ', w, pl+l, fl^LEFT_ADJ);
289                 return MAX(w, pl+l);
290         }
291         if (p<0) p=6;
292
293         if (y) y *= 0x1p28, e2-=28;
294
295         if (e2<0) a=r=z=big;
296         else a=r=z=big+sizeof(big)/sizeof(*big) - LDBL_MANT_DIG - 1;
297
298         do {
299                 *z = y;
300                 y = 1000000000*(y-*z++);
301         } while (y);
302
303         while (e2>0) {
304                 uint32_t carry=0;
305                 int sh=MIN(29,e2);
306                 for (d=z-1; d>=a; d--) {
307                         uint64_t x = ((uint64_t)*d<<sh)+carry;
308                         *d = x % 1000000000;
309                         carry = x / 1000000000;
310                 }
311                 if (!z[-1] && z>a) z--;
312                 if (carry) *--a = carry;
313                 e2-=sh;
314         }
315         while (e2<0) {
316                 uint32_t carry=0, *b;
317                 int sh=MIN(9,-e2);
318                 for (d=a; d<z; d++) {
319                         uint32_t rm = *d & (1<<sh)-1;
320                         *d = (*d>>sh) + carry;
321                         carry = (1000000000>>sh) * rm;
322                 }
323                 if (!*a) a++;
324                 if (carry) *z++ = carry;
325                 /* Avoid (slow!) computation past requested precision */
326                 b = (t|32)=='f' ? r : a;
327                 if (z-b > 2+p/9) z = b+2+p/9;
328                 e2+=sh;
329         }
330
331         if (a<z) for (i=10, e=9*(r-a); *a>=i; i*=10, e++);
332         else e=0;
333
334         /* Perform rounding: j is precision after the radix (possibly neg) */
335         j = p - ((t|32)!='f')*e - ((t|32)=='g' && p);
336         if (j < 9*(z-r-1)) {
337                 uint32_t x;
338                 /* We avoid C's broken division of negative numbers */
339                 d = r + 1 + ((j+9*LDBL_MAX_EXP)/9 - LDBL_MAX_EXP);
340                 j += 9*LDBL_MAX_EXP;
341                 j %= 9;
342                 for (i=10, j++; j<9; i*=10, j++);
343                 x = *d % i;
344                 /* Are there any significant digits past j? */
345                 if (x || d+1!=z) {
346                         long double round = CONCAT(0x1p,LDBL_MANT_DIG);
347                         long double small;
348                         if (*d/i & 1) round += 2;
349                         if (x<i/2) small=0x0.8p0;
350                         else if (x==i/2 && d+1==z) small=0x1.0p0;
351                         else small=0x1.8p0;
352                         if (pl && *prefix=='-') round*=-1, small*=-1;
353                         *d -= x;
354                         /* Decide whether to round by probing round+small */
355                         if (round+small != round) {
356                                 *d = *d + i;
357                                 while (*d > 999999999) {
358                                         *d--=0;
359                                         (*d)++;
360                                 }
361                                 if (d<a) a=d;
362                                 for (i=10, e=9*(r-a); *a>=i; i*=10, e++);
363                         }
364                 }
365                 if (z>d+1) z=d+1;
366                 for (; !z[-1] && z>a; z--);
367         }
368         
369         if ((t|32)=='g') {
370                 if (!p) p++;
371                 if (p>e && e>=-4) {
372                         t--;
373                         p-=e+1;
374                 } else {
375                         t-=2;
376                         p--;
377                 }
378                 if (!(fl&ALT_FORM)) {
379                         /* Count trailing zeros in last place */
380                         if (z>a && z[-1]) for (i=10, j=0; z[-1]%i==0; i*=10, j++);
381                         else j=9;
382                         if ((t|32)=='f')
383                                 p = MIN(p,MAX(0,9*(z-r-1)-j));
384                         else
385                                 p = MIN(p,MAX(0,9*(z-r-1)+e-j));
386                 }
387         }
388         l = 1 + p + (p || (fl&ALT_FORM));
389         if ((t|32)=='f') {
390                 if (e>0) l+=e;
391         } else {
392                 estr=fmt_u(e<0 ? -e : e, ebuf);
393                 while(ebuf-estr<2) *--estr='0';
394                 *--estr = (e<0 ? '-' : '+');
395                 *--estr = t;
396                 l += ebuf-estr;
397         }
398
399         pad(f, ' ', w, pl+l, fl);
400         out(f, prefix, pl);
401         pad(f, '0', w, pl+l, fl^ZERO_PAD);
402
403         if ((t|32)=='f') {
404                 if (a>r) a=r;
405                 for (d=a; d<=r; d++) {
406                         char *s = fmt_u(*d, buf+9);
407                         if (d!=a) while (s>buf) *--s='0';
408                         else if (s==buf+9) *--s='0';
409                         out(f, s, buf+9-s);
410                 }
411                 if (p || (fl&ALT_FORM)) out(f, ".", 1);
412                 for (; d<z && p>0; d++, p-=9) {
413                         char *s = fmt_u(*d, buf+9);
414                         while (s>buf) *--s='0';
415                         out(f, s, MIN(9,p));
416                 }
417                 pad(f, '0', p+9, 9, 0);
418         } else {
419                 if (z<=a) z=a+1;
420                 for (d=a; d<z && p>=0; d++) {
421                         char *s = fmt_u(*d, buf+9);
422                         if (s==buf+9) *--s='0';
423                         if (d!=a) while (s>buf) *--s='0';
424                         else {
425                                 out(f, s++, 1);
426                                 if (p>0||(fl&ALT_FORM)) out(f, ".", 1);
427                         }
428                         out(f, s, MIN(buf+9-s, p));
429                         p -= buf+9-s;
430                 }
431                 pad(f, '0', p+18, 18, 0);
432                 out(f, estr, ebuf-estr);
433         }
434
435         pad(f, ' ', w, pl+l, fl^LEFT_ADJ);
436
437         return MAX(w, pl+l);
438 }
439
440 static int getint(char **s) {
441         int i;
442         for (i=0; isdigit(**s); (*s)++)
443                 i = 10*i + (**s-'0');
444         return i;
445 }
446
447 static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg, int *nl_type)
448 {
449         char *a, *z, *s=(char *)fmt;
450         unsigned l10n=0, fl;
451         int w, p;
452         union arg arg;
453         int argpos;
454         unsigned st, ps;
455         int cnt=0, l=0;
456         int i;
457         char buf[sizeof(uintmax_t)*3+3+LDBL_MANT_DIG/4];
458         const char *prefix;
459         int t, pl;
460         wchar_t wc[2], *ws;
461         char mb[4];
462
463         for (;;) {
464                 /* Update output count, end loop when fmt is exhausted */
465                 if (cnt >= 0) {
466                         if (l > INT_MAX - cnt) {
467                                 errno = EOVERFLOW;
468                                 cnt = -1;
469                         } else cnt += l;
470                 }
471                 if (!*s) break;
472
473                 /* Handle literal text and %% format specifiers */
474                 for (a=s; *s && *s!='%'; s++);
475                 for (z=s; s[0]=='%' && s[1]=='%'; z++, s+=2);
476                 l = z-a;
477                 if (f) out(f, a, l);
478                 if (l) continue;
479
480                 if (isdigit(s[1]) && s[2]=='$') {
481                         l10n=1;
482                         argpos = s[1]-'0';
483                         s+=3;
484                 } else {
485                         argpos = -1;
486                         s++;
487                 }
488
489                 /* Read modifier flags */
490                 for (fl=0; (unsigned)*s-' '<32 && (FLAGMASK&(1U<<*s-' ')); s++)
491                         fl |= 1U<<*s-' ';
492
493                 /* Read field width */
494                 if (*s=='*') {
495                         if (isdigit(s[1]) && s[2]=='$') {
496                                 l10n=1;
497                                 nl_type[s[1]-'0'] = INT;
498                                 w = nl_arg[s[1]-'0'].i;
499                                 s+=3;
500                         } else if (!l10n) {
501                                 w = f ? va_arg(*ap, int) : 0;
502                                 s++;
503                         } else return -1;
504                         if (w<0) fl|=LEFT_ADJ, w=-w;
505                 } else if ((w=getint(&s))<0) return -1;
506
507                 /* Read precision */
508                 if (*s=='.' && s[1]=='*') {
509                         if (isdigit(s[2]) && s[3]=='$') {
510                                 nl_type[s[2]-'0'] = INT;
511                                 p = nl_arg[s[2]-'0'].i;
512                                 s+=4;
513                         } else if (!l10n) {
514                                 p = f ? va_arg(*ap, int) : 0;
515                                 s+=2;
516                         } else return -1;
517                 } else if (*s=='.') {
518                         s++;
519                         p = getint(&s);
520                 } else p = -1;
521
522                 /* Format specifier state machine */
523                 st=0;
524                 do {
525                         if (OOB(*s)) return -1;
526                         ps=st;
527                         st=states[st]S(*s++);
528                 } while (st-1<STOP);
529                 if (!st) return -1;
530
531                 /* Check validity of argument type (nl/normal) */
532                 if (st==NOARG) {
533                         if (argpos>=0) return -1;
534                 } else {
535                         if (argpos>=0) nl_type[argpos]=st, arg=nl_arg[argpos];
536                         else if (f) pop_arg(&arg, st, ap);
537                         else return 0;
538                 }
539
540                 if (!f) continue;
541
542                 z = buf + sizeof(buf);
543                 prefix = "-+   0X0x";
544                 pl = 0;
545                 t = s[-1];
546
547                 /* Transform ls,lc -> S,C */
548                 if (ps && (t&15)==3) t&=~32;
549
550                 /* - and 0 flags are mutually exclusive */
551                 if (fl & LEFT_ADJ) fl &= ~ZERO_PAD;
552
553                 switch(t) {
554                 case 'n':
555                         switch(ps) {
556                         case BARE: *(int *)arg.p = cnt; break;
557                         case LPRE: *(long *)arg.p = cnt; break;
558                         case LLPRE: *(long long *)arg.p = cnt; break;
559                         case HPRE: *(unsigned short *)arg.p = cnt; break;
560                         case HHPRE: *(unsigned char *)arg.p = cnt; break;
561                         case ZTPRE: *(size_t *)arg.p = cnt; break;
562                         case JPRE: *(uintmax_t *)arg.p = cnt; break;
563                         }
564                         continue;
565                 case 'p':
566                         p = MAX(p, 2*sizeof(void*));
567                         t = 'x';
568                         fl |= ALT_FORM;
569                 case 'x': case 'X':
570                         a = fmt_x(arg.i, z, t&32);
571                         if (arg.i && (fl & ALT_FORM)) prefix+=(t>>4), pl=2;
572                         if (0) {
573                 case 'o':
574                         a = fmt_o(arg.i, z);
575                         if ((fl&ALT_FORM) && arg.i) prefix+=5, pl=1;
576                         } if (0) {
577                 case 'd': case 'i':
578                         pl=1;
579                         if (arg.i>INTMAX_MAX) {
580                                 arg.i=-arg.i;
581                         } else if (fl & MARK_POS) {
582                                 prefix++;
583                         } else if (fl & PAD_POS) {
584                                 prefix+=2;
585                         } else pl=0;
586                 case 'u':
587                         a = fmt_u(arg.i, z);
588                         }
589                         if (p>=0) fl &= ~ZERO_PAD;
590                         if (!arg.i && !p) {
591                                 a=z;
592                                 break;
593                         }
594                         p = MAX(p, z-a + !arg.i);
595                         break;
596                 case 'c':
597                         *(a=z-(p=1))=arg.i;
598                         fl &= ~ZERO_PAD;
599                         break;
600                 case 'm':
601                         if (1) a = strerror(errno); else
602                 case 's':
603                         a = arg.p ? arg.p : "(null)";
604                         z = memchr(a, 0, p);
605                         if (!z) z=a+p;
606                         else p=z-a;
607                         fl &= ~ZERO_PAD;
608                         break;
609                 case 'C':
610                         wc[0] = arg.i;
611                         wc[1] = 0;
612                         arg.p = wc;
613                         p = -1;
614                 case 'S':
615                         ws = arg.p;
616                         for (i=l=0; i<0U+p && *ws && (l=wctomb(mb, *ws++))>=0 && l<=0U+p-i; i+=l);
617                         if (l<0) return -1;
618                         p = i;
619                         pad(f, ' ', w, p, fl);
620                         ws = arg.p;
621                         for (i=0; i<0U+p && *ws && i+(l=wctomb(mb, *ws++))<=p; i+=l)
622                                 out(f, mb, l);
623                         pad(f, ' ', w, p, fl^LEFT_ADJ);
624                         l = w>p ? w : p;
625                         continue;
626                 case 'e': case 'f': case 'g': case 'a':
627                 case 'E': case 'F': case 'G': case 'A':
628                         l = fmt_fp(f, arg.f, w, p, fl, t);
629                         continue;
630                 }
631
632                 if (p < z-a) p = z-a;
633                 if (w < pl+p) w = pl+p;
634
635                 pad(f, ' ', w, pl+p, fl);
636                 out(f, prefix, pl);
637                 pad(f, '0', w, pl+p, fl^ZERO_PAD);
638                 pad(f, '0', p, z-a, 0);
639                 out(f, a, z-a);
640                 pad(f, ' ', w, pl+p, fl^LEFT_ADJ);
641
642                 l = w;
643         }
644
645         if (f) return cnt;
646         if (!l10n) return 0;
647
648         for (i=1; i<=NL_ARGMAX && nl_type[i]; i++)
649                 pop_arg(nl_arg+i, nl_type[i], ap);
650         for (; i<=NL_ARGMAX && !nl_type[i]; i++);
651         if (i<=NL_ARGMAX) return -1;
652         return 1;
653 }
654
655 int vfprintf(FILE *restrict f, const char *restrict fmt, va_list ap)
656 {
657         va_list ap2;
658         int nl_type[NL_ARGMAX+1] = {0};
659         union arg nl_arg[NL_ARGMAX+1];
660         unsigned char internal_buf[80], *saved_buf = 0;
661         int ret;
662
663         /* the copy allows passing va_list* even if va_list is an array */
664         va_copy(ap2, ap);
665         if (printf_core(0, fmt, &ap2, nl_arg, nl_type) < 0) {
666                 va_end(ap2);
667                 return -1;
668         }
669
670         FLOCK(f);
671         if (!f->buf_size) {
672                 saved_buf = f->buf;
673                 f->wpos = f->wbase = f->buf = internal_buf;
674                 f->buf_size = sizeof internal_buf;
675                 f->wend = internal_buf + sizeof internal_buf;
676         }
677         ret = printf_core(f, fmt, &ap2, nl_arg, nl_type);
678         if (saved_buf) {
679                 f->write(f, 0, 0);
680                 if (!f->wpos) ret = -1;
681                 f->buf = saved_buf;
682                 f->buf_size = 0;
683                 f->wpos = f->wbase = f->wend = 0;
684         }
685         FUNLOCK(f);
686         va_end(ap2);
687         return ret;
688 }