diff options
| author | Paul Eggert <eggert@cs.ucla.edu> | 2025-11-01 17:42:59 -0600 |
|---|---|---|
| committer | Paul Eggert <eggert@cs.ucla.edu> | 2025-11-01 18:00:32 -0600 |
| commit | 70bb8fa34f52b961ceaf63f61645e06d9f14446e (patch) | |
| tree | 91d9e920cfbc3e34a27fc9a94aee2cfab21427d6 | |
| parent | ls: better nstrftime failure check (diff) | |
| download | coreutils-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.c | 13 |
1 files changed, 8 insertions, 5 deletions
@@ -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))); |
