Skip to contentSkip to conversation

fix %*s%n in sscanf

PATCH In review Andy Spencer 2 participants 3 messages Sep 25, 2009

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fix %*s%n in sscanf

When using %*s, sscanf should honor conversion specifiers immediately following the %*s. For example, the following code should find the position of the end of the string "hello".

  int end;
  char buf[] = "hello    world";
  sscanf(buf, "%*s%n", &end);
  printf("%d\n", end);

Ideally, sscanf would advance the fmt and str pointers the same as it would without the *, but the code for that is rather complicated and is not included in the patch.

Please CC me in replies.

lib/vsprintf.c +1 -1
index 73a14b8..da2daf2 100644
@@ -1766,7 +1766,7 @@ int vsscanf(const char * buf, const char * fmt, va_list args)
 		 * advance both strings to next white space
 		 */
 		if (*fmt == '*') {
-			while (!isspace(*fmt) && *fmt)
+			while (!isspace(*fmt) && *fmt != '%' && *fmt)
 				fmt++;
 			while (!isspace(*str) && *str)
 				str++;
Acked-by on v1 1/1
#1 lore raw
Quoted 4 lines · Andy Spencer

When using %*s, sscanf should honor conversion specifiers immediately following the %*s. For example, the following code should find the position of the end of the string "hello".

Quoted 8 lines

 int end;
 char buf[] = "hello    world";
 sscanf(buf, "%*s%n", &end);
 printf("%d\n", end);

Ideally, sscanf would advance the fmt and str pointers the same as it would without the *, but the code for that is rather complicated and is not included in the patch.

Yeah, seems reasonable. So, without your patch, sscanf() in kernel will ignore everything after '*', right?

Quoted 5 lines

Please CC me in replies.

Fortunately, a quick grep shows no one uses '%*' in sscanf() in kernel space:

% grep 'sscanf(.*\".*%\*.*\".*)' -nr ./

For completeness,

Quoted 15 lines
lib/vsprintf.c +1 -1
index 73a14b8..da2daf2 100644
@@ -1766,7 +1766,7 @@ int vsscanf(const char * buf, const char * fmt, va_list args)
                 * advance both strings to next white space
                 */
                if (*fmt == '*') {
-                       while (!isspace(*fmt) && *fmt)
+                       while (!isspace(*fmt) && *fmt != '%' && *fmt)
                                fmt++;
                        while (!isspace(*str) && *str)
                                str++;
#2 lore raw
Quoted 7 lines
Quoted 4 lines
  int end;
  char buf[] = "hello    world";
  sscanf(buf, "%*s%n", &end);
  printf("%d\n", end);

Yeah, seems reasonable. So, without your patch, sscanf() in kernel will ignore everything after '*', right?

Currently it ignores everything between '*' and whitespace. So '%*s%n' does not work, but '%*s %n' will correctly return the start of "world".