]> git.ipfire.org Git - thirdparty/postgresql.git/commit
Future-proof sort template against undefined behavior
authorJohn Naylor <john.naylor@postgresql.org>
Sat, 7 Feb 2026 10:02:35 +0000 (17:02 +0700)
committerJohn Naylor <john.naylor@postgresql.org>
Sat, 7 Feb 2026 10:02:35 +0000 (17:02 +0700)
commit7467041cde9ed1966cb3ea18da8ac119b462c2e4
tree6707b7c64f3cd319f503ed427ab99e13c5af59b8
parent0af05b5dbb42387957582e76232dc27138382e5a
Future-proof sort template against undefined behavior

Commit 176dffdf7 added a NULL array pointer check before performing
a qsort in order to prevent undefined behavior when passing NULL
pointer and zero length. To head off future degenerate cases, check
that there are at least two elements to sort before proceeding with
insertion sort. This has the added advantage of allowing us to remove
four equivalent checks that guarded against recursion/iteration.

There might be a tiny performance penalty from unproductive
recursions, but we can buy that back by increasing the insertion sort
threshold. That is left for future work.

Discussion: https://postgr.es/m/CANWCAZZWvds_35nXc4vXD-eBQa_=mxVtqZf-PM_ps=SD7ghhJg@mail.gmail.com
src/include/lib/sort_template.h