aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2025-11-01 17:42:59 -0600
committerPaul Eggert <eggert@cs.ucla.edu>2025-11-01 18:00:32 -0600
commit70bb8fa34f52b961ceaf63f61645e06d9f14446e (patch)
tree91d9e920cfbc3e34a27fc9a94aee2cfab21427d6
parentls: better nstrftime failure check (diff)
downloadcoreutils-70bb8fa34f52b961ceaf63f61645e06d9f14446e.tar.gz
coreutils-70bb8fa34f52b961ceaf63f61645e06d9f14446e.zip
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..
-rw-r--r--src/pr.c13
1 files changed, 8 insertions, 5 deletions
diff --git a/src/pr.c b/src/pr.c
index 440efac0a..10b8c528f 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)));