scanf conflict with ANSI/ISO C99 fscanf Example 3 -- glibc scanf accepting invalid integer and floating point strings
Andreas Jaeger
aj@suse.de
Sat Sep 13 13:47:00 GMT 2003
mjn3@codepoet.org (Manuel Novoa III) writes:
> Hello,
>
> Subject line and comments in attached code pretty much say it all.
No, it doesn't ;-)
> Manuel
>
>
> /* Illustration of glibc scanf accepting invalid floating point strings.
> Annotated output for glibc -- cvs Sep 10 14:37 CDT
>
> glibc's scanf behavior conflits with that specified in Example 3 for
> fscanf in the ANSI/ISO C99 standard.
>
> Based on additional tests run, it appears that scanf is failing to
> check for the existence of digits in some subparts of floating point
> and integer strings being scanned.
>
> ************* OUTPUT *********************************************************
>
> count = 3
> Error! count should be 0, as per Example 3 for fscanf in the ANSI/ISO C99 spec.
The example does not mention the input format!
> quant = 100.000000
> units = "rgs"
> item = "energy"
>
> Running some more tests which should all return count = 0.
> testing "1.Ez"... count = 2 float = 1.000000 trailing string = "z"
> testing "0x.pz"... count = 2 float = 0.000000 trailing string = "z"
> testing "0x.pz"... count = 2 int = 0 trailing string = ".pz"
You really need the space here.
>
> */
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main(void)
> {
> int count;
> float quant;
> char units[21], item[21];
>
> count = sscanf("100ergs of energy", "%f%20s of %20s", &quant, units, item);
100 ergs of energy...
100e is the initial suffix for a floating point number which cannot be
parsed correctly, so your input is wrong,
Andreas
--
Andreas Jaeger, aj@suse.de, http://www.suse.de/~aj
SuSE Linux AG, Deutschherrnstr. 15-19, 90429 Nürnberg, Germany
GPG fingerprint = 93A3 365E CE47 B889 DF7F FED1 389A 563C C272 A126
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: not available
URL: <http://sourceware.org/pipermail/libc-alpha/attachments/20030913/c0fdd541/attachment.sig>
More information about the Libc-alpha
mailing list