explicitly reject empty names in dynamic linker load_library function
[musl] / src / stdio / vfwprintf.c
index 6282980..c640059 100644 (file)
@@ -1,4 +1,11 @@
 #include "stdio_impl.h"
+#include <errno.h>
+#include <ctype.h>
+#include <limits.h>
+#include <string.h>
+#include <stdarg.h>
+#include <wchar.h>
+#include <inttypes.h>
 
 /* Convenient bit representation for modifier flags, which all fall
  * within 31 codepoints of the space character. */
@@ -160,7 +167,7 @@ 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;
+       wchar_t *a, *z, *s=(wchar_t *)fmt;
        unsigned l10n=0, litpct, fl;
        int w, p;
        union arg arg;
@@ -235,7 +242,6 @@ static int wprintf_core(FILE *f, const wchar_t *fmt, va_list *ap, union arg *nl_
                } else p = -1;
 
                /* Format specifier state machine */
-               s0=s;
                st=0;
                do {
                        if (OOB(*s)) return -1;
@@ -247,7 +253,6 @@ static int wprintf_core(FILE *f, const wchar_t *fmt, va_list *ap, union arg *nl_
                /* Check validity of argument type (nl/normal) */
                if (st==NOARG) {
                        if (argpos>=0) return -1;
-                       else if (!f) continue;
                } else {
                        if (argpos>=0) nl_type[argpos]=st, arg=nl_arg[argpos];
                        else if (f) pop_arg(&arg, st, ap);
@@ -281,8 +286,7 @@ static int wprintf_core(FILE *f, const wchar_t *fmt, va_list *ap, union arg *nl_
                case 'S':
                        a = arg.p;
                        z = wmemchr(a, 0, p);
-                       if (!z) z=a+p;
-                       else p=z-a;
+                       if (z) p=z-a;
                        if (w<p) w=p;
                        if (!(fl&LEFT_ADJ)) fprintf(f, "%.*s", w-p, "");
                        out(f, a, p);
@@ -336,17 +340,22 @@ static int wprintf_core(FILE *f, const wchar_t *fmt, va_list *ap, union arg *nl_
        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 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);
+       f->mode |= f->mode+1;
        ret = wprintf_core(f, fmt, &ap2, nl_arg, nl_type);
        FUNLOCK(f);
        va_end(ap2);