Skip to content

Commit 2f65b84

Browse files
committed
Fix rare sharedtuplestore.c corruption.
If the final chunk of an oversized tuple being written out to disk was exactly 32760 bytes, it would be corrupted due to a fencepost bug. Bug #17619. Back-patch to 11 where the code arrived. While testing that (see test module in archives), I (tmunro) noticed that the per-participant page counter was not initialized to zero as it should have been; that wasn't a live bug when it was written since DSM memory was originally always zeroed, but since 14 min_dynamic_shared_memory might be configured and it supplies non-zeroed memory, so that is also fixed here. Author: Dmitry Astapov <dastapov@gmail.com> Discussion: https://postgr.es/m/17619-0de62ceda812b8b5%40postgresql.org
1 parent e977415 commit 2f65b84

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

src/backend/utils/sort/sharedtuplestore.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ sts_initialize(SharedTuplestore *sts, int participants,
158158
LWLockInitialize(&sts->participants[i].lock,
159159
LWTRANCHE_SHARED_TUPLESTORE);
160160
sts->participants[i].read_page = 0;
161+
sts->participants[i].npages = 0;
161162
sts->participants[i].writing = false;
162163
}
163164

@@ -319,7 +320,7 @@ sts_puttuple(SharedTuplestoreAccessor *accessor, void *meta_data,
319320

320321
/* Do we have space? */
321322
size = accessor->sts->meta_data_size + tuple->t_len;
322-
if (accessor->write_pointer + size >= accessor->write_end)
323+
if (accessor->write_pointer + size > accessor->write_end)
323324
{
324325
if (accessor->write_chunk == NULL)
325326
{
@@ -339,7 +340,7 @@ sts_puttuple(SharedTuplestoreAccessor *accessor, void *meta_data,
339340
}
340341

341342
/* It may still not be enough in the case of a gigantic tuple. */
342-
if (accessor->write_pointer + size >= accessor->write_end)
343+
if (accessor->write_pointer + size > accessor->write_end)
343344
{
344345
size_t written;
345346

0 commit comments

Comments
 (0)