All of lore.kernel.org
 help / color / mirror / Atom feed
From: Joel Cruz Cabrera <tf.joel.work@gmail.com>
To: akpm@linux-foundation.org
Cc: tpiepho@gmail.com, linux-kernel@vger.kernel.org
Subject: [PATCH 1/2] math: rational: avoid overflow when comparing the semi-convergent term
Date: Tue,  1 Dec 2026 12:46:33 +0000	[thread overview]
Message-ID: <20261201124634.48970-1-tf.joel.work@gmail.com> (raw)

rational_best_approximation() chooses between the previous convergent
and the best semi-convergent with "2u * t > a".  When t is
2^(BITS_PER_LONG - 1) or more, the product wraps, the test fails, and
the previous convergent is returned even though the semi-convergent is
much closer.  The result still respects max_numerator and
max_denominator; it is just not the best approximation.

t is bounded by max_numerator and max_denominator, so this needs a
bound of at least 2^(BITS_PER_LONG - 1).  For example, on a 32-bit
kernel

  rational_best_approximation(1, U32_MAX, U32_MAX, U32_MAX - 1, &n, &d)

returns 0/1 instead of 1/(U32_MAX - 1), and 2147484020/2147484019 with
max_denominator = 2147484017 gives 1/1 instead of
2147484018/2147484017.  64-bit kernels behave the same with bounds of
2^63 or more.  No in-tree caller is known to pass bounds that large.

Compare t with a / 2 instead.  This is equivalent and cannot overflow:
2 * t > a if and only if t > a / 2, and 2 * t == a if and only if a is
even and t == a / 2.

The other products in the function cannot overflow.  The Euclidean
algorithm gives d1 * d + d2 * dp == given_denominator and
n1 * d + n2 * dp == given_numerator, and d0 <= d2, so d0 * dp, d1 * d,
d2 and n2 never exceed the inputs.

The problem was found with the help of an AI assistant (Claude, by
Anthropic) while checking the lib/math helpers.  The assistant also
wrote the user-space test harness, proposed this change and drafted
this changelog; I reviewed all of it.  Testing: the unmodified function,
compiled in user space with 64-bit and with 32-bit unsigned long, was
compared with a brute-force and an exact 128-bit reference: exhaustively
for all inputs and bounds up to 120, on 4 * 10^7 random large inputs and
on targeted cases.  It is wrong only in the case described above; with
this change there are no mismatches.  The KUnit case added in the next
patch fails without this change and passes with it (kunit.py, arm64
and 32-bit arm under QEMU).

Fixes: 323dd2c3ed06 ("lib/math/rational.c: fix possible incorrect result from rational fractions helper")




Signed-off-by: Joel Cruz Cabrera <tf.joel.work@gmail.com>
---
 lib/math/rational.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/lib/math/rational.c b/lib/math/rational.c
index d2c34e629..bf22f7f4c 100644
--- a/lib/math/rational.c
+++ b/lib/math/rational.c
@@ -90,8 +90,11 @@ void rational_best_approximation(
 			/* This tests if the semi-convergent is closer than the previous
 			 * convergent.  If d1 is zero there is no previous convergent as this
 			 * is the 1st iteration, so always choose the semi-convergent.
+			 * Compare t with a / 2 rather than 2 * t with a: t can be as large
+			 * as the bounds, and 2 * t would overflow.
 			 */
-			if (!d1 || 2u * t > a || (2u * t == a && d0 * dp > d1 * d)) {
+			if (!d1 || t > a / 2 ||
+			    (t == a / 2 && !(a & 1) && d0 * dp > d1 * d)) {
 				n1 = n0 + t * n1;
 				d1 = d0 + t * d1;
 			}

base-commit: 19cc08c1a6a650f8e54c0461ff198be6448f425d
-- 
2.50.1 (Apple Git-155)


             reply	other threads:[~2026-12-01 12:46 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 12:46 Joel Cruz Cabrera [this message]
2026-12-01 12:46 ` [PATCH 2/2] math: rational: add a KUnit case for a semi-convergent term above LONG_MAX Joel Cruz Cabrera
2026-12-01 18:54   ` Trent Piepho

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=20261201124634.48970-1-tf.joel.work@gmail.com \
    --to=tf.joel.work@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=tpiepho@gmail.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.