]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
pr: improve nstrftime failure check
authorPaul Eggert <eggert@cs.ucla.edu>
Sat, 1 Nov 2025 23:42:59 +0000 (17:42 -0600)
committerPaul Eggert <eggert@cs.ucla.edu>
Sun, 2 Nov 2025 00:00:32 +0000 (18:00 -0600)
* 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..

src/pr.c

index 440efac0af3bac0ad62548b1e732ee20136e2683..10b8c528f217dff77b5355dce018f6e9b46ad76e 100644 (file)
--- 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)));