]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Fix theoretical bug in tuplesort
authorDavid Rowley <drowley@postgresql.org>
Tue, 13 Jul 2021 00:45:35 +0000 (12:45 +1200)
committerDavid Rowley <drowley@postgresql.org>
Tue, 13 Jul 2021 00:45:35 +0000 (12:45 +1200)
This fixes a theoretical bug in tuplesort.c which, if a bounded sort was
used in combination with a byval Datum sort (tuplesort_begin_datum), when
switching the sort to a bounded heap in make_bounded_heap(), we'd call
free_sort_tuple().  The problem was that when sorting Datums of a byval
type, the tuple is NULL and free_sort_tuple() would free the memory for it
regardless of that.  This would result in a crash.

Here we fix that simply by adding a check to see if the tuple is NULL
before trying to disassociate and free any memory belonging to it.

The reason this bug is only theoretical is that nowhere in the current
code base do we do tuplesort_set_bound() when performing a Datum sort.
However, let's backpatch a fix for this as if any extension uses the code
in this way then it's likely to cause problems.

Author: Ronan Dunklau
Discussion: https://postgr.es/m/CAApHDvpdoqNC5FjDb3KUTSMs5dg6f+XxH4Bg_dVcLi8UYAG3EQ@mail.gmail.com
Backpatch-through: 9.6, oldest supported version

src/backend/utils/sort/tuplesort.c

index f770b570eb6072f145ac49f3fc5a41fffd5bed7b..d7f106dba577156c7bb151d8c66f57cd29f0b6ce 100644 (file)
@@ -4584,6 +4584,9 @@ leader_takeover_tapes(Tuplesortstate *state)
 static void
 free_sort_tuple(Tuplesortstate *state, SortTuple *stup)
 {
-       FREEMEM(state, GetMemoryChunkSpace(stup->tuple));
-       pfree(stup->tuple);
+       if (stup->tuple)
+       {
+               FREEMEM(state, GetMemoryChunkSpace(stup->tuple));
+               pfree(stup->tuple);
+       }
 }