[PATCH] stdio: make scanf(%f) eat the same number of characters as strtod() would

Florian Weimer fweimer@redhat.com
Thu Jan 2 10:14:56 GMT 2025


* Miklós Máté:

> diff --git a/stdio-common/vfscanf-internal.c b/stdio-common/vfscanf-internal.c
> index 87f23b5845..c3d4e81476 100644
> --- a/stdio-common/vfscanf-internal.c
> +++ b/stdio-common/vfscanf-internal.c
> @@ -2582,6 +2582,17 @@ digits_extended_fail:
>  		*ARG (float *) = d;
>  	    }
>  
> +          if (__glibc_unlikely (char_buffer_size (&charbuf) > tw - char_buffer_start (&charbuf)))
> +            {
> +              --charbuf.current; /* We have the read chars plus a '\0'.  */
> +              int n = char_buffer_size (&charbuf) - (tw - char_buffer_start (&charbuf));
> +              for (int i = 0; i < n; ++i)
> +                {
> +                  c = *(--charbuf.current);
> +                  ungetc_not_eof (c, s);
> +                }
> +            }
> +

We cannot safely perform multiple ungetc calls like this because they
might require the use of malloc, and we cannot report failure here.

Thanks,
Florian



More information about the Libc-alpha mailing list