On 2017-03-30 10:52:33 +0100, Claude Heiland-Allen wrote:
> The failing source code is:
> 
>   /* EOF for no matching */
>   {
>     char buf[128];
>     ret = gmp_sscanf ("   ", "%s", buf);
>     ASSERT_ALWAYS (ret == EOF);
>     ret = fromstring_gmp_fscanf ("   ", "%s", buf);
>     ASSERT_ALWAYS (ret == EOF);
>     if (option_libc_scanf)
>       {
>         ret = sscanf ("   ", "%s", buf);
>         ASSERT_ALWAYS (ret == EOF);
>         ret = fun_fscanf ("   ", "%s", buf, NULL);
>         ASSERT_ALWAYS (ret == EOF);
>       }
>   }
> 
> Commenting out the asserts and 'if' and inserting debugging printf()
> statements gives:
> 
>     gmp_sscanf() ret = 0
>     fromstring_gmp_fscanf() ret = 0
>     sscanf() ret = 0
      ^^^^^^^^^^^^^^^^
>     fun_fscanf() ret = 0

The initial issue is the 0 returned by sscanf(). Then GMP is consistent
with the C implementation.

Though the C standard may be ambiguous, 0 is not possible as a return
value. Thus this is a bug in the C library (or compiler).

If %s can match an empty sequence of non-white-space characters, then
the return value should be 1 (contrary to %[, there is no mention of
"nonempty" for %s in the text of the standard). Otherwise, this is an
input failure because the end of the input string is reached before
the %s matching:

  4  The fscanf function executes each directive of the format in turn.
     When all directives have been executed, or if a directive fails
     (as detailed below), the function returns. Failures are described
     as input failures (due to the occurrence of an encoding error or
     the unavailability of input characters), or matching failures (due
     to inappropriate input).

And in case of input failure before the first conversion has completed
(which is the case here), EOF is returned:

 16  The fscanf function returns the value of the macro EOF if an
     input failure occurs before the first conversion (if any) has
     completed. Otherwise, the function returns the number of input
     items assigned, which can be fewer than provided for, or even
     zero, in the event of an early matching failure.

-- 
Vincent Lefèvre <vinc...@vinc17.net> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)
_______________________________________________
gmp-bugs mailing list
gmp-bugs@gmplib.org
https://gmplib.org/mailman/listinfo/gmp-bugs

Reply via email to