Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2267689pxj; Sat, 22 May 2021 17:08:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyNpEtCa5paLLvdP9xpdHCCnmYWivUZTmc6IzDwnTuqhR/6ofXUSRBgVjiv4p86Q6Y/EDyU X-Received: by 2002:aa7:cb8d:: with SMTP id r13mr17265291edt.184.1621728513402; Sat, 22 May 2021 17:08:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621728513; cv=none; d=google.com; s=arc-20160816; b=OqI9L2DZajRM5K/uFcSr2QrzWEXEArOR8+7dqgw3UmTEd8/8U8C+s5goxtfII241Pa J6rJY4E2X9prJcfIBO60Y2to9nKAxAWnjSPFCofw88so69udlIOyBQfGenbtF16SQIrC wFuKb3OR8lZ0EHa9eG6jkRDRVcEkZ4r80uoPsEvWRZ41rdLBmpVTuVkyEGkLSDDPSBQn xsn2ULxImzmV8uoMK/xlm9mNJcTuKmviwQyAJPdxQlTBuuOv91LXCa6fMhqyKuPUaM2p IraYey4w5AAkSNcoMaOKWqdCpS5dm2rJfGbHM4PuwyKvMsvl70/nlwnK0CYDdm5hxt0f iWQA== 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=ivy3nqszZrnAQTpRgs8RLYL4mi7jLAex7afb3rEY1SQ=; b=nQnUCZ3P/nXrxBTO98E9qsHkiwXXLmxSrmCvL/49OSoQyIp8YHc/KZMWypdNfLRSjq RobNMwux4pMtpqzsCRBwusXEezFLicBFfXuljeEhCZTc4oEF1BoU7peAEbeQTy8SJ8RJ McgMoikOedsNYmuiqwljhAJunr1HjFoQr3LVtF5EruzWl+HvV09PQOukKO62Jhh3jSWW wbV0Coih5LsiA/wV3u0Xn+eSgJvR2VWHft0YCjeoqpgJIcQX1eYliy62AiydcpQVmtog aCu1ZudX68opt+DUHm6qG7Sp5dM0DAsKsTkDclMFVIWfgDuTMgIQ6wlZz6GXRM4wmssg 3ovg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=hl8pIV4D; 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 op24si9119106ejb.278.2021.05.22.17.08.10; Sat, 22 May 2021 17:08:33 -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=hl8pIV4D; 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 S231452AbhEWAGr (ORCPT + 99 others); Sat, 22 May 2021 20:06:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34210 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231440AbhEWAGr (ORCPT ); Sat, 22 May 2021 20:06:47 -0400 Received: from mail-ej1-x632.google.com (mail-ej1-x632.google.com [IPv6:2a00:1450:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8D723C061574 for ; Sat, 22 May 2021 17:05:20 -0700 (PDT) Received: by mail-ej1-x632.google.com with SMTP id f18so5878036ejq.10 for ; Sat, 22 May 2021 17:05:20 -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=ivy3nqszZrnAQTpRgs8RLYL4mi7jLAex7afb3rEY1SQ=; b=hl8pIV4DGQ7pBk1r9cmtUYmm1pYEbtLnN5Ac5BXtihF0NTCMncOmcVvX8IcnVnAMb8 cPsDij7iGH0XsJlAqf+O19iN2NX7yle88E3UHZ70FdyrvDUcVDB2mXdT0KuPcu2CfOmu Y0VbWcSLqaNks7wtKQHIYVFw+exvz2pf7gCSvwlMCnpS5GU5fFi8eNLm2EJeso+Qk2Cm q/L7VPpJfTAB1U5CDvtl7h15eVRm7jx4CeKN5A1KCCP7crrzbOLiP7TTeGFhFsyxrt/q zBx4e7CfdfBcfWIE0ANmq1y8us3hvLeU8ljIjypvQZk54/cy9WvFBP1Td+iVUrzxLC3e XuWg== 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=ivy3nqszZrnAQTpRgs8RLYL4mi7jLAex7afb3rEY1SQ=; b=QF/l4JhG6mrjpoERxzjMzjpUH1hKdYp4t9KEweg2ChnnqESvnmMZ/V+59MWJYcDWeC +fcEe2+O/hL24FVwCOJK/QhNgefAE3FgkRrQ2qafn4CIL594KaxbTVsb9Lb3PlMMmGGN QD9l7/gs61ad3maO/hmVdIHcx/ICESKu/9Of3ju+wbC8vhT2v2NFAk1VvKdCfDC7jnbq 3iFLnrjyBPS2IT+rNCESR57WYydz0uJ+KiJ02cgJJagWLy9z5B6wouVKODA6Wo6cxXuC 5mpfbeJUJpXbWq5owHIkAb3WL0rCrupk7Ka4cD0CVBWQbe2UiyWwNtxO0C6xZ9XUyxU4 FPuA== X-Gm-Message-State: AOAM5323NSQAatOjQHddoCRqK3H5IbrjDA4iBbhhfi0xQeSV4M7qoFxJ 0uWLlA/LUB7ZX1utbkTtDjQp6Yyvczlg5AGrcP0= X-Received: by 2002:a17:906:5052:: with SMTP id e18mr16755490ejk.112.1621728318923; Sat, 22 May 2021 17:05:18 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Trent Piepho Date: Sat, 22 May 2021 17:05:08 -0700 Message-ID: Subject: Re: A divide by zero bug in lib/math/rational.c (with triggering input) To: Andy Shevchenko Cc: Daniel Latypov , 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 On Fri, May 21, 2021 at 2:53 AM Andy Shevchenko wrote: > > > > 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 came up with some more test cases, and there is another possibility, if the value is small. e.g. rational_best_approximation(1,30, 1,10, ...) rational_best_approximation(1,19, 1,10, ...) The former should be 0/1 and the latter 1/10. These will divide by zero on the 2nd iteration. But I have a patch now that works. It gets the closest answer in all cases, larger than max, less than min but closer to the min than to zero, and closest to zero. It ends up being zero additional arithmetic to do this. All that is needed is a few additional branches in the termination condition.