Re: BUG #14722: Segfault in tuplesort_heap_siftup, 32 bit overflow
Sergey Koposov <skoposov@cmu.edu>
From: Sergey Koposov <skoposov@cmu.edu>
To: "pg@bowt.ie" <pg@bowt.ie>
Cc: "pgsql-bugs@postgresql.org" <pgsql-bugs@postgresql.org>
Date: 2017-06-29T17:50:36Z
Lists: pgsql-bugs
On Thu, 2017-06-29 at 10:00 -0700, Peter Geoghegan wrote: > On Thu, Jun 29, 2017 at 9:16 AM, <skoposov@cmu.edu> wrote: > > From a quick look of the code it looks to me that the reason for the bug is > > the 32 bit int overflow in the j=2*i+1 calculation inside the > > tuplesort_heap_siftup leading to negative values of j. > > It seems likely that the explanation is as simple as that. This > happens during run generation with replacement selection. All versions > are affected, but version 9.6+ is dramatically less likely to be > affected, because replacement selection was all but killed in Postgres > 9.6. > > 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. For the time being I've just changed the type of i,j from int to long (or int64) and I am running the index creation now. I let you submit a patch -- thanks in advance. I also noticed that the availMem variable was negative in the printout of the TupleSortState. availMem =-6442450776, I don't know whether that's an issue on its own or was caused by the (i,j) overflow. (availMem seems to be int64 variable though). Sergey
Commits
-
Avoid integer overflow while sifting-up a heap in tuplesort.c.
- e439bbe9996f 9.4.13 landed
- e7213fe2bda8 9.5.8 landed
- 512f67c8d02c 10.0 landed
- 09c598898166 9.6.4 landed
-
Implement binary heap replace-top operation in a smarter way.
- 24598337c8d2 10.0 cited
-
Permit super-MaxAllocSize allocations with MemoryContextAllocHuge().
- 263865a48973 9.4.0 cited