linux-stable/lib/math
Trent Piepho 456554040e lib/math/rational.c: fix divide by zero
[ Upstream commit 65a0d3c146 ]

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.

Link: https://lkml.kernel.org/r/20210525144250.214670-1-tpiepho@gmail.com
Fixes: 323dd2c3ed ("lib/math/rational.c: fix possible incorrect result from rational fractions helper")
Signed-off-by: Trent Piepho <tpiepho@gmail.com>
Reported-by: Yiyuan Guo <yguoaz@gmail.com>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: Oskar Schirmer <oskar@scara.com>
Cc: Daniel Latypov <dlatypov@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
2021-07-14 16:56:51 +02:00
..
cordic.c
div64.c math: Export mul_u64_u64_div_u64 2021-04-10 13:36:10 +02:00
gcd.c treewide: Add SPDX license identifier for missed files 2019-05-21 10:50:45 +02:00
int_pow.c
int_sqrt.c
Kconfig lib: Make prime number generator independently selectable 2020-06-02 10:25:20 -06:00
lcm.c treewide: Add SPDX license identifier for missed files 2019-05-21 10:50:45 +02:00
Makefile treewide: Add SPDX license identifier - Makefile/Kconfig 2019-05-21 10:50:46 +02:00
prime_numbers.c lib/math: avoid trailing newline hidden in pr_fmt() 2020-06-04 19:06:24 -07:00
rational.c lib/math/rational.c: fix divide by zero 2021-07-14 16:56:51 +02:00
reciprocal_div.c kernel.h: split out min()/max() et al. helpers 2020-10-16 11:11:19 -07:00