fix integer overflows and uncaught EOVERFLOW in printf core
[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
17 /* Convenient bit representation for modifier flags, which all fall
18  * within 31 codepoints of the space character. */
19
20 #define ALT_FORM   (1U<<'#'-' ')
21 #define ZERO_PAD   (1U<<'0'-' ')
22 #define LEFT_ADJ   (1U<<'-'-' ')
23 #define PAD_POS    (1U<<' '-' ')
24 #define MARK_POS   (1U<<'+'-' ')
25 #define GROUPED    (1U<<'\''-' ')
26
27 #define FLAGMASK (ALT_FORM|ZERO_PAD|LEFT_ADJ|PAD_POS|MARK_POS|GROUPED)
28
29 #if UINT_MAX == ULONG_MAX
30 #define LONG_IS_INT
31 #endif
32
33 #if SIZE_MAX != ULONG_MAX || UINTMAX_MAX != ULLONG_MAX
34 #define ODD_TYPES
35 #endif
36
37 /* State machine to accept length modifiers + conversion specifiers.
38  * Result is 0 on failure, or an argument type to pop on success. */
39
40 enum {
41         BARE, LPRE, LLPRE, HPRE, HHPRE, BIGLPRE,
42         ZTPRE, JPRE,
43         STOP,
44         PTR, INT, UINT, ULLONG,
45 #ifndef LONG_IS_INT
46         LONG, ULONG,
47 #else
48 #define LONG INT
49 #define ULONG UINT
50 #endif
51         SHORT, USHORT, CHAR, UCHAR,
52 #ifdef ODD_TYPES
53         LLONG, SIZET, IMAX, UMAX, PDIFF, UIPTR,
54 #else
55 #define LLONG ULLONG
56 #define SIZET ULONG
57 #define IMAX LLONG
58 #define UMAX ULLONG
59 #define PDIFF LONG
60 #define UIPTR ULONG
61 #endif
62         DBL, LDBL,
63         NOARG,
64         MAXSTATE
65 };
66
67 #define S(x) [(x)-'A']
68
69 static const unsigned char states[]['z'-'A'+1] = {
70         { /* 0: bare types */
71                 S('d') = INT, S('i') = INT,
72                 S('o') = UINT, S('u') = UINT, S('x') = UINT, S('X') = UINT,
73                 S('e') = DBL, S('f') = DBL, S('g') = DBL, S('a') = DBL,
74                 S('E') = DBL, S('F') = DBL, S('G') = DBL, S('A') = DBL,
75                 S('c') = CHAR, S('C') = INT,
76                 S('s') = PTR, S('S') = PTR, S('p') = UIPTR, S('n') = PTR,
77                 S('m') = NOARG,
78                 S('l') = LPRE, S('h') = HPRE, S('L') = BIGLPRE,
79                 S('z') = ZTPRE, S('j') = JPRE, S('t') = ZTPRE,
80         }, { /* 1: l-prefixed */
81                 S('d') = LONG, S('i') = LONG,
82                 S('o') = ULONG, S('u') = ULONG, S('x') = ULONG, S('X') = ULONG,
83                 S('e') = DBL, S('f') = DBL, S('g') = DBL, S('a') = DBL,
84                 S('E') = DBL, S('F') = DBL, S('G') = DBL, S('A') = DBL,
85                 S('c') = INT, S('s') = PTR, S('n') = PTR,
86                 S('l') = LLPRE,
87         }, { /* 2: ll-prefixed */
88                 S('d') = LLONG, S('i') = LLONG,
89                 S('o') = ULLONG, S('u') = ULLONG,
90                 S('x') = ULLONG, S('X') = ULLONG,
91                 S('n') = PTR,
92         }, { /* 3: h-prefixed */
93                 S('d') = SHORT, S('i') = SHORT,
94                 S('o') = USHORT, S('u') = USHORT,
95                 S('x') = USHORT, S('X') = USHORT,
96                 S('n') = PTR,
97                 S('h') = HHPRE,
98         }, { /* 4: hh-prefixed */
99                 S('d') = CHAR, S('i') = CHAR,
100                 S('o') = UCHAR, S('u') = UCHAR,
101                 S('x') = UCHAR, S('X') = UCHAR,
102                 S('n') = PTR,
103         }, { /* 5: L-prefixed */
104                 S('e') = LDBL, S('f') = LDBL, S('g') = LDBL, S('a') = LDBL,
105                 S('E') = LDBL, S('F') = LDBL, S('G') = LDBL, S('A') = LDBL,
106                 S('n') = PTR,
107         }, { /* 6: z- or t-prefixed (assumed to be same size) */
108                 S('d') = PDIFF, S('i') = PDIFF,
109                 S('o') = SIZET, S('u') = SIZET,
110                 S('x') = SIZET, S('X') = SIZET,
111                 S('n') = PTR,
112         }, { /* 7: j-prefixed */
113                 S('d') = IMAX, S('i') = IMAX,
114                 S('o') = UMAX, S('u') = UMAX,
115                 S('x') = UMAX, S('X') = UMAX,
116                 S('n') = PTR,
117         }
118 };
119
120 #define OOB(x) ((unsigned)(x)-'A' > 'z'-'A')
121
122 union arg
123 {
124         uintmax_t i;
125         long double f;
126         void *p;
127 };
128
129 static void pop_arg(union arg *arg, int type, va_list *ap)
130 {
131         /* Give the compiler a hint for optimizing the switch. */
132         if ((unsigned)type > MAXSTATE) return;
133         switch (type) {
134                case PTR:        arg->p = va_arg(*ap, void *);
135         break; case INT:        arg->i = va_arg(*ap, int);
136         break; case UINT:       arg->i = va_arg(*ap, unsigned int);
137 #ifndef LONG_IS_INT
138         break; case LONG:       arg->i = va_arg(*ap, long);
139         break; case ULONG:      arg->i = va_arg(*ap, unsigned long);
140 #endif
141         break; case ULLONG:     arg->i = va_arg(*ap, unsigned long long);
142         break; case SHORT:      arg->i = (short)va_arg(*ap, int);
143         break; case USHORT:     arg->i = (unsigned short)va_arg(*ap, int);
144         break; case CHAR:       arg->i = (signed char)va_arg(*ap, int);
145         break; case UCHAR:      arg->i = (unsigned char)va_arg(*ap, int);
146 #ifdef ODD_TYPES
147         break; case LLONG:      arg->i = va_arg(*ap, long long);
148         break; case SIZET:      arg->i = va_arg(*ap, size_t);
149         break; case IMAX:       arg->i = va_arg(*ap, intmax_t);
150         break; case UMAX:       arg->i = va_arg(*ap, uintmax_t);
151         break; case PDIFF:      arg->i = va_arg(*ap, ptrdiff_t);
152         break; case UIPTR:      arg->i = (uintptr_t)va_arg(*ap, void *);
153 #endif
154         break; case DBL:        arg->f = va_arg(*ap, double);
155         break; case LDBL:       arg->f = va_arg(*ap, long double);
156         }
157 }
158
159 static void out(FILE *f, const char *s, size_t l)
160 {
161         if (!(f->flags & F_ERR)) __fwritex((void *)s, l, f);
162 }
163
164 static void pad(FILE *f, char c, int w, int l, int fl)
165 {
166         char pad[256];
167         if (fl & (LEFT_ADJ | ZERO_PAD) || l >= w) return;
168         l = w - l;
169         memset(pad, c, l>sizeof pad ? sizeof pad : l);
170         for (; l >= sizeof pad; l -= sizeof pad)
171                 out(f, pad, sizeof pad);
172         out(f, pad, l);
173 }
174
175 static const char xdigits[16] = {
176         "0123456789ABCDEF"
177 };
178
179 static char *fmt_x(uintmax_t x, char *s, int lower)
180 {
181         for (; x; x>>=4) *--s = xdigits[(x&15)]|lower;
182         return s;
183 }
184
185 static char *fmt_o(uintmax_t x, char *s)
186 {
187         for (; x; x>>=3) *--s = '0' + (x&7);
188         return s;
189 }
190
191 static char *fmt_u(uintmax_t x, char *s)
192 {
193         unsigned long y;
194         for (   ; x>ULONG_MAX; x/=10) *--s = '0' + x%10;
195         for (y=x;           y; y/=10) *--s = '0' + y%10;
196         return s;
197 }
198
199 /* Do not override this check. The floating point printing code below
200  * depends on the float.h constants being right. If they are wrong, it
201  * may overflow the stack. */
202 #if LDBL_MANT_DIG == 53
203 typedef char compiler_defines_long_double_incorrectly[9-(int)sizeof(long double)];
204 #endif
205
206 static int fmt_fp(FILE *f, long double y, int w, int p, int fl, int t)
207 {
208         uint32_t big[(LDBL_MANT_DIG+28)/29 + 1          // mantissa expansion
209                 + (LDBL_MAX_EXP+LDBL_MANT_DIG+28+8)/9]; // exponent expansion
210         uint32_t *a, *d, *r, *z;
211         int e2=0, e, i, j, l;
212         char buf[9+LDBL_MANT_DIG/4], *s;
213         const char *prefix="-0X+0X 0X-0x+0x 0x";
214         int pl;
215         char ebuf0[3*sizeof(int)], *ebuf=&ebuf0[3*sizeof(int)], *estr;
216
217         pl=1;
218         if (signbit(y)) {
219                 y=-y;
220         } else if (fl & MARK_POS) {
221                 prefix+=3;
222         } else if (fl & PAD_POS) {
223                 prefix+=6;
224         } else prefix++, pl=0;
225
226         if (!isfinite(y)) {
227                 char *s = (t&32)?"inf":"INF";
228                 if (y!=y) s=(t&32)?"nan":"NAN";
229                 pad(f, ' ', w, 3+pl, fl&~ZERO_PAD);
230                 out(f, prefix, pl);
231                 out(f, s, 3);
232                 pad(f, ' ', w, 3+pl, fl^LEFT_ADJ);
233                 return MAX(w, 3+pl);
234         }
235
236         y = frexpl(y, &e2) * 2;
237         if (y) e2--;
238
239         if ((t|32)=='a') {
240                 long double round = 8.0;
241                 int re;
242
243                 if (t&32) prefix += 9;
244                 pl += 2;
245
246                 if (p<0 || p>=LDBL_MANT_DIG/4-1) re=0;
247                 else re=LDBL_MANT_DIG/4-1-p;
248
249                 if (re) {
250                         while (re--) round*=16;
251                         if (*prefix=='-') {
252                                 y=-y;
253                                 y-=round;
254                                 y+=round;
255                                 y=-y;
256                         } else {
257                                 y+=round;
258                                 y-=round;
259                         }
260                 }
261
262                 estr=fmt_u(e2<0 ? -e2 : e2, ebuf);
263                 if (estr==ebuf) *--estr='0';
264                 *--estr = (e2<0 ? '-' : '+');
265                 *--estr = t+('p'-'a');
266
267                 s=buf;
268                 do {
269                         int x=y;
270                         *s++=xdigits[x]|(t&32);
271                         y=16*(y-x);
272                         if (s-buf==1 && (y||p>0||(fl&ALT_FORM))) *s++='.';
273                 } while (y);
274
275                 if (p > INT_MAX-2-(ebuf-estr)-pl)
276                         return -1;
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 (carry) *--a = carry;
312                 while (z>a && !z[-1]) z--;
313                 e2-=sh;
314         }
315         while (e2<0) {
316                 uint32_t carry=0, *b;
317                 int sh=MIN(9,-e2), need=1+(p+LDBL_MANT_DIG/3U+8)/9;
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 > need) z = b+need;
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 = 2/LDBL_EPSILON;
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                                         if (d<a) *--a=0;
360                                         (*d)++;
361                                 }
362                                 for (i=10, e=9*(r-a); *a>=i; i*=10, e++);
363                         }
364                 }
365                 if (z>d+1) z=d+1;
366         }
367         for (; z>a && !z[-1]; z--);
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         if (p > INT_MAX-1-(p || (fl&ALT_FORM)))
389                 return -1;
390         l = 1 + p + (p || (fl&ALT_FORM));
391         if ((t|32)=='f') {
392                 if (e > INT_MAX-l) return -1;
393                 if (e>0) l+=e;
394         } else {
395                 estr=fmt_u(e<0 ? -e : e, ebuf);
396                 while(ebuf-estr<2) *--estr='0';
397                 *--estr = (e<0 ? '-' : '+');
398                 *--estr = t;
399                 if (ebuf-estr > INT_MAX-l) return -1;
400                 l += ebuf-estr;
401         }
402
403         if (l > INT_MAX-pl) return -1;
404         pad(f, ' ', w, pl+l, fl);
405         out(f, prefix, pl);
406         pad(f, '0', w, pl+l, fl^ZERO_PAD);
407
408         if ((t|32)=='f') {
409                 if (a>r) a=r;
410                 for (d=a; d<=r; d++) {
411                         char *s = fmt_u(*d, buf+9);
412                         if (d!=a) while (s>buf) *--s='0';
413                         else if (s==buf+9) *--s='0';
414                         out(f, s, buf+9-s);
415                 }
416                 if (p || (fl&ALT_FORM)) out(f, ".", 1);
417                 for (; d<z && p>0; d++, p-=9) {
418                         char *s = fmt_u(*d, buf+9);
419                         while (s>buf) *--s='0';
420                         out(f, s, MIN(9,p));
421                 }
422                 pad(f, '0', p+9, 9, 0);
423         } else {
424                 if (z<=a) z=a+1;
425                 for (d=a; d<z && p>=0; d++) {
426                         char *s = fmt_u(*d, buf+9);
427                         if (s==buf+9) *--s='0';
428                         if (d!=a) while (s>buf) *--s='0';
429                         else {
430                                 out(f, s++, 1);
431                                 if (p>0||(fl&ALT_FORM)) out(f, ".", 1);
432                         }
433                         out(f, s, MIN(buf+9-s, p));
434                         p -= buf+9-s;
435                 }
436                 pad(f, '0', p+18, 18, 0);
437                 out(f, estr, ebuf-estr);
438         }
439
440         pad(f, ' ', w, pl+l, fl^LEFT_ADJ);
441
442         return MAX(w, pl+l);
443 }
444
445 static int getint(char **s) {
446         int i;
447         for (i=0; isdigit(**s); (*s)++) {
448                 if (i > INT_MAX/10U || **s-'0' > INT_MAX-10*i) i = -1;
449                 else i = 10*i + (**s-'0');
450         }
451         return i;
452 }
453
454 static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg, int *nl_type)
455 {
456         char *a, *z, *s=(char *)fmt;
457         unsigned l10n=0, fl;
458         int w, p, xp;
459         union arg arg;
460         int argpos;
461         unsigned st, ps;
462         int cnt=0, l=0;
463         size_t i;
464         char buf[sizeof(uintmax_t)*3+3+LDBL_MANT_DIG/4];
465         const char *prefix;
466         int t, pl;
467         wchar_t wc[2], *ws;
468         char mb[4];
469
470         for (;;) {
471                 /* This error is only specified for snprintf, but since it's
472                  * unspecified for other forms, do the same. Stop immediately
473                  * on overflow; otherwise %n could produce wrong results. */
474                 if (l > INT_MAX - cnt) goto overflow;
475
476                 /* Update output count, end loop when fmt is exhausted */
477                 cnt += l;
478                 if (!*s) break;
479
480                 /* Handle literal text and %% format specifiers */
481                 for (a=s; *s && *s!='%'; s++);
482                 for (z=s; s[0]=='%' && s[1]=='%'; z++, s+=2);
483                 if (z-a > INT_MAX-cnt) goto overflow;
484                 l = z-a;
485                 if (f) out(f, a, l);
486                 if (l) continue;
487
488                 if (isdigit(s[1]) && s[2]=='$') {
489                         l10n=1;
490                         argpos = s[1]-'0';
491                         s+=3;
492                 } else {
493                         argpos = -1;
494                         s++;
495                 }
496
497                 /* Read modifier flags */
498                 for (fl=0; (unsigned)*s-' '<32 && (FLAGMASK&(1U<<*s-' ')); s++)
499                         fl |= 1U<<*s-' ';
500
501                 /* Read field width */
502                 if (*s=='*') {
503                         if (isdigit(s[1]) && s[2]=='$') {
504                                 l10n=1;
505                                 nl_type[s[1]-'0'] = INT;
506                                 w = nl_arg[s[1]-'0'].i;
507                                 s+=3;
508                         } else if (!l10n) {
509                                 w = f ? va_arg(*ap, int) : 0;
510                                 s++;
511                         } else goto inval;
512                         if (w<0) fl|=LEFT_ADJ, w=-w;
513                 } else if ((w=getint(&s))<0) goto overflow;
514
515                 /* Read precision */
516                 if (*s=='.' && s[1]=='*') {
517                         if (isdigit(s[2]) && s[3]=='$') {
518                                 nl_type[s[2]-'0'] = INT;
519                                 p = nl_arg[s[2]-'0'].i;
520                                 s+=4;
521                         } else if (!l10n) {
522                                 p = f ? va_arg(*ap, int) : 0;
523                                 s+=2;
524                         } else goto inval;
525                         xp = (p>=0);
526                 } else if (*s=='.') {
527                         s++;
528                         p = getint(&s);
529                         xp = 1;
530                 } else {
531                         p = -1;
532                         xp = 0;
533                 }
534
535                 /* Format specifier state machine */
536                 st=0;
537                 do {
538                         if (OOB(*s)) goto inval;
539                         ps=st;
540                         st=states[st]S(*s++);
541                 } while (st-1<STOP);
542                 if (!st) goto inval;
543
544                 /* Check validity of argument type (nl/normal) */
545                 if (st==NOARG) {
546                         if (argpos>=0) goto inval;
547                 } else {
548                         if (argpos>=0) nl_type[argpos]=st, arg=nl_arg[argpos];
549                         else if (f) pop_arg(&arg, st, ap);
550                         else return 0;
551                 }
552
553                 if (!f) continue;
554
555                 z = buf + sizeof(buf);
556                 prefix = "-+   0X0x";
557                 pl = 0;
558                 t = s[-1];
559
560                 /* Transform ls,lc -> S,C */
561                 if (ps && (t&15)==3) t&=~32;
562
563                 /* - and 0 flags are mutually exclusive */
564                 if (fl & LEFT_ADJ) fl &= ~ZERO_PAD;
565
566                 switch(t) {
567                 case 'n':
568                         switch(ps) {
569                         case BARE: *(int *)arg.p = cnt; break;
570                         case LPRE: *(long *)arg.p = cnt; break;
571                         case LLPRE: *(long long *)arg.p = cnt; break;
572                         case HPRE: *(unsigned short *)arg.p = cnt; break;
573                         case HHPRE: *(unsigned char *)arg.p = cnt; break;
574                         case ZTPRE: *(size_t *)arg.p = cnt; break;
575                         case JPRE: *(uintmax_t *)arg.p = cnt; break;
576                         }
577                         continue;
578                 case 'p':
579                         p = MAX(p, 2*sizeof(void*));
580                         t = 'x';
581                         fl |= ALT_FORM;
582                 case 'x': case 'X':
583                         a = fmt_x(arg.i, z, t&32);
584                         if (arg.i && (fl & ALT_FORM)) prefix+=(t>>4), pl=2;
585                         if (0) {
586                 case 'o':
587                         a = fmt_o(arg.i, z);
588                         if ((fl&ALT_FORM) && p<z-a+1) prefix+=5, pl=1;
589                         } if (0) {
590                 case 'd': case 'i':
591                         pl=1;
592                         if (arg.i>INTMAX_MAX) {
593                                 arg.i=-arg.i;
594                         } else if (fl & MARK_POS) {
595                                 prefix++;
596                         } else if (fl & PAD_POS) {
597                                 prefix+=2;
598                         } else pl=0;
599                 case 'u':
600                         a = fmt_u(arg.i, z);
601                         }
602                         if (xp && p<0) goto overflow;
603                         if (p>=0) fl &= ~ZERO_PAD;
604                         if (!arg.i && !p) {
605                                 a=z;
606                                 break;
607                         }
608                         p = MAX(p, z-a + !arg.i);
609                         break;
610                 case 'c':
611                         *(a=z-(p=1))=arg.i;
612                         fl &= ~ZERO_PAD;
613                         break;
614                 case 'm':
615                         if (1) a = strerror(errno); else
616                 case 's':
617                         a = arg.p ? arg.p : "(null)";
618                         z = a + strnlen(a, p<0 ? INT_MAX : p);
619                         if (p<0 && *z) goto overflow;
620                         p = z-a;
621                         fl &= ~ZERO_PAD;
622                         break;
623                 case 'C':
624                         wc[0] = arg.i;
625                         wc[1] = 0;
626                         arg.p = wc;
627                         p = -1;
628                 case 'S':
629                         ws = arg.p;
630                         for (i=l=0; i<p && *ws && (l=wctomb(mb, *ws++))>=0 && l<=p-i; i+=l);
631                         if (l<0) return -1;
632                         if (i > INT_MAX) goto overflow;
633                         p = i;
634                         pad(f, ' ', w, p, fl);
635                         ws = arg.p;
636                         for (i=0; i<0U+p && *ws && i+(l=wctomb(mb, *ws++))<=p; i+=l)
637                                 out(f, mb, l);
638                         pad(f, ' ', w, p, fl^LEFT_ADJ);
639                         l = w>p ? w : p;
640                         continue;
641                 case 'e': case 'f': case 'g': case 'a':
642                 case 'E': case 'F': case 'G': case 'A':
643                         if (xp && p<0) goto overflow;
644                         l = fmt_fp(f, arg.f, w, p, fl, t);
645                         if (l<0) goto overflow;
646                         continue;
647                 }
648
649                 if (p < z-a) p = z-a;
650                 if (p > INT_MAX-pl) goto overflow;
651                 if (w < pl+p) w = pl+p;
652                 if (w > INT_MAX-cnt) goto overflow;
653
654                 pad(f, ' ', w, pl+p, fl);
655                 out(f, prefix, pl);
656                 pad(f, '0', w, pl+p, fl^ZERO_PAD);
657                 pad(f, '0', p, z-a, 0);
658                 out(f, a, z-a);
659                 pad(f, ' ', w, pl+p, fl^LEFT_ADJ);
660
661                 l = w;
662         }
663
664         if (f) return cnt;
665         if (!l10n) return 0;
666
667         for (i=1; i<=NL_ARGMAX && nl_type[i]; i++)
668                 pop_arg(nl_arg+i, nl_type[i], ap);
669         for (; i<=NL_ARGMAX && !nl_type[i]; i++);
670         if (i<=NL_ARGMAX) goto inval;
671         return 1;
672
673 inval:
674         errno = EINVAL;
675         return -1;
676 overflow:
677         errno = EOVERFLOW;
678         return -1;
679 }
680
681 int vfprintf(FILE *restrict f, const char *restrict fmt, va_list ap)
682 {
683         va_list ap2;
684         int nl_type[NL_ARGMAX+1] = {0};
685         union arg nl_arg[NL_ARGMAX+1];
686         unsigned char internal_buf[80], *saved_buf = 0;
687         int olderr;
688         int ret;
689
690         /* the copy allows passing va_list* even if va_list is an array */
691         va_copy(ap2, ap);
692         if (printf_core(0, fmt, &ap2, nl_arg, nl_type) < 0) {
693                 va_end(ap2);
694                 return -1;
695         }
696
697         FLOCK(f);
698         olderr = f->flags & F_ERR;
699         if (f->mode < 1) f->flags &= ~F_ERR;
700         if (!f->buf_size) {
701                 saved_buf = f->buf;
702                 f->wpos = f->wbase = f->buf = internal_buf;
703                 f->buf_size = sizeof internal_buf;
704                 f->wend = internal_buf + sizeof internal_buf;
705         }
706         ret = printf_core(f, fmt, &ap2, nl_arg, nl_type);
707         if (saved_buf) {
708                 f->write(f, 0, 0);
709                 if (!f->wpos) ret = -1;
710                 f->buf = saved_buf;
711                 f->buf_size = 0;
712                 f->wpos = f->wbase = f->wend = 0;
713         }
714         if (f->flags & F_ERR) ret = -1;
715         f->flags |= olderr;
716         FUNLOCK(f);
717         va_end(ap2);
718         return ret;
719 }