linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] lib/math/rational.c: Fix divide by zero
@ 2021-05-23  0:18 Trent Piepho
  2021-05-24 10:51 ` Andy Shevchenko
  0 siblings, 1 reply; 17+ messages in thread
From: Trent Piepho @ 2021-05-23  0:18 UTC (permalink / raw)
  To: linux-kernel; +Cc: andy, akpm, oskar, Trent Piepho, Yiyuan Guo

If the input is out of the range of the allowed values, either larger
than the largest value or closer to zero than the smallest non-zero
allowed value, then a division by zero would occur.

In the case of input too large, the division by zero will occur on the
first iteration.  The best result (largest allowed value) will be found
by always choosing the semi-convergent and excluding the denominator
based limit when finding it.

In the case of the input too small, the division by zero will occur on
the second iteration.  The numerator based semi-convergent should not be
calculated to avoid the division by zero.  But the semi-convergent vs
previous convergent test is still needed, which effectively chooses
between 0 (the previous convergent) vs the smallest allowed fraction
(best semi-convergent) as the result.

Reported-by: Yiyuan Guo <yguoaz@gmail.com>
Signed-off-by: Trent Piepho <tpiepho@gmail.com>
---
 lib/math/rational.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/lib/math/rational.c b/lib/math/rational.c
index 9781d521963d..c0ab51d8fbb9 100644
--- a/lib/math/rational.c
+++ b/lib/math/rational.c
@@ -12,6 +12,7 @@
 #include <linux/compiler.h>
 #include <linux/export.h>
 #include <linux/minmax.h>
+#include <linux/limits.h>
 
 /*
  * calculate best rational approximation for a given fraction
@@ -78,13 +79,18 @@ void rational_best_approximation(
 		 * found below as 't'.
 		 */
 		if ((n2 > max_numerator) || (d2 > max_denominator)) {
-			unsigned long t = min((max_numerator - n0) / n1,
-					      (max_denominator - d0) / d1);
+			unsigned long t = ULONG_MAX;
 
-			/* This tests if the semi-convergent is closer
-			 * than the previous convergent.
+			if (d1)
+				t = (max_denominator - d0) / d1;
+			if (n1)
+				t = min(t, (max_numerator - n0) / n1);
+
+			/* 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.
 			 */
-			if (2u * t > a || (2u * t == a && d0 * dp > d1 * d)) {
+			if (!d1 || 2u * t > a || (2u * t == a && d0 * dp > d1 * d)) {
 				n1 = n0 + t * n1;
 				d1 = d0 + t * d1;
 			}
-- 
2.26.2


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

end of thread, other threads:[~2021-05-25 17:10 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-23  0:18 [PATCH] lib/math/rational.c: Fix divide by zero Trent Piepho
2021-05-24 10:51 ` Andy Shevchenko
2021-05-24 16:55   ` Daniel Latypov
2021-05-24 22:04     ` Randy Dunlap
2021-05-24 22:56       ` Daniel Latypov
2021-05-24 23:30         ` Randy Dunlap
2021-05-24 23:38           ` Daniel Latypov
2021-05-25  0:42             ` David Gow
2021-05-25  1:49               ` Randy Dunlap
2021-05-25  1:57                 ` David Gow
2021-05-25  5:08                 ` Trent Piepho
2021-05-24 20:17   ` Trent Piepho
2021-05-24 20:35     ` Daniel Latypov
2021-05-25  9:02     ` Andy Shevchenko
2021-05-25  9:21       ` Trent Piepho
2021-05-25 12:03         ` Andy Shevchenko
2021-05-25 17:10       ` Daniel Latypov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).