Fix scanf return value
"Indan Zupancic" <[email protected]> Fri, 5 Nov 2010 17:55:47 +0100 (CET)
| Newsgroups | gmane.linux.lib.dietlibc |
|---|---|
| Message-ID | <[email protected]> |
Hello,
The return value of sscanf() is wrong for non-matching %[..] formats, because
n is always increased even if no characters match. Example:
ret = sscanf(buf, " %15[^#= \t\n] = %255[^\n]\n", cmd, str);
would return 1 for bug="#", while it should be 0. With the below patch this
case works.
Greetings,
Indan
Index: lib/__v_scanf.c
=================================================================== RCS file:
/cvs/dietlibc/lib/__v_scanf.c,v
retrieving revision 1.21
diff -u -p -b -B -w -r1.21 __v_scanf.c
--- lib/__v_scanf.c 7 Jul 2008 12:52:55 -0000 1.21
+++ lib/__v_scanf.c 4 Nov 2010 22:17:32 -0000
@@ -338,6 +338,7 @@ exp_out:
char cset[256];
int flag_not=0;
int flag_dash=0;
+ int i;
memset(cset,0,sizeof(cset));
ch=*format++;
/* first char specials */
@@ -370,15 +371,15 @@ exp_out:
/* like %c or %s */
if (!flag_discard) {
s=(char *)va_arg(arg_ptr,char*);
- ++n;
}
- while (width && (tpch>=0) && (cset[tpch]^flag_not)) {
- if (!flag_discard) *s=tpch;
- if (tpch) ++s; else break;
- --width;
+ for (i=0; i<width && tpch>=0 && (cset[tpch]^flag_not); ++i){ + if
(!flag_discard) s[i]=tpch;
tpch=A_GETC(fn);
}
- if (!flag_discard) *s=0;
+ if (!flag_discard && i) {
+ s[i] = 0;
+ ++n;
+ }
++format;
}
break;