From 9de00a019e01c8a37a0b54f39be4bd54028fc57f Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Mon, 7 Apr 2014 02:05:20 -0400 Subject: [PATCH] fix failure of printf %g to strip trailing zeros in some cases the code to strip trailing zeros was only looking in the last slot for up to 9 zeros, assuming that the rounding code had already removed fully-zero slots from the end. however, this ignored cases where the rounding code did not run at all, which occur when the value being printed is exactly representable in the requested precision. the simplest solution is to move the code that strips trailing zero slots to run unconditionally, immediately after rounding, rather than as the last step of rounding. (cherry picked from commit 89740868c9f1c84b8ee528468d12df1fa72cd392) --- src/stdio/vfprintf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/stdio/vfprintf.c b/src/stdio/vfprintf.c index bec63ecf..0be75498 100644 --- a/src/stdio/vfprintf.c +++ b/src/stdio/vfprintf.c @@ -363,8 +363,8 @@ static int fmt_fp(FILE *f, long double y, int w, int p, int fl, int t) } } if (z>d+1) z=d+1; - for (; !z[-1] && z>a; z--); } + for (; z>a && !z[-1]; z--); if ((t|32)=='g') { if (!p) p++; -- 2.25.1