Avoid faulty alignment of Datums in build_sorted_items().
authorTom Lane <tgl@sss.pgh.pa.us>
Wed, 10 Sep 2025 21:51:24 +0000 (17:51 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Wed, 10 Sep 2025 21:51:24 +0000 (17:51 -0400)
commit09036dc71c682b0bf7234ed39c1429ed99fbe442
tree7c47fc117554e185d25c854e2b6c9acbd61e4768
parentbdc6cfcd12f5c95799328e05aa4bfa75cfe3e79f
Avoid faulty alignment of Datums in build_sorted_items().

If sizeof(Pointer) is 4 then sizeof(SortItem) will be 12, so that
if data->numrows is odd then we placed the values array at a location
that's not a multiple of 8.  That was fine when sizeof(Datum) was also
4, but in the wake of commit 2a600a93c it makes some alignment-picky
machines unhappy.  (You need a 32-bit machine that nonetheless expects
8-byte alignment of 8-byte quantities, which is an odd-seeming
combination but it does exist outside the Intel universe.)

To fix, MAXALIGN the space allocated to the SortItem array.
In passing, let's make the "len" variable be Size not int,
just for paranoia's sake.

This code was arguably not too safe even before 2a600a93c, but at
present I don't see a strong argument for back-patching.

Reported-by: Tomas Vondra <tomas@vondra.me>
Author: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/87036018-8d70-40ad-a0ac-192b07bd7b04@vondra.me
src/backend/statistics/extended_stats.c