[wjoye@cfa.harvard.edu: 1.3.6: sscanf bug (error with %n)]

J. Johnston jjohnstn@cygnus.com
Fri Jan 11 14:08:00 GMT 2002

Christopher Faylor wrote:
> I haven't verified if this is actually a problem or not.
> cgf

Strictly speaking with regards to the ANSI spec, it is not a problem.  From
ANSI/ISO 9899-1990,

"A directive composed of white-space character(s) is executed by reading input
up to the first non-white-space character (which remains unread), or until
no more characters can be read."

So, the space ahead of %n forced newlib to read to EOF.  The spec later on says:

"If end-of-file is encountered during input, conversion is terminated. ..."

There are no special caveats for this rule regarding %n so newlib terminated
the conversion.

On the otherhand, the Single Unix spec says:

"If end-of-file is encountered during input, conversion is terminated. If
end-of-file occurs 
before any bytes matching the current conversion specification (except for %n)
have been read (other than leading white-space characters, where permitted),
execution of 
the current conversion specification terminates with an input failure.
unless execution of the current conversion specification is terminated with a
matching failure, 
execution of the following conversion specification (if any) is terminated with
an input failure."

This could probably have been better worded.  IMO, the 2nd statement implies the
requested behavior because %n does not consume any input from the file and thus, 
end-of-file must be reached prior to processing the %n.

The expected output is generated by glibc.  That said, I have just checked in
accompanying patch into the CVS repository that fixes the test case to work
as desired.

-- Jeff J.

2002-01-11 Jeff Johnston  <jjohnstn@redhat.com>

        * libc/stdio/vfscanf.c (__svfscanf_r): Change loop that
        reads blanks from the input file to break if EOF reached
        rather than end processing.

> ----- Forwarded message from William Joye <wjoye@cfa.harvard.edu> -----
> From: William Joye <wjoye@cfa.harvard.edu>
> To: cygwin@cygwin.com
> Subject: 1.3.6: sscanf bug (error with %n)
> Date: Fri, 11 Jan 2002 14:04:22 -0500
> There seems to be a bug with trailing spaces and %n with sscanf strings.
> In particular, %n fails if it is the last item in the format string and
> it is preceeded with a space.
> Here is some sample code:
> #include <stdio.h>
> main()
> {
>   char foo[] = " name 1111.1 ";
>   char bar[8];
>   float ff;
>   int nc;
>   int r;
>   nc = 0;
>   r = sscanf(foo, " %s %f %n", bar, &ff, &nc);
>   printf ("  space got:%d count:%d\n", r, nc);
>   nc = 0;
>   r = sscanf(foo, " %s %f%n", bar, &ff, &nc);
>   printf ("nospace got:%d count:%d\n", r, nc);
> }
> cygwin 1.3.6 outputs:
>   space got:2 count:0
> nospace got:2 count:12
> The correct output should be:
>   space got:2 count:13
> nospace got:2 count:12
> ----- End forwarded message -----
-------------- next part --------------
Index: newlib/libc/stdio/vfscanf.c
RCS file: /cvs/src/src/newlib/libc/stdio/vfscanf.c,v
retrieving revision 1.10
diff -u -r1.10 vfscanf.c
--- newlib/libc/stdio/vfscanf.c	2001/10/01 18:05:11	1.10
+++ newlib/libc/stdio/vfscanf.c	2002/01/11 21:54:28
@@ -292,9 +292,7 @@
 	  for (;;)
-	      if (BufferEmpty)
-		return nassigned;
-	      if (!isspace (*fp->_p))
+	      if (BufferEmpty || !isspace (*fp->_p))
 	      nread++, fp->_r--, fp->_p++;

More information about the Newlib mailing list