mailing list of musl libc
 help / color / mirror / code / Atom feed
* patch for sscanf issues
@ 2011-04-24 20:41 gs
  0 siblings, 0 replies; only message in thread
From: gs @ 2011-04-24 20:41 UTC (permalink / raw)
  To: musl

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: multipart/mixed;, Size: 2587 bytes --]

This is a multi-part message in MIME format.
--------------070409060600030405000807
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 8bit

the current sscanf implementation has 2 issues:
- it doesnt return EOF with fscanf, which causes the /gcc/genhooks.c to 
enter an endless loop when compiling gcc 4.6.0
- it doesnt return the correct value in %n pointer, when something like 
"%*d%n" is used

both bugs are fixed in the following patch. this means a) gcc 4.6.0 
compilation succeeds now, and b) musl now passes the rigorous sscanf 
testsuite of gnu gmp, which is executed when "make check" is called.


here are some testcases
bug 1: EOF (testcase.txt is available here: http://int3.at/testcase.txt )
this one causes GCC compilation to fail

         FILE* f = fopen("testcase.txt", "r");

         while (fscanf (f, "%*[^@]"), buf[0] = '\0',
                  fscanf (f, "@%5[^ \n]", buf) != EOF) {
                 if(*buf) {
                         i++;
                         printf("%d: %s\n",i,buf);
                 }
         }
         fclose(f);

bug 2: %n after %*d
this one causes GMP testsuite to fail

     int i;
         assert(sscanf ("123", "%*d%d", &i) == -1);
         assert(sscanf ("123", "%*d%n", &i) == 0);
         printf("value passed in pointer: %d\n", i); // it seems glibc 
returns 3 in i, while musl returns 2
         ret = sscanf("123" + i, "%d%n", &i);
         printf("%d\n", ret);
         assert(ret == -1);

regards, günter schäffler.






--------------070409060600030405000807
Content-Type: text/plain;
 name="musl-patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
 filename="musl-patch"

diff --git a/src/stdio/__scanf.c b/src/stdio/__scanf.c
index 185615d..6e4bd4c 100644
--- a/src/stdio/__scanf.c
+++ b/src/stdio/__scanf.c
@@ -99,8 +99,10 @@ int __scanf(rctx_t *r, const wchar_t *fmt, va_list ap)
 		} else if (*p != '%' || p[1] == '%') {
 			if (*p == '%') p++;
 			r->w = 1;
-			if (*p++ != read(r))
+			if (*p++ != read(r)) {
+				if(r->c == -1) matches = -1;
 				goto match_fail;
+			}
 			continue;
 		}
 		p++;
@@ -126,7 +128,7 @@ int __scanf(rctx_t *r, const wchar_t *fmt, va_list ap)
 		} else if (mode == 1) {
 			dest = va_arg(ap, void *);
 		}
-		
+
 		if (!*p) goto fmt_fail;
 
 		width = 0;
@@ -474,6 +476,7 @@ writefloat:
 		/* unread will do nothing if field width was exhausted */
 		unread(r);
 		if (dest) matches++;
+		else(r->u = 0); //prevent bad size returned with follow-up %n
 	}
 	return matches;
 enc_fail:

--------------070409060600030405000807--


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2011-04-24 20:41 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-04-24 20:41 patch for sscanf issues gs

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).