Skip to content

Commit d858994

Browse files
committed
Fix use-after-free around DISTINCT transition function calls.
Have tuplesort_gettupleslot() copy the contents of its current table slot as needed. This is based on an approach taken by tuplestore_gettupleslot(). In the future, tuplesort_gettupleslot() may also be taught to avoid copying the tuple where caller can determine that that is safe (the tuplestore_gettupleslot() interface already offers this option to callers). Patch by Peter Geoghegan. Fixes bug #14344, reported by Regina Obe. Report: <20160929035538.20224.39628@wrigleys.postgresql.org> Backpatch-through: 9.6
1 parent 9e083fd commit d858994

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/backend/utils/sort/tuplesort.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2084,6 +2084,10 @@ tuplesort_gettuple_common(Tuplesortstate *state, bool forward,
20842084
* determination of "non-equal tuple" based on simple binary inequality. A
20852085
* NULL value in leading attribute will set abbreviated value to zeroed
20862086
* representation, which caller may rely on in abbreviated inequality check.
2087+
*
2088+
* The slot receives a copied tuple (sometimes allocated in caller memory
2089+
* context) that will stay valid regardless of future manipulations of the
2090+
* tuplesort's state.
20872091
*/
20882092
bool
20892093
tuplesort_gettupleslot(Tuplesortstate *state, bool forward,
@@ -2104,6 +2108,11 @@ tuplesort_gettupleslot(Tuplesortstate *state, bool forward,
21042108
if (state->sortKeys->abbrev_converter && abbrev)
21052109
*abbrev = stup.datum1;
21062110

2111+
if (!should_free)
2112+
{
2113+
stup.tuple = heap_copy_minimal_tuple((MinimalTuple) stup.tuple);
2114+
should_free = true;
2115+
}
21072116
ExecStoreMinimalTuple((MinimalTuple) stup.tuple, slot, should_free);
21082117
return true;
21092118
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy