From: Paul Eggert Date: Sat, 1 Nov 2025 23:42:59 +0000 (-0600) Subject: pr: improve nstrftime failure check X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=70bb8fa34f52b961ceaf63f61645e06d9f14446e;p=thirdparty%2Fcoreutils.git pr: improve nstrftime failure check * src/pr.c (init_header): Do not report an nstrftime EOVERFLOW error as memory exhaustion. Instead, output the time as an integer. Also, work even if nstrftime (nullptr, SIZE_MAX, ...) would return PTRDIFF_MAX which means adding 1 would overflow.. --- diff --git a/src/pr.c b/src/pr.c index 440efac0af..10b8c528f2 100644 --- a/src/pr.c +++ b/src/pr.c @@ -1669,12 +1669,15 @@ init_header (char const *filename, int desc) ns = t.tv_nsec; if (localtime_rz (localtz, &t.tv_sec, &tm)) { - size_t bufsize - = nstrftime (nullptr, SIZE_MAX, date_format, &tm, localtz, ns) + 1; - buf = xmalloc (bufsize); - nstrftime (buf, bufsize, date_format, &tm, localtz, ns); + ptrdiff_t len = nstrftime (nullptr, MIN (PTRDIFF_MAX, SIZE_MAX), + date_format, &tm, localtz, ns); + if (0 <= len) + { + buf = ximalloc (len + 1); + nstrftime (buf, len + 1, date_format, &tm, localtz, ns); + } } - else + if (!buf) { char secbuf[INT_BUFSIZE_BOUND (intmax_t)]; buf = xmalloc (sizeof secbuf + MAX (10, INT_BUFSIZE_BOUND (int)));