fix float parsing logic for long decimal expansions
[musl] / src / internal / floatscan.c
index aa962f4..bba5753 100644 (file)
@@ -89,7 +89,7 @@ static long double decfloat(FILE *f, int c, int bits, int emin, int sign, int po
                        if (gotrad) break;
                        gotrad = 1;
                        lrp = dc;
-               } else if (k < KMAX-2) {
+               } else if (k < KMAX-3) {
                        dc++;
                        if (c!='0') lnz = dc;
                        if (j) x[k] = x[k]*10 + c-'0';
@@ -101,7 +101,7 @@ static long double decfloat(FILE *f, int c, int bits, int emin, int sign, int po
                        gotdig=1;
                } else {
                        dc++;
-                       if (c!='0') x[KMAX-3] |= 1;
+                       if (c!='0') x[KMAX-4] |= 1;
                }
        }
        if (!gotrad) lrp=dc;
@@ -143,7 +143,7 @@ static long double decfloat(FILE *f, int c, int bits, int emin, int sign, int po
        }
 
        /* Align incomplete final B1B digit */
-       if (k<KMAX && j) {
+       if (j) {
                for (; j<9; j++) x[k]*=10;
                k++;
                j=0;
@@ -199,11 +199,11 @@ static long double decfloat(FILE *f, int c, int bits, int emin, int sign, int po
                }
                if (carry) {
                        rp += 9;
+                       a = (a-1 & MASK);
                        if (a == z) {
                                z = (z-1 & MASK);
                                x[z-1 & MASK] |= x[z];
                        }
-                       a = (a-1 & MASK);
                        x[a] = carry;
                }
        }
@@ -244,7 +244,7 @@ static long double decfloat(FILE *f, int c, int bits, int emin, int sign, int po
 
        /* Assemble desired bits into floating point variable */
        for (y=i=0; i<LD_B1B_DIG; i++) {
-               if ((a+i & MASK)==z) x[z=(z+1 & MASK)] = 0;
+               if ((a+i & MASK)==z) x[(z=(z+1 & MASK))-1] = 0;
                y = 1000000000.0L * y + x[a+i & MASK];
        }
 
@@ -352,7 +352,7 @@ static long double hexfloat(FILE *f, int bits, int emin, int sign, int pok)
                } else {
                        shlim(f, 0);
                }
-               return 0;
+               return sign * 0.0;
        }
        if (!gotrad) rp = dc;
        while (dc<8) x *= 16, dc++;