shithub: riscv

Download patch

ref: bfc8cdfff5aba1da7ddef1c2ef7e1167f8dd2e8d
parent: 758edf2b1448bcc461bc2f99eeaabad1a077351d
author: Ori Bernstein <ori@eigenstate.org>
date: Sat May 9 11:10:39 EDT 2020

fix '%[]' specifiers and '%n' (thanks phil9)

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.

--- a/sys/src/ape/lib/ap/stdio/vfscanf.c
+++ b/sys/src/ape/lib/ap/stdio/vfscanf.c
@@ -72,7 +72,6 @@
 			do
 				c=ngetc(f);
 			while(isspace(c));
-			if(c==EOF) return ncvt?ncvt:EOF;
 			nungetc(c, f);
 			break;
 		}
@@ -396,13 +395,14 @@
 			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;
--- a/sys/src/libstdio/vfscanf.c
+++ b/sys/src/libstdio/vfscanf.c
@@ -68,7 +68,6 @@
 			do
 				c=ngetc(f);
 			while(isspace(c));
-			if(c==EOF) return ncvt?ncvt:EOF;
 			nungetc(c, f);
 			break;
 		}
@@ -353,11 +352,13 @@
 			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;