postgresql/src
Tom Lane c2d4eb1b1f Fix actual and potential double-frees around tuplesort usage.
tuplesort_gettupleslot() passed back tuples allocated in the tuplesort's
own memory context, even when the caller was responsible to free them.
This created a double-free hazard, because some callers might destroy
the tuplesort object (via tuplesort_end) before trying to clean up the
last returned tuple.  To avoid this, change the API to specify that the
tuple is allocated in the caller's memory context.  v10 and HEAD already
did things that way, but in 9.5 and 9.6 this is a live bug that can
demonstrably cause crashes with some grouping-set usages.

In 9.5 and 9.6, this requires doing an extra tuple copy in some cases,
which is unfortunate.  But the amount of refactoring needed to avoid it
seems excessive for a back-patched change, especially since the cases
where an extra copy happens are less performance-critical.

Likewise change tuplesort_getdatum() to return pass-by-reference Datums
in the caller's context not the tuplesort's context.  There seem to be
no live bugs among its callers, but clearly the same sort of situation
could happen in future.

For other tuplesort fetch routines, continue to allocate the memory in
the tuplesort's context.  This is a little inconsistent with what we now
do for tuplesort_gettupleslot() and tuplesort_getdatum(), but that's
preferable to adding new copy overhead in the back branches where it's
clearly unnecessary.  These other fetch routines provide the weakest
possible guarantees about tuple memory lifespan from v10 on, anyway,
so this actually seems more consistent overall.

Adjust relevant comments to reflect these API redefinitions.

Arguably, we should change the pre-9.5 branches as well, but since
there are no known failure cases there, it seems not worth the risk.

Peter Geoghegan, per report from Bernd Helmle.  Reviewed by Kyotaro
Horiguchi; thanks also to Andreas Seltenreich for extracting a
self-contained test case.

Discussion: https://postgr.es/m/1512661638.9720.34.camel@oopsware.de
2018-03-28 13:26:57 -04:00
..
backend Fix actual and potential double-frees around tuplesort usage. 2018-03-28 13:26:57 -04:00
bin Allow HOT updates for some expression indexes 2018-03-27 19:57:02 +01:00
common restrict -> pg_restrict 2018-03-15 10:02:59 -03:00
fe_utils Add \if support to pgbench 2018-03-22 17:42:03 +03:00
include Store 2PC GID in commit/abort WAL recs for logical decoding 2018-03-28 17:42:50 +01:00
interfaces libpq: PQhost to return active connected host or hostaddr 2018-03-27 12:32:18 -04:00
makefiles Always use -fPIC, not -fpic, when building shared libraries with gcc. 2017-06-01 13:32:55 -04:00
pl Allow memory contexts to have both fixed and variable ident strings. 2018-03-27 16:46:51 -04:00
port Add an assertion that we don't pass NULL to snprintf("%s"). 2018-02-14 15:06:01 -05:00
template Force "restrict" not to be used when compiling with xlc. 2017-10-13 12:15:06 -07:00
test Make fast_default regression tests locale independent 2018-03-28 17:06:45 +10:30
timezone Update time zone data files to tzdata release 2018c. 2018-01-27 16:42:28 -05:00
tools Attempt to fix jsonb_plpython build on Windows 2018-03-28 11:49:23 -04:00
tutorial Update copyright for 2018 2018-01-02 23:30:12 -05:00
.gitignore
DEVELOPERS
Makefile Basic JIT provider and error handling infrastructure. 2018-03-21 19:28:28 -07:00
Makefile.global.in Support for optimizing and emitting code in LLVM JIT provider. 2018-03-22 11:05:22 -07:00
Makefile.shlib Fix make rules that generate multiple output files. 2018-03-23 13:46:00 -04:00
nls-global.mk nls-global.mk: search build dir for source files, too 2016-06-07 18:55:18 -04:00