Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp2739246pxv; Mon, 12 Jul 2021 00:21:40 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyAYHyijbCv1Kdfn+vZ+bFW4Icsh8nYsCoE+FZPTu6UqGBA8WKY8TLtRK1VDv2TYGpPChnw X-Received: by 2002:a02:93a3:: with SMTP id z32mr12254797jah.33.1626074392684; Mon, 12 Jul 2021 00:19:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626074392; cv=none; d=google.com; s=arc-20160816; b=Ahz8c7hkaJBMH5xuYXeRA6UtytNSKDrdZDeRejcZExT2dVxwohuQ1iFXkk0oSwuOBd xAGCoLjyrIBvTyVPHe9VXA1qPOnsryWKZRNHOO6ym/BkKd+oqVTlJ6kLTKqlnNvSM8Sm tiHN4z9SnT936kE/mz8PDeGrAWS3Xhk5CgW0qMA6Cng4MqpvdrdZ7AzKmWvr2K8YwVrA B+vE4lD70/7xet+vMbydVcVUzGR/fMBDYLAHgAYsUzd82qKdxpbRkiyyRfO8MFNyuFzV Qh8SV0cBI+xlSHqixuaZQ+GnxhmVRZvjI260KzSNFj+Z+32wGCFCvBb1/mGY/T+veIgv Cf/Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=dwtT4FaA2A0zwaNYz+u8mBlGcycbCmq+VqCHMA8BT+I=; b=LT6NBuHLt9PU8hggLceWopRIszWdg1Gp1rGe+ErpB5aJXeL0eIL7ndXJfu7rgrZZDs RAU/gNf2BtePJYoOTtBT4/O+0MLVPabX/WvM9hpze00LcABASETjPTh6PM1bGhOYWr5i 96FvqEgC+3KJINA474a8fdFHR5CldVAcTlMonOo1Wc88YklSC5tkPfc7kK3yYaoR/sYu ytSZt/DCt/TVlGXaanJ2t5t553HkdI8ATigHby8MzEYIjImPpjA6rmBROxj1FlyYURzo iDZtmtiwnHYy+vh+aM/ilqECyoRoA/EgfHcAGdIhGqlMMlmTipJRKorNKDUNSMmtFqT1 lxyw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="k8rqnD4/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id x6si18551623ilv.59.2021.07.12.00.19.40; Mon, 12 Jul 2021 00:19:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="k8rqnD4/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245559AbhGLHTi (ORCPT + 99 others); Mon, 12 Jul 2021 03:19:38 -0400 Received: from mail.kernel.org ([198.145.29.99]:55776 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237708AbhGLGzK (ORCPT ); Mon, 12 Jul 2021 02:55:10 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 86ECE60FD8; Mon, 12 Jul 2021 06:52:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1626072741; bh=fob3s5mDsTJVZqnRf2jvmECc0ABXF+X68QEkJbejXo4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=k8rqnD4/7Fo1W9h44PmFUUxWR886SjfEz+gtwPNhaA7VtJjLEOz+qgUrEJBYw0SO7 2m6ZLku9DocgG++H/nLncd+dxfykK09Nsxo3BQ7d7tfuLjE9FMkKYDd/6nAijlZfQL 6Z7LO/BIp/qyTxysssFTvAiERcKgt1bAPIJ3UwIw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Trent Piepho , Yiyuan Guo , Andy Shevchenko , Oskar Schirmer , Daniel Latypov , Andrew Morton , Linus Torvalds , Sasha Levin Subject: [PATCH 5.10 573/593] lib/math/rational.c: fix divide by zero Date: Mon, 12 Jul 2021 08:12:13 +0200 Message-Id: <20210712060958.032686275@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210712060843.180606720@linuxfoundation.org> References: <20210712060843.180606720@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Trent Piepho [ Upstream commit 65a0d3c14685663ba111038a35db70f559e39336 ] 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: 323dd2c3ed0 ("lib/math/rational.c: fix possible incorrect result from rational fractions helper") Signed-off-by: Trent Piepho Reported-by: Yiyuan Guo Reviewed-by: Andy Shevchenko Cc: Oskar Schirmer Cc: Daniel Latypov Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- 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 #include #include +#include /* * 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.30.2