From 162c198aabcdbe3795d34142c4707649f60fe499 Mon Sep 17 00:00:00 2001 From: dana Date: Sat, 29 Dec 2018 05:22:34 -0600 Subject: 43953: Fix rounding/truncation error in %. time-format specifier Also fixes an issue where %. couldn't be used more than once in a format string without strange results Tweaked very slightly per workers/43954 --- Src/utils.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'Src/utils.c') diff --git a/Src/utils.c b/Src/utils.c index e43a3cdb4..0969cef37 100644 --- a/Src/utils.c +++ b/Src/utils.c @@ -3334,19 +3334,28 @@ morefmt: #endif switch (*fmt++) { case '.': - if (ztrftimebuf(&bufsize, digs)) - return -1; + { + long fnsec = nsec; if (digs > 9) digs = 9; + if (ztrftimebuf(&bufsize, digs)) + return -1; if (digs < 9) { int trunc; - for (trunc = 8 - digs; trunc; trunc--) - nsec /= 10; - nsec = (nsec + 8) / 10; + long max = 100000000; + for (trunc = 8 - digs; trunc; trunc--) { + max /= 10; + fnsec /= 10; + } + max -= 1; + fnsec = (fnsec + 5) / 10; + if (fnsec > max) + fnsec = max; } - sprintf(buf, "%0*ld", digs, nsec); + sprintf(buf, "%0*ld", digs, fnsec); buf += digs; break; + } case '\0': /* Guard against premature end of string */ *buf++ = '%'; -- cgit 1.4.1