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]

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


Hello,

Subject line and comments in attached code pretty much say it all.

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.
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"

*/

#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);

    printf("count = %d\n", count);
    if (count == 0) {
        return EXIT_SUCCESS;
    }

    printf("Error! count should be 0, as per Example 3 for fscanf in the ANSI/ISO C99 spec.\n");

    if (count >= 1) {
        printf("quant = %f\n", quant);
        if (count >= 2) {
            printf("units = \"%s\"\n", units);
            if (count == 3) {
                printf("item = \"%s\"\n", item);
            }
        }

        printf("\nRunning some more tests which should all return count = 0.\n");
        
        {
            static const char f1[] = "1.Ez";

            printf("testing \"%s\"...", f1);
            count = sscanf(f1, "%f%20s", &quant, units);
            printf("  count = %d", count);
            if (count >= 1) {
                printf("  float = %f", quant);
                if (count >= 2) {
                    printf("  trailing string = \"%s\"", units);
                }
            }
            printf("\n");
        }

        {
            static const char f2[] = "0x.pz";

            printf("testing \"%s\"...", f2);
            count = sscanf(f2, "%f%20s", &quant, units);
            printf("  count = %d", count);
            if (count >= 1) {
                printf("  float = %f", quant);
                if (count >= 2) {
                    printf("  trailing string = \"%s\"", units);
                }
            }
            printf("\n");
        }

        {
            static const char f3[] = "0x.pz";
			int n;

            printf("testing \"%s\"...", f3);
            count = sscanf(f3, "%i%20s", &n, units);
            printf("  count = %d", count);
            if (count >= 1) {
                printf("  int = %d", n);
                if (count >= 2) {
                    printf("  trailing string = \"%s\"", units);
                }
            }
            printf("\n");
        }
    }

    return EXIT_FAILURE;
}

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