From 81c5f5de755911f3a24900886d57a7b0dbd0178b Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Tue, 4 Oct 2022 15:38:14 -0700 Subject: [PATCH] Fix theoretical bug when int == intmax_t MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * src/file.c (file_timestamp_sprintf): Port to platforms where int == intmax_t. I don’t know of any such platforms, so to some extent this patch merely documents the assumption that INT_MAX < INTMAX_MAX. --- src/file.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/file.c b/src/file.c index 62b8dd00..b0b24cc9 100644 --- a/src/file.c +++ b/src/file.c @@ -1019,10 +1019,15 @@ file_timestamp_sprintf (char *p, FILE_TIMESTAMP ts) if (tm) { +#if defined INTMAX_MAX && INTMAX_MAX - 1900 < INT_MAX + /* A theoretical host where (intmax_t) INT_MAX + 1900 overflows. */ + strftime (p, FILE_TIMESTAMP_PRINT_LEN_BOUND + 1, "%Y-%m-%d %H:%M:%S", tm); +#else intmax_t year = tm->tm_year; sprintf (p, "%04" PRIdMAX "-%02d-%02d %02d:%02d:%02d", year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec); +#endif } else if (t < 0) sprintf (p, "%" PRIdMAX, (intmax_t) t); -- 2.37.3