mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 1/2] math: rational: avoid overflow when comparing the semi-convergent term
@ 2026-12-01 12:46 Joel Cruz Cabrera
  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
  0 siblings, 1 reply; 2+ messages in thread
From: Joel Cruz Cabrera @ 2026-12-01 12:46 UTC (permalink / raw)
  To: akpm; +Cc: tpiepho, linux-kernel

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)


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-01 12:46 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-01 12:46 [PATCH 1/2] math: rational: avoid overflow when comparing the semi-convergent term Joel Cruz Cabrera
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

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®