Re: BUG #14722: Segfault in tuplesort_heap_siftup, 32 bit overflow

From: Andres Freund <andres(at)anarazel(dot)de>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Sergey Koposov <skoposov(at)cmu(dot)edu>, "pg(at)bowt(dot)ie" <pg(at)bowt(dot)ie>, "pgsql-bugs(at)postgresql(dot)org" <pgsql-bugs(at)postgresql(dot)org>
Subject: Re: BUG #14722: Segfault in tuplesort_heap_siftup, 32 bit overflow
Date: 2017-07-05 22:14:14
Message-ID: 20170705221414.bgnw73z2mwkjwor7@alap3.anarazel.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On 2017-07-05 18:03:56 -0400, Tom Lane wrote:
> Sergey Koposov <skoposov(at)cmu(dot)edu> 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;

Isn't an added conditional likely going to be more costly than the
s/32/64/ bit calculations on the majority of machines pg runs on? I'm
quite doubtful that it's worth catering for the few cases where that's
really slow.

- Andres

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Peter Geoghegan 2017-07-05 22:19:57 Re: BUG #14722: Segfault in tuplesort_heap_siftup, 32 bit overflow
Previous Message Tom Lane 2017-07-05 22:03:56 Re: BUG #14722: Segfault in tuplesort_heap_siftup, 32 bit overflow