From 2938aa2a5b1cebb41f9e54c1ea289c286139c21e Mon Sep 17 00:00:00 2001 From: Noah Misch Date: Sun, 30 Jun 2019 17:34:17 -0700 Subject: Don't read fields of a misaligned ExpandedObjectHeader or AnyArrayType. UBSan complains about this. Instead, cast to a suitable type requiring only 4-byte alignment. DatumGetAnyArrayP() already assumes one can cast between AnyArrayType and ArrayType, so this doesn't introduce a new assumption. Back-patch to 9.5, where AnyArrayType was introduced. Reviewed by Tom Lane. Discussion: https://postgr.es/m/20190629210334.GA1244217@rfd.leadboat.com --- src/include/utils/array.h | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'src/include/utils/array.h') diff --git a/src/include/utils/array.h b/src/include/utils/array.h index 6164f119ba7..9fd34c6272f 100644 --- a/src/include/utils/array.h +++ b/src/include/utils/array.h @@ -153,7 +153,10 @@ typedef struct ExpandedArrayHeader /* * Functions that can handle either a "flat" varlena array or an expanded - * array use this union to work with their input. + * array use this union to work with their input. Don't refer to "flt"; + * instead, cast to ArrayType. This struct nominally requires 8-byte + * alignment on 64-bit, but it's often used for an ArrayType having 4-byte + * alignment. UBSan complains about referencing "flt" in such cases. */ typedef union AnyArrayType { @@ -307,17 +310,21 @@ typedef struct ArrayIteratorData *ArrayIterator; * Macros for working with AnyArrayType inputs. Beware multiple references! */ #define AARR_NDIM(a) \ - (VARATT_IS_EXPANDED_HEADER(a) ? (a)->xpn.ndims : ARR_NDIM(&(a)->flt)) + (VARATT_IS_EXPANDED_HEADER(a) ? \ + (a)->xpn.ndims : ARR_NDIM((ArrayType *) (a))) #define AARR_HASNULL(a) \ (VARATT_IS_EXPANDED_HEADER(a) ? \ ((a)->xpn.dvalues != NULL ? (a)->xpn.dnulls != NULL : ARR_HASNULL((a)->xpn.fvalue)) : \ - ARR_HASNULL(&(a)->flt)) + ARR_HASNULL((ArrayType *) (a))) #define AARR_ELEMTYPE(a) \ - (VARATT_IS_EXPANDED_HEADER(a) ? (a)->xpn.element_type : ARR_ELEMTYPE(&(a)->flt)) + (VARATT_IS_EXPANDED_HEADER(a) ? \ + (a)->xpn.element_type : ARR_ELEMTYPE((ArrayType *) (a))) #define AARR_DIMS(a) \ - (VARATT_IS_EXPANDED_HEADER(a) ? (a)->xpn.dims : ARR_DIMS(&(a)->flt)) + (VARATT_IS_EXPANDED_HEADER(a) ? \ + (a)->xpn.dims : ARR_DIMS((ArrayType *) (a))) #define AARR_LBOUND(a) \ - (VARATT_IS_EXPANDED_HEADER(a) ? (a)->xpn.lbound : ARR_LBOUND(&(a)->flt)) + (VARATT_IS_EXPANDED_HEADER(a) ? \ + (a)->xpn.lbound : ARR_LBOUND((ArrayType *) (a))) /* -- cgit v1.2.3