postgresql/src/backend
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
..
access Store 2PC GID in commit/abort WAL recs for logical decoding 2018-03-28 17:42:50 +01:00
bootstrap Handle heap rewrites even better in logical decoding 2018-03-21 09:15:04 -04:00
catalog Fast ALTER TABLE ADD COLUMN with a non-NULL default 2018-03-28 10:43:52 +10:30
commands Fast ALTER TABLE ADD COLUMN with a non-NULL default 2018-03-28 10:43:52 +10:30
executor Fast ALTER TABLE ADD COLUMN with a non-NULL default 2018-03-28 10:43:52 +10:30
foreign Remove bogus "extern" annotations on function definitions. 2018-02-19 12:07:44 -05:00
jit Quick adaption of JIT tuple deforming to the fast default patch. 2018-03-27 21:03:10 -07:00
lib Minor clean-up in dshash.{c,h}. 2018-03-01 16:25:46 -08:00
libpq Fix typo in comment 2018-03-19 10:45:44 +01:00
main Update copyright for 2018 2018-01-02 23:30:12 -05:00
nodes Basic planner and executor integration for JIT. 2018-03-22 11:51:58 -07:00
optimizer Fast ALTER TABLE ADD COLUMN with a non-NULL default 2018-03-28 10:43:52 +10:30
parser Handle INSERT .. ON CONFLICT with partitioned tables 2018-03-26 10:43:54 -03:00
po Translation updates 2017-08-07 13:55:34 -04:00
port Support huge pages on Windows 2018-01-21 15:40:46 +01:00
postmaster Log when a BRIN autosummarization request fails 2018-03-14 11:59:40 -03:00
regex Update copyright for 2018 2018-01-02 23:30:12 -05:00
replication Allow memory contexts to have both fixed and variable ident strings. 2018-03-27 16:46:51 -04:00
rewrite Fast ALTER TABLE ADD COLUMN with a non-NULL default 2018-03-28 10:43:52 +10:30
snowball Avoid unnecessary use of pg_strcasecmp for already-downcased identifiers. 2018-01-26 18:25:14 -05:00
statistics Fast ALTER TABLE ADD COLUMN with a non-NULL default 2018-03-28 10:43:52 +10:30
storage Skip temp tables from basebackup. 2018-03-27 16:14:40 +03:00
tcop Small refactoring 2018-03-23 17:18:22 -04:00
tsearch Avoid unnecessary use of pg_strcasecmp for already-downcased identifiers. 2018-01-26 18:25:14 -05:00
utils Fix actual and potential double-frees around tuplesort usage. 2018-03-28 13:26:57 -04:00
.gitignore Add .gitignore entries for AIX-specific intermediate build artifacts. 2015-07-08 20:44:22 -04:00
Makefile Fix make rules that generate multiple output files. 2018-03-23 13:46:00 -04:00
common.mk Support for optimizing and emitting code in LLVM JIT provider. 2018-03-22 11:05:22 -07:00
nls.mk Translation updates 2017-05-15 12:19:54 -04:00