mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Danila Chernetsov <listdansp@mail.ru>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: Danila Chernetsov <listdansp@mail.ru>,
	linux-kernel@vger.kernel.org, lvc-project@linuxtesting.org
Subject: [PATCH] lib: interval_tree_test: fix division by zero on a zero endpoint
Date: Sat, 28 Nov 2026 00:40:18 +0000	[thread overview]
Message-ID: <20261128004018.135865-1-listdansp@mail.ru> (raw)

The test draws an interval endpoint with

  last = (prandom_u32_state(&rnd) >> 4) % max_endpoint;

so last is in [0, max_endpoint), and then draws the start with

  start = (prandom_u32_state(&rnd) >> 4) % last;

When last is zero this is a division by zero. The result is undefined,
and on x86 it triggers a divide error and causes a kernel oops during
module initialization.

Commit c1fff9794a16 ("lib: interval_tree_test: validate benchmark
parameters") rejects max_endpoint values below 2, so the case where
every endpoint is zero is already excluded. It does not prevent an
individual draw from being zero. With max_endpoint=2 about half of the
draws are zero, and with the default max_endpoint of ~0 it takes a
shifted random value of zero, roughly 1 in 2^28 per draw.

The same two-statement pattern is used in init() to generate the nodes
and in intersection_range_check() and span_iteration_check() to
generate search ranges, so all three are affected.

The only interval whose last endpoint is 0 is the point [0, 0], and the
tree allows start == last. When last is zero, use that point and skip
the modulo. Behavior for last > 0 is unchanged.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes: a8ec14d4f6aa ("lib/interval_tree_test.c: allow users to limit scope of endpoint")
Fixes: 82114e45131f ("lib/interval_tree: add test case for interval_tree_iter_xxx() helpers")
Fixes: ccaf3efceefc ("lib/interval_tree: add test case for span iteration")
Signed-off-by: Danila Chernetsov <listdansp@mail.ru>
---
 lib/interval_tree_test.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/lib/interval_tree_test.c b/lib/interval_tree_test.c
index 06f77fb3179f..a01ba4f16015 100644
--- a/lib/interval_tree_test.c
+++ b/lib/interval_tree_test.c
@@ -48,7 +48,8 @@ static void init(void)
 
 	for (i = 0; i < nnodes; i++) {
 		u32 b = (prandom_u32_state(&rnd) >> 4) % max_endpoint;
-		u32 a = (prandom_u32_state(&rnd) >> 4) % b;
+		/* A zero endpoint is the point [0, 0], not a divisor. */
+		u32 a = b ? (prandom_u32_state(&rnd) >> 4) % b : 0;
 
 		nodes[i].start = a;
 		nodes[i].last = b;
@@ -165,7 +166,8 @@ static int intersection_range_check(void)
 				last = ULONG_MAX;
 			} else {
 				last = (prandom_u32_state(&rnd) >> 4) % max_endpoint;
-				start = (prandom_u32_state(&rnd) >> 4) % last;
+				/* A zero endpoint is the point [0, 0], not a divisor. */
+				start = last ? (prandom_u32_state(&rnd) >> 4) % last : 0;
 			}
 
 			/* Walk nodes to mark intersection nodes */
@@ -272,7 +274,8 @@ static int span_iteration_check(void)
 				last = ULONG_MAX;
 			} else {
 				last = (prandom_u32_state(&rnd) >> 4) % max_endpoint;
-				start = (prandom_u32_state(&rnd) >> 4) % last;
+				/* A zero endpoint is the point [0, 0], not a divisor. */
+				start = last ? (prandom_u32_state(&rnd) >> 4) % last : 0;
 			}
 
 			mas_span.first_index = start;
-- 
2.25.1


                 reply	other threads:[~2026-11-28  0:40 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261128004018.135865-1-listdansp@mail.ru \
    --to=listdansp@mail.ru \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lvc-project@linuxtesting.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®