Re: Bug in scanf?
наб <[email protected]> Fri, 19 Dec 2025 14:04:53 +0100
| Newsgroups | gmane.os.netbsd.devel.userlevel |
|---|---|
| Message-ID | <6ry65ipj7ykw5n7zdrwy3fz6vkwlkrukzz4cv2msrgqlwzvm3e@tarta.nabijaczleweli.xyz> |
On Mon, Dec 08, 2025 at 08:45:00PM +0100, Anders Magnusson wrote:
> I just stumbled over something which may be a bug in scanf()...?
> This example is in C99 7.19.6.2 clause 20.
>
> This lines;
>
> int count; float quant; char units[21], item[21];
> count = sscanf("100ergs of energy\n", "%f%20s of %20s", &quant, units,
> item);
>
> Should have a count of 0;
> count = 0; // "100e" fails to match "%f"
The standard C angle interested me, so I put this to PhD
(who happens to be the WG14 editor) off-list.
I relay his analysis from <https://pony.social/@thephd/115743060918796972>:
> This is not a bug.
>
> 100e is not a decimal number. 100 is, 100e1 is, 100e is not.
> The grammar for fscanf with %f follows strtod, and strtod follows
> §6.4.5.3 Floating literals. The grammar is unambiguous that 100e
> does not match because e requires at least 1 digit.
>
> We can argue about how much lookahead is required to make this a
> parsing error (read e and assume an exponent, then fail) vs. failing
> more "gracefully" (read e but do not assume an exponent until
> digit-sequence and possibly a preceiding sign are parsed, and if
> not just make it text) but the stdio text would support doing either
> because fscanf has a "only puts one character back at most" floor
> limit for algorithm implementation (e.g., a more or less forward-only
> algorithm is the minimum requirement). So seeing e and then no
> digits and then failing is valid, and SO is seeing e and then
> realizing there's no more digits and rewinding the e and the the
> extra digit is also a valid implementation.
>
> "That's stupid" it's a library from C89. Of course it's fucking
> stupid.
signature.asc
(application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEfWlHToQCjFzAxEFjvP0LAY0mWPEFAmlFTXIACgkQvP0LAY0m WPH5LBAAjaAm20CV1OksScqKFpeaWzz2EaC2mog3C6GgW0uwiR+2WFOm2ago2n2n c2l7NaETLSJ+KTETfwJvl2mg/KzIYpolZQsxT6Uux1dBOZ55MBhwMuD0dnIz+JHn odxwhYaXNZ+ZmCcxLUgzuAahaJmDLn2e4RPtHFJKNh0MtoMwGcLBYny1SS2wNVqW ZrwdlrtP3JziebKxinjIFtHiV/L+gYg0k767SNMeH3Uqh+5xHQf6khu0WDGJ5VUk nVCw+CW+a6xrP0Bl0TiK7najy5I+M+88V/s++6f0VjbcB7xtkQUQfQG7WuUUS4rz K3T3diNYd+UHb04ZIoL+Kd8kPsBKb05XZIKQZOJOJr+fMh/SjqjzqCVWBblOwQiy NNNVJBmVttmJT2wZrJ0ZB6MUDNGzFUOiGqghT+ggCCynWlFuQPzIGT+8aJIUzF0q /r3Z7dF/A7MjSSZQZAAB36zjmbkkJN7WrPZeHGN6fdDG0yX+wzKUqI8R5MkChcbs gR+dPuJ5cN32ld9H3v9IB3VorOR1bocsnZy/4X/FksYN6nZxhMijbz/YXySVokpI 2U+r5IJ/c1KITKWWqkN1H8t0PfMVoT7efaNWeSHDuowYoelEIhipevcVnNEp82WJ tl2a8PQe92udfWBQk1UptYL+bCWeDKscwd/ukmXOtuEeSmUk8qw= =3aY2 -----END PGP SIGNATURE-----