[PATCH 3/3] tools/accounting: simplify 32-bit time_t overflow check in format_timespec()

From: wang.yaxin

Date: Sat Jul 11 2026 - 05:34:59 EST


From: Wang Yaxin <wang.yaxin@xxxxxxxxxx>

Replace the Y2038 overflow guard in format_timespec() with a direct
narrowing truncation check ((long long)time_sec != ts->tv_sec), which
is both simpler and more robust across platforms. Also move the
time_sec assignment earlier to avoid duplication.

While at it, fix a minor alignment issue in delaytop.c by adding a
leading space to the output format string.

Signed-off-by: Wang Yaxin <wang.yaxin@xxxxxxxxxx>
---
tools/accounting/delaytop.c | 2 +-
tools/accounting/format_timespec.c | 12 +++++-------
2 files changed, 6 insertions(+), 8 deletions(-)

diff --git a/tools/accounting/delaytop.c b/tools/accounting/delaytop.c
index 1144ca325447..097b57887147 100644
--- a/tools/accounting/delaytop.c
+++ b/tools/accounting/delaytop.c
@@ -1099,7 +1099,7 @@ static void display_results(int psi_ret)
get_field_delay_values(&tasks[i], cfg.type_field, &avg_ms,
&max_ms, &max_ts);

- suc &= BOOL_FPRINT(out, "%12.2f %12.2f %20s\n",
+ suc &= BOOL_FPRINT(out, " %12.2f %12.2f %20s\n",
avg_ms, max_ms, format_timespec(&max_ts));
} else if (cfg.display_mode == MODE_MEMVERBOSE) {
suc &= BOOL_FPRINT(out, DELAY_FMT_MEMVERBOSE,
diff --git a/tools/accounting/format_timespec.c b/tools/accounting/format_timespec.c
index 1dba50cac895..d7bfd307c00b 100644
--- a/tools/accounting/format_timespec.c
+++ b/tools/accounting/format_timespec.c
@@ -14,22 +14,20 @@ const char *format_timespec(const struct __kernel_timespec *ts)
{
static char buffer[32];
struct tm tm_info;
- time_t time_sec;
+ time_t time_sec = ts->tv_sec;

if (ts->tv_sec == 0 && ts->tv_nsec == 0)
return "N/A";

/*
* On 32-bit platforms time_t is 32-bit and cannot represent
- * dates beyond Y2038. The kernel timestamp is always 64-bit,
- * so reject values that would overflow.
+ * timestamps outside [INT32_MIN, INT32_MAX]. A 64-bit kernel
+ * timestamp that does not survive the narrowing truncation is
+ * rejected to avoid silent data corruption.
*/
- if (sizeof(time_t) < sizeof(ts->tv_sec) &&
- ts->tv_sec > (__u64)((1ULL << (sizeof(time_t) * 8 - 1)) - 1))
+ if ((long long)time_sec != ts->tv_sec)
return "N/A";

- time_sec = ts->tv_sec;
-
if (!localtime_r(&time_sec, &tm_info))
return "N/A";

--
2.27.0