Subject: pgsql: Fix various instances of undefined behavior

From: John Naylor <[email-masked]>
Date: Wed, 04 Feb 2026 11:09:09 +0000

Fix various instances of undefined behavior

Mostly this involves checking for NULL pointer before doing operations
that add a non-zero offset.

The exception is an overflow warning in heap_fetch_toast_slice(). This
was caused by unneeded parentheses forcing an expression to be
evaluated to a negative integer, which then got cast to size_t.

Per clang 21 undefined behavior sanitizer.

Backpatch to all supported versions.

Co-authored-by: Alexander Lakhin <[email-masked]>
Reported-by: Alexander Lakhin <[email-masked]>
Discussion: https://postgr.es/m/[email-masked]
Backpatch-through: 14

Branch
------
REL_17_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/1662cd0cb7ae42334af17caa7dceb172b45248f7

Modified Files
--------------
contrib/pg_trgm/trgm_gist.c               | 5 ++++-
src/backend/access/heap/heaptoast.c       | 2 +-
src/backend/utils/adt/multirangetypes.c   | 5 +++--
src/backend/utils/sort/sharedtuplestore.c | 3 ++-
4 files changed, 10 insertions(+), 5 deletions(-)