handle whitespace before %% in scanf
authorBartosz Brachaczek <b.brachaczek@gmail.com>
Sun, 9 Jul 2017 21:00:18 +0000 (23:00 +0200)
committerRich Felker <dalias@aerifal.cx>
Mon, 4 Sep 2017 20:59:38 +0000 (16:59 -0400)
this is mandated by C and POSIX standards and is in accordance with
glibc behavior.

src/stdio/vfscanf.c
src/stdio/vfwscanf.c

index d4d2454b08417de91621a40939c1970f9e42950a..9e030fc4441f0854f95eae1cf31ae3687fd71dd6 100644 (file)
@@ -89,15 +89,19 @@ int vfscanf(FILE *restrict f, const char *restrict fmt, va_list ap)
                        continue;
                }
                if (*p != '%' || p[1] == '%') {
-                       p += *p=='%';
                        shlim(f, 0);
-                       c = shgetc(f);
+                       if (*p == '%') {
+                               p++;
+                               while (isspace((c=shgetc(f))));
+                       } else {
+                               c = shgetc(f);
+                       }
                        if (c!=*p) {
                                shunget(f);
                                if (c<0) goto input_fail;
                                goto match_fail;
                        }
-                       pos++;
+                       pos += shcnt(f);
                        continue;
                }
 
index 1ebc5cef51882c822e29b2642dc42de705a3a342..a7cd09231a311f2a4abc8dba4cd5a3921ff65bc1 100644 (file)
@@ -117,8 +117,12 @@ int vfwscanf(FILE *restrict f, const wchar_t *restrict fmt, va_list ap)
                        continue;
                }
                if (*p != '%' || p[1] == '%') {
-                       p += *p=='%';
-                       c = getwc(f);
+                       if (*p == '%') {
+                               p++;
+                               while (iswspace((c=getwc(f)))) pos++;
+                       } else {
+                               c = getwc(f);
+                       }
                        if (c!=*p) {
                                ungetwc(c, f);
                                if (c<0) goto input_fail;