This is the mail archive of the libc-alpha@sources.redhat.com mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: scanf conflict with ANSI/ISO C99 fscanf Example 3 -- glibcscanf accepting invalid integer and floating point strings


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

Attachment: pgp00000.pgp
Description: PGP signature


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]