fix printf rounding with %g for some corner case midpoints
authorRich Felker <dalias@aerifal.cx>
Mon, 7 Apr 2014 17:50:05 +0000 (13:50 -0400)
committerRich Felker <dalias@aerifal.cx>
Wed, 16 Apr 2014 06:46:05 +0000 (02:46 -0400)
the subsequent rounding code assumes the end pointer (z) accurately
reflects the end of significance in the decimal expansion, but for
certain large integers, spurious trailing zero slots were left behind
when applying the binary exponent.

issue reported by Morten Welinder; the analysis of the cause was
performed by nsz, who also proposed this change.

(cherry picked from commit e94d0692864ecf9522fd6a97610a47a2f718d3de)

src/stdio/vfprintf.c

index 0be75498a36ce689ace70e1bd869819acaf10715..f6e7f38d21cd06c03985edb5471b59b748f8eaae 100644 (file)
@@ -308,8 +308,8 @@ static int fmt_fp(FILE *f, long double y, int w, int p, int fl, int t)
                        *d = x % 1000000000;
                        carry = x / 1000000000;
                }
-               if (!z[-1] && z>a) z--;
                if (carry) *--a = carry;
+               while (z>a && !z[-1]) z--;
                e2-=sh;
        }
        while (e2<0) {