fix incorrect initial count in shgetc when data is already buffered
[musl] / src / internal / floatscan.c
index 7d9a452..3aa5408 100644 (file)
@@ -3,6 +3,7 @@
 #include <math.h>
 #include <float.h>
 #include <limits.h>
+#include <errno.h>
 
 #include "shgetc.h"
 #include "floatscan.h"
@@ -43,7 +44,7 @@ static long long scanexp(FILE *f, int pok)
        }
        for (x=0; c-'0'<10U && x<INT_MAX/10; c = shgetc(f))
                x = 10*x + c-'0';
-       for (y=x; c-'0'<10U && x<LLONG_MAX/10; c = shgetc(f))
+       for (y=x; c-'0'<10U && x<LLONG_MAX/100; c = shgetc(f))
                y = 10*y + c-'0';
        for (; c-'0'<10U; c = shgetc(f));
        shunget(f);
@@ -51,7 +52,7 @@ static long long scanexp(FILE *f, int pok)
 }
 
 
-static long double decfloat(FILE *f, int bits, int emin, int sign, int pok)
+static long double decfloat(FILE *f, int c, int bits, int emin, int sign, int pok)
 {
        uint32_t x[KMAX];
        static const uint32_t th[] = { LD_B1B_MAX };
@@ -64,13 +65,10 @@ static long double decfloat(FILE *f, int bits, int emin, int sign, int pok)
        long double y;
        long double frac=0;
        long double bias=0;
-       int c;
 
        j=0;
        k=0;
 
-       c = shgetc(f);
-
        /* Don't let leading zeros consume buffer space */
        for (; c=='0'; c = shgetc(f)) gotdig=1;
 
@@ -111,6 +109,7 @@ static long double decfloat(FILE *f, int bits, int emin, int sign, int pok)
                shunget(f);
        }
        if (!gotdig) {
+               errno = EINVAL;
                shlim(f, 0);
                return 0;
        }
@@ -119,10 +118,14 @@ static long double decfloat(FILE *f, int bits, int emin, int sign, int pok)
                return sign * 0.0;
        if (lrp==dc && (!k || (k==1 && !j)) && (bits>30 || x[0]>>bits==0))
                return sign * (long double)x[0];
-       if (lrp > -emin/2)
+       if (lrp > -emin/2) {
+               errno = ERANGE;
                return sign * LDBL_MAX * LDBL_MAX;
-       if (lrp < emin-2*LDBL_MANT_DIG)
+       }
+       if (lrp < emin-2*LDBL_MANT_DIG) {
+               errno = ERANGE;
                return sign * LDBL_MIN * LDBL_MIN;
+       }
 
        if (k<KMAX && j) {
                for (; j<9; j++) x[k]*=10;
@@ -219,8 +222,10 @@ static long double decfloat(FILE *f, int bits, int emin, int sign, int pok)
                }
        }
 
-       for (y=i=0; i<LD_B1B_DIG && (a+i & MASK)!=z; i++)
+       for (y=i=0; i<LD_B1B_DIG; i++) {
+               if ((a+i & MASK)==z) x[z=(z+1 & MASK)] = 0;
                y = 1000000000.0L * y + x[a+i & MASK];
+       }
 
        y *= sign;
 
@@ -257,6 +262,8 @@ static long double decfloat(FILE *f, int bits, int emin, int sign, int pok)
 
        y = scalbnl(y, e2);
 
+       if (!y) errno = ERANGE;
+
        return y;
 }
 
@@ -328,12 +335,20 @@ static long double hexfloat(FILE *f, int bits, int emin, int sign, int pok)
                        }
                        e2 = 0;
                }
+       } else {
+               shunget(f);
        }
        e2 += 4*rp - 32;
 
        if (!x) return sign * 0.0;
-       if (e2 > -emin) return sign * LDBL_MAX * LDBL_MAX;
-       if (e2 < emin-2*LDBL_MANT_DIG) return sign * LDBL_MIN * LDBL_MIN;
+       if (e2 > -emin) {
+               errno = ERANGE;
+               return sign * LDBL_MAX * LDBL_MAX;
+       }
+       if (e2 < emin-2*LDBL_MANT_DIG) {
+               errno = ERANGE;
+               return sign * LDBL_MIN * LDBL_MIN;
+       }
 
        while (x < 0x80000000) {
                if (y>=0.5) {
@@ -359,6 +374,8 @@ static long double hexfloat(FILE *f, int bits, int emin, int sign, int pok)
        y = bias + sign*(long double)x + sign*y;
        y -= bias;
 
+       if (!y) errno = ERANGE;
+
        return scalbnl(y, e2);
 }
 
@@ -409,6 +426,7 @@ long double __floatscan(FILE *f, int c, int prec, int pok)
 
        if (i) {
                shunget(f);
+               errno = EINVAL;
                shlim(f, 0);
                return 0;
        }
@@ -417,9 +435,9 @@ long double __floatscan(FILE *f, int c, int prec, int pok)
                c = shgetc(f);
                if ((c|32) == 'x')
                        return hexfloat(f, bits, emin, sign, pok);
+               shunget(f);
                c = '0';
        }
 
-       shunget(f);
-       return decfloat(f, bits, emin, sign, pok);
+       return decfloat(f, c, bits, emin, sign, pok);
 }