[LTP] [PATCH 1/3] tst_timer_test: Fix out-of-bounds array access

Andrea Cervesato andrea.cervesato@suse.de
Tue Oct 6 15:02:37 CEST 2026


From: Andrea Cervesato <andrea.cervesato@suse.com>

Both outlier and early-wake loops in do_timer_test() evaluate
samples[i] before verifying that index i is within bounds.
When all samples are outliers or early, this causes heap buffer
overreads at samples[nsamples] or samples[-1].

Check index bounds before indexing the samples array.

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
 lib/tst_timer_test.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/lib/tst_timer_test.c b/lib/tst_timer_test.c
index b5a088a14..8f9ec12f6 100644
--- a/lib/tst_timer_test.c
+++ b/lib/tst_timer_test.c
@@ -276,7 +276,7 @@ void do_timer_test(long long usec, unsigned int nsamples)
 
 	write_to_file();
 
-	for (i = 0; samples[i] > 10 * usec && i < (int)nsamples; i++) {
+	for (i = 0; i < (int)nsamples && samples[i] > 10 * usec; i++) {
 		if (samples[i] <= 3 * monotonic_resolution)
 			break;
 	}
@@ -286,7 +286,8 @@ void do_timer_test(long long usec, unsigned int nsamples)
 			i, samples[0], samples[i-1]);
 	}
 
-	for (i = nsamples - 1; samples[i] < usec && i > -1; i--);
+	for (i = nsamples - 1; i >= 0 && samples[i] < usec; i--)
+		;
 
 	if (i < (int)nsamples - 1) {
 		tst_res(TFAIL, "%s woken up early %u times range: [%lli,%lli]",

-- 
2.51.0



More information about the ltp mailing list