]> git.lizzy.rs Git - plan9front.git/commitdiff
fix '%[]' specifiers and '%n' (thanks phil9)
authorOri Bernstein <ori@eigenstate.org>
Sat, 9 May 2020 22:10:39 +0000 (15:10 -0700)
committerOri Bernstein <ori@eigenstate.org>
Sat, 9 May 2020 22:10:39 +0000 (15:10 -0700)
When a match() fails, we need to unget the character we
tried to match against, rather than leaving it consumed.

Also, we can't break out of a conversion before we reach
the end of a format string, because things like the '%n'
conversion do not consume anything, and should still be
handled.

sys/src/ape/lib/ap/stdio/vfscanf.c
sys/src/libstdio/vfscanf.c

index c44bd7576e85af35d76bafd23c6b4bc9c7a7667d..95c14b7b23d16c01610abb1a14da9b056ec37ad6 100644 (file)
@@ -72,7 +72,6 @@ int vfscanf(FILE *f, const char *s, va_list args)
                        do
                                c=ngetc(f);
                        while(isspace(c));
-                       if(c==EOF) return ncvt?ncvt:EOF;
                        nungetc(c, f);
                        break;
                }
@@ -396,13 +395,14 @@ icvt_sq(FILE *f, va_list *args, int store, int width, int)
                        if(nn==0) return 0;
                        else goto Done;
                }
-               if(!match(c, pat))
-                       break;
+               if(!match(c, pat)){
+                       nungetc(c, f);
+                       return 0;
+               }
                if(store)
                        *s++=c;
                nn++;
        }
-       nungetc(c, f);
 Done:
        if(store) *s='\0';
        return 1;
index d17dcf2c4ad9b0b585fd834614425063a3c15e75..d811b78037aa955e9729039e9ab7ab53565ecb1a 100644 (file)
@@ -68,7 +68,6 @@ int vfscanf(FILE *f, const char *s, va_list args){
                        do
                                c=ngetc(f);
                        while(isspace(c));
-                       if(c==EOF) return ncvt?ncvt:EOF;
                        nungetc(c, f);
                        break;
                }
@@ -353,11 +352,13 @@ static int icvt_sq(FILE *f, va_list *args, int store, int width, int type){
                        if(nn==0) return 0;
                        else goto Done;
                }
-               if(!match(c, pat)) break;
+               if(!match(c, pat)){
+                       nungetc(c, f);
+                       return 0;
+               }
                if(store) *s++=c;
                nn++;
        }
-       nungetc(c, f);
 Done:
        if(store) *s='\0';
        return 1;