Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dSsPg-0006RB-OH for pgsql-bugs@arkaria.postgresql.org; Wed, 05 Jul 2017 22:04:48 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dSsPg-0002IB-1Q for pgsql-bugs@arkaria.postgresql.org; Wed, 05 Jul 2017 22:04:48 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dSsOx-00012U-1H for pgsql-bugs@postgresql.org; Wed, 05 Jul 2017 22:04:03 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dSsOu-0001HU-7U for pgsql-bugs@postgresql.org; Wed, 05 Jul 2017 22:04:01 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id v65M3uhk028907; Wed, 5 Jul 2017 18:03:56 -0400 From: Tom Lane To: Sergey Koposov cc: "pg@bowt.ie" , "pgsql-bugs@postgresql.org" Subject: Re: BUG #14722: Segfault in tuplesort_heap_siftup, 32 bit overflow In-reply-to: <1499281579.8258.32.camel@cmu.edu> References: <20170629161637.1478.93109@wrigleys.postgresql.org> <1499281579.8258.32.camel@cmu.edu> Comments: In-reply-to Sergey Koposov message dated "Wed, 05 Jul 2017 19:06:19 -0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <28905.1499292236.1@sss.pgh.pa.us> Date: Wed, 05 Jul 2017 18:03:56 -0400 Message-ID: <28906.1499292236@sss.pgh.pa.us> List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org Sergey Koposov writes: > On Thu, 2017-06-29 at 10:00 -0700, Peter Geoghegan wrote: >> This is an oversight in commit 263865a. The fix is to use a variable >> that won't overflow in tuplesort_heap_siftup() -- this is probably a >> one-liner, because when the variable overflows today, the correct >> behavior would be for control to break out of the loop that declares >> the overflowing variable "j", and, I don't see any similar problem in >> other heap maintenance routines. It's a very isolated problem. >> >> I could write a patch. > Just to avoid being forgotten, I attach a trivial patch against 9.5 > branch as well as have created a commitfest submission > https://commitfest.postgresql.org/14/1189/ I don't like s/int/int64/g as a fix for this. That loop is probably a hot spot, and this fix is going to be expensive on any machine where int64 isn't the native word width. How about something like this instead: - int j = 2 * i + 1; + int j; + if (unlikely(i > INT_MAX / 2)) + break; /* if j would overflow, we're done */ + j = 2 * i + 1; if (j >= n) break; regards, tom lane -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs