Future-proof sort template against undefined behavior
John Naylor <john.naylor@postgresql.org>
Author:
John Naylor <john.naylor@postgresql.org>
Date: 2026-02-07T10:02:35Z
Releases:
19 (unreleased)
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
Files
| Path | Change | +/− |
|---|---|---|
| src/include/lib/sort_template.h | modified | +21 −19 |
Discussion
- Undefined behavior detected by new clang's ubsan 12 messages · 2026-01-20 → 2026-02-04