[PATCH] selftests: timers: Fix integer overflows in 32-bit mode
From: Wake Liu
Date: Wed Jun 03 2026 - 04:29:54 EST
Several timer tests use NSEC_PER_SEC for arithmetic. Since NSEC_PER_SEC
is defined as 1000000000L in vdso/time64.h, it is 32-bit on 32-bit
architectures. Multiplications like NSEC_PER_SEC * 10 or 5 * NSEC_PER_SEC
overflow 32-bit signed long.
Fix this by using LL suffixes or casting NSEC_PER_SEC to long long to
force 64-bit multiplication.
Signed-off-by: Wake Liu <wakel@xxxxxxxxxx>
---
tools/testing/selftests/timers/alarmtimer-suspend.c | 4 ++--
tools/testing/selftests/timers/nanosleep.c | 2 +-
tools/testing/selftests/timers/nsleep-lat.c | 6 +++---
tools/testing/selftests/timers/valid-adjtimex.c | 8 ++++----
4 files changed, 10 insertions(+), 10 deletions(-)
diff --git a/tools/testing/selftests/timers/alarmtimer-suspend.c b/tools/testing/selftests/timers/alarmtimer-suspend.c
index aa66c805f6a4..a000688d5e23 100644
--- a/tools/testing/selftests/timers/alarmtimer-suspend.c
+++ b/tools/testing/selftests/timers/alarmtimer-suspend.c
@@ -32,7 +32,7 @@
#include <errno.h>
#include "kselftest.h"
-#define UNREASONABLE_LAT (NSEC_PER_SEC * 5) /* hopefully we resume in 5 secs */
+#define UNREASONABLE_LAT (NSEC_PER_SEC * 5LL) /* hopefully we resume in 5 secs */
#define SUSPEND_SECS 15
int alarmcount;
@@ -89,7 +89,7 @@ void sigalarm(int signo)
alarmcount++;
delta_ns = timespec_sub(start_time, ts);
- delta_ns -= NSEC_PER_SEC * SUSPEND_SECS * alarmcount;
+ delta_ns -= (long long)NSEC_PER_SEC * SUSPEND_SECS * alarmcount;
printf("ALARM(%i): %ld:%ld latency: %lld ns ", alarmcount, ts.tv_sec,
ts.tv_nsec, delta_ns);
diff --git a/tools/testing/selftests/timers/nanosleep.c b/tools/testing/selftests/timers/nanosleep.c
index a054680b3372..fcdc649ec7b4 100644
--- a/tools/testing/selftests/timers/nanosleep.c
+++ b/tools/testing/selftests/timers/nanosleep.c
@@ -188,7 +188,7 @@ int main(int argc, char **argv)
fflush(stdout);
length = 10;
- while (length <= (NSEC_PER_SEC * 10)) {
+ while (length <= (NSEC_PER_SEC * 10LL)) {
ret = nanosleep_test(clockid, length);
if (ret == UNSUPPORTED) {
ksft_test_result_skip("%-31s\n", clockstring(clockid));
diff --git a/tools/testing/selftests/timers/nsleep-lat.c b/tools/testing/selftests/timers/nsleep-lat.c
index a7ba1eb1e21b..c49133afe270 100644
--- a/tools/testing/selftests/timers/nsleep-lat.c
+++ b/tools/testing/selftests/timers/nsleep-lat.c
@@ -76,9 +76,9 @@ struct timespec timespec_add(struct timespec ts, unsigned long long ns)
long long timespec_sub(struct timespec a, struct timespec b)
{
- long long ret = NSEC_PER_SEC * b.tv_sec + b.tv_nsec;
+ long long ret = (long long)NSEC_PER_SEC * b.tv_sec + b.tv_nsec;
- ret -= NSEC_PER_SEC * a.tv_sec + a.tv_nsec;
+ ret -= (long long)NSEC_PER_SEC * a.tv_sec + a.tv_nsec;
return ret;
}
@@ -146,7 +146,7 @@ int main(int argc, char **argv)
continue;
length = 10;
- while (length <= (NSEC_PER_SEC * 10)) {
+ while (length <= (NSEC_PER_SEC * 10LL)) {
ret = nanosleep_lat_test(clockid, length);
if (ret)
break;
diff --git a/tools/testing/selftests/timers/valid-adjtimex.c b/tools/testing/selftests/timers/valid-adjtimex.c
index e1e56d3097d6..8f10d64f0c96 100644
--- a/tools/testing/selftests/timers/valid-adjtimex.c
+++ b/tools/testing/selftests/timers/valid-adjtimex.c
@@ -260,16 +260,16 @@ int validate_set_offset(void)
if (set_offset(-NSEC_PER_SEC - 1, 1))
return -1;
- if (set_offset(5 * NSEC_PER_SEC, 1))
+ if (set_offset(5LL * NSEC_PER_SEC, 1))
return -1;
- if (set_offset(-5 * NSEC_PER_SEC, 1))
+ if (set_offset(-5LL * NSEC_PER_SEC, 1))
return -1;
- if (set_offset(5 * NSEC_PER_SEC + NSEC_PER_SEC / 2, 1))
+ if (set_offset(5LL * NSEC_PER_SEC + NSEC_PER_SEC / 2, 1))
return -1;
- if (set_offset(-5 * NSEC_PER_SEC - NSEC_PER_SEC / 2, 1))
+ if (set_offset(-5LL * NSEC_PER_SEC - NSEC_PER_SEC / 2, 1))
return -1;
if (set_offset(USEC_PER_SEC - 1, 0))
--
2.54.0.1013.g208068f2d8-goog