提交 f5c1418b 编写于 作者: T Trent Piepho 提交者: Zheng Zengkai

lib/math/rational.c: fix divide by zero

stable inclusion
from stable-5.10.50
commit 456554040e5af2b047551ff62c4d483df7a900db
bugzilla: 174522 https://gitee.com/openeuler/kernel/issues/I4DNFY

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=456554040e5af2b047551ff62c4d483df7a900db

--------------------------------

[ Upstream commit 65a0d3c1 ]

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: 323dd2c3 ("lib/math/rational.c: fix possible incorrect result from rational fractions helper")
Signed-off-by: NTrent Piepho <tpiepho@gmail.com>
Reported-by: NYiyuan Guo <yguoaz@gmail.com>
Reviewed-by: NAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: Oskar Schirmer <oskar@scara.com>
Cc: Daniel Latypov <dlatypov@google.com>
Signed-off-by: NAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: NLinus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: NSasha Levin <sashal@kernel.org>
Signed-off-by: NChen Jun <chenjun102@huawei.com>
Acked-by: NWeilong Chen <chenweilong@huawei.com>
Signed-off-by: NChen Jun <chenjun102@huawei.com>
Signed-off-by: NZheng Zengkai <zhengzengkai@huawei.com>
上级 410ec3e3
......@@ -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;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册