[PATCH 3/3] tools/accounting: simplify 32-bit time_t overflow check in format_timespec()
From: wang.yaxin
Date: Sat Jul 18 2026 - 01:38:31 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 | 6 +++++-
tools/accounting/format_timespec.c | 12 +++++-------
tools/accounting/getdelays.c | 4 ++++
3 files changed, 14 insertions(+), 8 deletions(-)
diff --git a/tools/accounting/delaytop.c b/tools/accounting/delaytop.c
index 1144ca325447..d32aedbfa07d 100644
--- a/tools/accounting/delaytop.c
+++ b/tools/accounting/delaytop.c
@@ -728,10 +728,14 @@ static void fetch_and_fill_task_info(int pid, const char *comm)
nl_len = GENLMSG_PAYLOAD(&resp.n);
na = (struct nlattr *) GENLMSG_DATA(&resp);
while (nl_len > 0) {
+ if (na->nla_len < NLA_HDRLEN)
+ break;
if (na->nla_type == TASKSTATS_TYPE_AGGR_PID) {
nested = (struct nlattr *) NLA_DATA(na);
nested_len = NLA_PAYLOAD(na->nla_len);
while (nested_len > 0) {
+ if (nested->nla_len < NLA_HDRLEN)
+ break;
if (nested->nla_type == TASKSTATS_TYPE_STATS) {
size_t payload_len = NLA_PAYLOAD(nested->nla_len);
@@ -1099,7 +1103,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";
diff --git a/tools/accounting/getdelays.c b/tools/accounting/getdelays.c
index d3193f670d89..f47b9d008352 100644
--- a/tools/accounting/getdelays.c
+++ b/tools/accounting/getdelays.c
@@ -659,6 +659,8 @@ int main(int argc, char *argv[])
na = (struct nlattr *) GENLMSG_DATA(&msg);
len = 0;
while (len < rep_len) {
+ if (na->nla_len < NLA_HDRLEN)
+ break;
len += NLA_ALIGN(na->nla_len);
switch (na->nla_type) {
case TASKSTATS_TYPE_AGGR_TGID:
@@ -669,6 +671,8 @@ int main(int argc, char *argv[])
/* For nested attributes, na follows */
na = (struct nlattr *) NLA_DATA(na);
while (len2 < aggr_len) {
+ if (na->nla_len < NLA_HDRLEN)
+ break;
switch (na->nla_type) {
case TASKSTATS_TYPE_PID:
rtid = *(int *) NLA_DATA(na);
--
2.27.0