Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp1395960pxj; Fri, 21 May 2021 13:12:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw5/6wkKZaIeP9oADDK8oTy1qD82dEGuE1vw6G9PJKwrduJicyt87Xxp+Vi6Gdr0/5cUowP X-Received: by 2002:a02:7fc1:: with SMTP id r184mr7281616jac.109.1621627932913; Fri, 21 May 2021 13:12:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621627932; cv=none; d=google.com; s=arc-20160816; b=eM0qb1/8Cu91QdVFsgQuirNRHIjPxcNuStt9V52k2Dl0CNEL4HHhifYUetHa4JvYEF 7QpHGK9Kx7f0Qtbl0j6ATPupbnPzhKCCQ0va06hqrxVrLUR+wkU0NGM8MFEzuD4qCuhu Zsb9UgpI0LQPEDkp7jCyVgojgvPMlZyC7OigCsAusFAg1C5FVFNmN0Ja9Ggbjtyi0VcA G+f4PhlLBCWyy/Ipl4MhJp91BZFETUOeCoz3YrPzG/7UuraUBy5UpFeK8O3hKK8SQL5G fAlq2HPpDVRNK3qvFM1mkLzA9LtZ+eWbV3PXmLrtkaX4Dmdzb4m5aGcFSRBsaOj9m3IQ nlDA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=3SBPs6mShzezlAEKsBtxITwEilEWSuPi+uM6YOTTKA0=; b=foH6Ja8LoKEPMhn3qYvveSDxwk1x993cusq8j2L0QImhWlIFGTfiEuBNxu20qN4sI8 E+bg/FXRQtaQudEJVd5aPHnF0Tu6skrSpRb8iJuk/UFa09+PJrqVN35nA9M83WGKKbSC nNI7vm81djYL5dHe/pcj/wI3Y7fsXh/KJHsFcyVmsaaZGWvCjV+BDsseMAnLdIWDTTq4 qF1EigN1BywCdQI7BFlGzNwIh8KzBkvM+ezARTZ/KoS49mrJ6PLzRwREGfVsV6tHDTxF kME7TOE4Cb9eGf+vAZUk+mVmFEvz3qi2aogh/kawPYpzlYl7ZdM/0QyWNfZ5Ry3MrhAY eiug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=MZr7JvOQ; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l24si6041359ioh.105.2021.05.21.13.12.00; Fri, 21 May 2021 13:12:12 -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=@gmail.com header.s=20161025 header.b=MZr7JvOQ; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234618AbhEUJ6Q (ORCPT + 99 others); Fri, 21 May 2021 05:58:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35938 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237317AbhEUJzJ (ORCPT ); Fri, 21 May 2021 05:55:09 -0400 Received: from mail-pg1-x531.google.com (mail-pg1-x531.google.com [IPv6:2607:f8b0:4864:20::531]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 631F7C061763 for ; Fri, 21 May 2021 02:53:44 -0700 (PDT) Received: by mail-pg1-x531.google.com with SMTP id v14so11093117pgi.6 for ; Fri, 21 May 2021 02:53:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=3SBPs6mShzezlAEKsBtxITwEilEWSuPi+uM6YOTTKA0=; b=MZr7JvOQFyam98C+J1xL7XGvMTEVpYt+9iUJO+AM6hYm3DSm4p04UGBjIlVnREsYGL UyO5WUQv+tKfePEB0+w+gQpFuAaVm+zW6TdDiWRbstgx6StOsiPUj7jcFXSRJx6GxD/b TsWRXVLU/vWDKYmA0dsvqyLiEE8qZYEvkIanbqzOMjJz3N2I437+34U0E7qjjBp0ytHo tfY7fiUYAKA9eO4BXIvcbgICiqrPN2unRSbY17Q6nnVjhT7xVIUQ97dTjbo21Z75KSQj ne0EIZSa/Z+1bg+d/Cf+gUoyNnTY2QKHJPdHRjN5MLzDwHhNqOxB/kX0XTYVyymoc+Wv Lmlg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=3SBPs6mShzezlAEKsBtxITwEilEWSuPi+uM6YOTTKA0=; b=d14XIt2tZ5guwXJ/VQ0nCKMi6fDV6KE1gnulmFAJe/kP5gEQ6aVSV0Pe1g7vpWMYsr huq0PsHPlReHcyI5srQN92bXwooFnKUAJ6rhHjEUejaEdHZmNORX7VY3VeZ5FTALgEry 8Fp62UvljchcHLslbYEmR5JWuiZXktYGH/7B1HnZFGc1nbOeZ+ZAS54RBbXCkz92OabJ 74QtkY0V0lj99cpvi8yrmmo8R5EXQZnP0eD97uyV+OV/Ic/Y38KgkvuNQ3EUAuSZbRok H0YUmsVdSZ63vEz0u+CXamGbMm+fezP3JTAGfUYwYeoUdy8/iqn50ZDfpIslesa8zEMw bZIQ== X-Gm-Message-State: AOAM530ZdYYwIGb2mkxoCjP6jnnnLJ9IS9CGJTvonIzSF1hn4JWT17u+ nqCIK8zHOjMXWo1cGCSsK2tEMsPhEeu5k5v80XU= X-Received: by 2002:a63:4145:: with SMTP id o66mr9106201pga.4.1621590823809; Fri, 21 May 2021 02:53:43 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Andy Shevchenko Date: Fri, 21 May 2021 12:53:27 +0300 Message-ID: Subject: Re: A divide by zero bug in lib/math/rational.c (with triggering input) To: Trent Piepho , Daniel Latypov Cc: Yiyuan guo , "linux-kernel@vger.kernel.org" , "andy@kernel.org" , "akpm@linux-foundation.org" , "oskar@scara.com" Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org +Cc: Daniel (here is a real case for test cases!) On Fri, May 21, 2021 at 12:20 PM Trent Piepho wrote: > On Fri, May 21, 2021 at 12:55 AM Yiyuan guo wrote: > > > > Thanks for your timely response. > > > > I am not familiar with the theorem. But any input satisfying the > > condition below will > > trigger a divide by zero at the first loop iteration: > > > > (given_numerator / given_denominator > max_numerator) || (1 + > > given_numerator / given_denominator > max_denominator) > > I think the error can only occur when the loop exits on the 1st > iteration, when d1 is still zero. In this case the prior convergent, > n1/d1 = 1/0, does not really exist as this is the 1st iteration. The > actual series of convergents generated will never have zero terms, > because we stop at zero, so there will never be zero from the prior > iteration as we would have stopped there. This is my conclusion as well, but you beat me to it. And below is exactly my understanding of what's going on. > I think the prior version of the code, which did not consider > semi-convergents, would have determined the 1st convergent, 314/1, > exceeded the bounds and would return the prior one, 1/0, without > generating an exception but also not a correct answer, since 1/0 isn't > really part of the series, it's just an initial value to make the math > that generates the series work (d2 = a * d1 + d0). > > With semi-convergents, this can actually get the correct answer. The > best semi-convergent term is correctly found, (max_numerator - n0) / > n1 = 255. Using this would return 255/1, which is in this case the > best answer. > > But the "is semi-convergent better than prior convergent" test does > not consider what I think is a special case of there being no prior > convergent. In this case it should always select the semi-convergent. > > I think this handles it: > > if ((n2 > max_numerator) || (d2 > max_denominator)) { > unsigned long t = (max_numerator - n0) / n1; > if (!d1 || (t = min(t, max_denominator - d0) / d1)) || > 2u * t > a || (2u * t == a && d0 * dp > d1 * d)) { > n1 = n0 + t * n1; > d1 = d0 + t * d1; > } > break; > } > > Above !d1 is the special case. I don't like that, but I'm not seeing > a way to think about the problect that doesn't involve one. Let me think about it. > > I think such a condition is rather complex and may not be enforced by > > all callers of this function. > > > > On Fri, May 21, 2021 at 3:42 PM Andy Shevchenko > > wrote: > > > On Friday, May 21, 2021, Andy Shevchenko wrote: > > >> On Friday, May 21, 2021, Yiyuan guo wrote: > > >>> > > >>> In the file lib/math/rational.c, the function > > >>> rational_best_approximation has the following > > >>> code: > > >>> > > >>> void rational_best_approximation( > > >>> unsigned long given_numerator, unsigned long given_denominator, > > >>> unsigned long max_numerator, unsigned long max_denominator, > > >>> unsigned long *best_numerator, unsigned long *best_denominator) { > > >>> ... > > >>> if ((n2 > max_numerator) || (d2 > max_denominator)) { > > >>> unsigned long t = min((max_numerator - n0) / n1, > > >>> (max_denominator - d0) / d1); > > >>> ... > > >>> } > > >>> > > >>> d1 may be equal to zero when performing the division, leading to a > > >>> divide by zero problem. > > >>> > > >>> One input to trigger the divide by zero bug is: > > >>> rational_best_approximation(31415, 100, (1 << 8) - 1, (1 << 5) - 1, &n, &d) > > >> > > >> Have you read a theorem about this? TL;DR; as far as I can see the input data is not suitable for this function. > > > > > > I think we may add the proper check and saturate the output which in your case should be (255,1). -- With Best Regards, Andy Shevchenko