fix wide printf continuation after output or encoding errors
[musl] / src / stdio / vfwprintf.c
index 0adf0b7..119fdff 100644 (file)
@@ -53,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 */
@@ -240,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;
 
@@ -256,16 +262,13 @@ static int wprintf_core(FILE *f, const wchar_t *fmt, va_list *ap, union arg *nl_
                        }
                        continue;
                case 'c':
+               case 'C':
                        if (w<1) w=1;
                        if (w>1 && !(fl&LEFT_ADJ)) fprintf(f, "%*s", w-1, "");
-                       fputwc(btowc(arg.i), f);
+                       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 'C':
-                       fputwc(arg.i, f);
-                       l = 1;
-                       continue;
                case 'S':
                        a = arg.p;
                        z = a + wcsnlen(a, p<0 ? INT_MAX : p);
@@ -292,7 +295,7 @@ static int wprintf_core(FILE *f, const wchar_t *fmt, va_list *ap, union arg *nl_
                        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, "");
                        l=w;