fix wide printf continuation after output or encoding errors
[musl] / src / stdio / vfwprintf.c
index 42ce304..119fdff 100644 (file)
@@ -1,4 +1,13 @@
 #include "stdio_impl.h"
+#include <errno.h>
+#include <ctype.h>
+#include <limits.h>
+#include <string.h>
+#include <stdarg.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <wchar.h>
+#include <inttypes.h>
 
 /* Convenient bit representation for modifier flags, which all fall
  * within 31 codepoints of the space character. */
 
 #define FLAGMASK (ALT_FORM|ZERO_PAD|LEFT_ADJ|PAD_POS|MARK_POS|GROUPED)
 
-#if UINT_MAX == ULONG_MAX
-#define LONG_IS_INT
-#endif
-
-#if SIZE_MAX != ULONG_MAX || UINTMAX_MAX != ULLONG_MAX
-#define ODD_TYPES
-#endif
-
 /* State machine to accept length modifiers + conversion specifiers.
  * Result is 0 on failure, or an argument type to pop on success. */
 
@@ -28,23 +29,9 @@ enum {
        ZTPRE, JPRE,
        STOP,
        PTR, INT, UINT, ULLONG,
-#ifndef LONG_IS_INT
        LONG, ULONG,
-#else
-#define LONG INT
-#define ULONG UINT
-#endif
        SHORT, USHORT, CHAR, UCHAR,
-#ifdef ODD_TYPES
        LLONG, SIZET, IMAX, UMAX, PDIFF, UIPTR,
-#else
-#define LLONG ULLONG
-#define SIZET ULONG
-#define IMAX LLONG
-#define UMAX ULLONG
-#define PDIFF LONG
-#define UIPTR ULONG
-#endif
        DBL, LDBL,
        NOARG,
        MAXSTATE
@@ -66,6 +53,8 @@ static const unsigned char states[]['z'-'A'+1] = {
        }, { /* 1: l-prefixed */
                S('d') = LONG, S('i') = LONG,
                S('o') = ULONG, S('u') = ULONG, S('x') = ULONG, S('X') = ULONG,
+               S('e') = DBL, S('f') = DBL, S('g') = DBL, S('a') = DBL,
+               S('E') = DBL, S('F') = DBL, S('G') = DBL, S('A') = DBL,
                S('c') = INT, S('s') = PTR, S('n') = PTR,
                S('l') = LLPRE,
        }, { /* 2: ll-prefixed */
@@ -112,29 +101,23 @@ union arg
 
 static void pop_arg(union arg *arg, int type, va_list *ap)
 {
-       /* Give the compiler a hint for optimizing the switch. */
-       if ((unsigned)type > MAXSTATE) return;
        switch (type) {
               case PTR:        arg->p = va_arg(*ap, void *);
        break; case INT:        arg->i = va_arg(*ap, int);
        break; case UINT:       arg->i = va_arg(*ap, unsigned int);
-#ifndef LONG_IS_INT
        break; case LONG:       arg->i = va_arg(*ap, long);
        break; case ULONG:      arg->i = va_arg(*ap, unsigned long);
-#endif
        break; case ULLONG:     arg->i = va_arg(*ap, unsigned long long);
        break; case SHORT:      arg->i = (short)va_arg(*ap, int);
        break; case USHORT:     arg->i = (unsigned short)va_arg(*ap, int);
        break; case CHAR:       arg->i = (signed char)va_arg(*ap, int);
        break; case UCHAR:      arg->i = (unsigned char)va_arg(*ap, int);
-#ifdef ODD_TYPES
        break; case LLONG:      arg->i = va_arg(*ap, long long);
        break; case SIZET:      arg->i = va_arg(*ap, size_t);
        break; case IMAX:       arg->i = va_arg(*ap, intmax_t);
        break; case UMAX:       arg->i = va_arg(*ap, uintmax_t);
        break; case PDIFF:      arg->i = va_arg(*ap, ptrdiff_t);
        break; case UIPTR:      arg->i = (uintptr_t)va_arg(*ap, void *);
-#endif
        break; case DBL:        arg->f = va_arg(*ap, double);
        break; case LDBL:       arg->f = va_arg(*ap, long double);
        }
@@ -142,13 +125,15 @@ static void pop_arg(union arg *arg, int type, va_list *ap)
 
 static void out(FILE *f, const wchar_t *s, size_t l)
 {
-       while (l--) fputwc(*s++, f);
+       while (l-- && !(f->flags & F_ERR)) fputwc(*s++, f);
 }
 
 static int getint(wchar_t **s) {
        int i;
-       for (i=0; iswdigit(**s); (*s)++)
-               i = 10*i + (**s-'0');
+       for (i=0; iswdigit(**s); (*s)++) {
+               if (i > INT_MAX/10U || **s-'0' > INT_MAX-10*i) i = -1;
+               else i = 10*i + (**s-'0');
+       }
        return i;
 }
 
@@ -160,9 +145,9 @@ static const char sizeprefix['y'-'a'] = {
 
 static int wprintf_core(FILE *f, const wchar_t *fmt, va_list *ap, union arg *nl_arg, int *nl_type)
 {
-       wchar_t *a, *z, *s=(wchar_t *)fmt, *s0;
-       unsigned l10n=0, litpct, fl;
-       int w, p;
+       wchar_t *a, *z, *s=(wchar_t *)fmt;
+       unsigned l10n=0, fl;
+       int w, p, xp;
        union arg arg;
        int argpos;
        unsigned st, ps;
@@ -174,20 +159,19 @@ static int wprintf_core(FILE *f, const wchar_t *fmt, va_list *ap, union arg *nl_
        wchar_t wc;
 
        for (;;) {
+               /* This error is only specified for snprintf, but since it's
+                * unspecified for other forms, do the same. Stop immediately
+                * on overflow; otherwise %n could produce wrong results. */
+               if (l > INT_MAX - cnt) goto overflow;
+
                /* Update output count, end loop when fmt is exhausted */
-               if (cnt >= 0) {
-                       if (l > INT_MAX - cnt) {
-                               if (!ferror(f)) errno = EOVERFLOW;
-                               cnt = -1;
-                       } else cnt += l;
-               }
+               cnt += l;
                if (!*s) break;
 
                /* Handle literal text and %% format specifiers */
                for (a=s; *s && *s!='%'; s++);
-               litpct = wcsspn(s, L"%")/2; /* Optimize %%%% runs */
-               z = s+litpct;
-               s += 2*litpct;
+               for (z=s; s[0]=='%' && s[1]=='%'; z++, s+=2);
+               if (z-a > INT_MAX-cnt) goto overflow;
                l = z-a;
                if (f) out(f, a, l);
                if (l) continue;
@@ -215,9 +199,9 @@ static int wprintf_core(FILE *f, const wchar_t *fmt, va_list *ap, union arg *nl_
                        } else if (!l10n) {
                                w = f ? va_arg(*ap, int) : 0;
                                s++;
-                       } else return -1;
+                       } else goto inval;
                        if (w<0) fl|=LEFT_ADJ, w=-w;
-               } else if ((w=getint(&s))<0) return -1;
+               } else if ((w=getint(&s))<0) goto overflow;
 
                /* Read precision */
                if (*s=='.' && s[1]=='*') {
@@ -228,26 +212,29 @@ static int wprintf_core(FILE *f, const wchar_t *fmt, va_list *ap, union arg *nl_
                        } else if (!l10n) {
                                p = f ? va_arg(*ap, int) : 0;
                                s+=2;
-                       } else return -1;
+                       } else goto inval;
+                       xp = (p>=0);
                } else if (*s=='.') {
                        s++;
                        p = getint(&s);
-               } else p = -1;
+                       xp = 1;
+               } else {
+                       p = -1;
+                       xp = 0;
+               }
 
                /* Format specifier state machine */
-               s0=s;
                st=0;
                do {
-                       if (OOB(*s)) return -1;
+                       if (OOB(*s)) goto inval;
                        ps=st;
                        st=states[st]S(*s++);
                } while (st-1<STOP);
-               if (!st) return -1;
+               if (!st) goto inval;
 
                /* Check validity of argument type (nl/normal) */
                if (st==NOARG) {
-                       if (argpos>=0) return -1;
-                       else if (!f) continue;
+                       if (argpos>=0) goto inval;
                } else {
                        if (argpos>=0) nl_type[argpos]=st, arg=nl_arg[argpos];
                        else if (f) pop_arg(&arg, st, ap);
@@ -255,6 +242,10 @@ static int wprintf_core(FILE *f, const wchar_t *fmt, va_list *ap, union arg *nl_
                }
 
                if (!f) continue;
+
+               /* Do not process any new directives once in error state. */
+               if (ferror(f)) return -1;
+
                t = s[-1];
                if (ps && (t&15)==3) t&=~32;
 
@@ -271,43 +262,47 @@ static int wprintf_core(FILE *f, const wchar_t *fmt, va_list *ap, union arg *nl_
                        }
                        continue;
                case 'c':
-                       fputwc(btowc(arg.i), f);
-                       l = 1;
-                       continue;
                case 'C':
-                       fputwc(arg.i, f);
-                       l = 1;
+                       if (w<1) w=1;
+                       if (w>1 && !(fl&LEFT_ADJ)) fprintf(f, "%*s", w-1, "");
+                       out(f, &(wchar_t){t=='C' ? arg.i : btowc(arg.i)}, 1);
+                       if (w>1 && (fl&LEFT_ADJ)) fprintf(f, "%*s", w-1, "");
+                       l = w;
                        continue;
                case 'S':
                        a = arg.p;
-                       z = wmemchr(a, 0, p);
-                       if (!z) z=a+p;
-                       else p=z-a;
+                       z = a + wcsnlen(a, p<0 ? INT_MAX : p);
+                       if (p<0 && *z) goto overflow;
+                       p = z-a;
                        if (w<p) w=p;
-                       if (!(fl&LEFT_ADJ)) fprintf(f, "%.*s", w-p, "");
+                       if (!(fl&LEFT_ADJ)) fprintf(f, "%*s", w-p, "");
                        out(f, a, p);
-                       if ((fl&LEFT_ADJ)) fprintf(f, "%.*s", w-p, "");
+                       if ((fl&LEFT_ADJ)) fprintf(f, "%*s", w-p, "");
                        l=w;
                        continue;
+               case 'm':
+                       arg.p = strerror(errno);
                case 's':
+                       if (!arg.p) arg.p = "(null)";
                        bs = arg.p;
-                       if (p<0) p = INT_MAX;
-                       for (l=0; l<p && (i=mbtowc(&wc, bs, MB_LEN_MAX))>0; bs+=i, l++);
+                       for (i=l=0; l<(p<0?INT_MAX:p) && (i=mbtowc(&wc, bs, MB_LEN_MAX))>0; bs+=i, l++);
                        if (i<0) return -1;
+                       if (p<0 && *bs) goto overflow;
                        p=l;
                        if (w<p) w=p;
-                       if (!(fl&LEFT_ADJ)) fprintf(f, "%.*s", w-p, "");
+                       if (!(fl&LEFT_ADJ)) fprintf(f, "%*s", w-p, "");
                        bs = arg.p;
                        while (l--) {
                                i=mbtowc(&wc, bs, MB_LEN_MAX);
                                bs+=i;
-                               fputwc(wc, f);
+                               out(f, &wc, 1);
                        }
-                       if ((fl&LEFT_ADJ)) fprintf(f, "%.*s", w-p, "");
+                       if ((fl&LEFT_ADJ)) fprintf(f, "%*s", w-p, "");
                        l=w;
                        continue;
                }
 
+               if (xp && p<0) goto overflow;
                snprintf(charfmt, sizeof charfmt, "%%%s%s%s%s%s*.*%c%c",
                        "#"+!(fl & ALT_FORM),
                        "+"+!(fl & MARK_POS),
@@ -334,20 +329,37 @@ static int wprintf_core(FILE *f, const wchar_t *fmt, va_list *ap, union arg *nl_
        for (; i<=NL_ARGMAX && !nl_type[i]; i++);
        if (i<=NL_ARGMAX) return -1;
        return 1;
+
+inval:
+       errno = EINVAL;
+       return -1;
+overflow:
+       errno = EOVERFLOW;
+       return -1;
 }
 
-int vfwprintf(FILE *f, const wchar_t *fmt, va_list ap)
+int vfwprintf(FILE *restrict f, const wchar_t *restrict fmt, va_list ap)
 {
        va_list ap2;
        int nl_type[NL_ARGMAX] = {0};
        union arg nl_arg[NL_ARGMAX];
+       int olderr;
        int ret;
 
+       /* the copy allows passing va_list* even if va_list is an array */
        va_copy(ap2, ap);
-       if (wprintf_core(0, fmt, &ap2, nl_arg, nl_type) < 0) return -1;
+       if (wprintf_core(0, fmt, &ap2, nl_arg, nl_type) < 0) {
+               va_end(ap2);
+               return -1;
+       }
 
        FLOCK(f);
+       fwide(f, 1);
+       olderr = f->flags & F_ERR;
+       f->flags &= ~F_ERR;
        ret = wprintf_core(f, fmt, &ap2, nl_arg, nl_type);
+       if (f->flags & F_ERR) ret = -1;
+       f->flags |= olderr;
        FUNLOCK(f);
        va_end(ap2);
        return ret;