From 576a37e97570393a3fa8536c9d930d38a652d2a8 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Wed, 17 Apr 2024 00:17:40 -0700 Subject: [PATCH] Ensure items are always aligned --- src/ivfbuild.c | 12 +++--------- src/ivfutils.c | 3 +++ 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/ivfbuild.c b/src/ivfbuild.c index e927468..0350e9b 100644 --- a/src/ivfbuild.c +++ b/src/ivfbuild.c @@ -337,20 +337,14 @@ GetMaxDimensions(IvfflatType type) static Size GetItemSize(IvfflatType type, int dimensions) { - Size size; - if (type == IVFFLAT_TYPE_VECTOR) - size = VECTOR_SIZE(dimensions); + return VECTOR_SIZE(dimensions); else if (type == IVFFLAT_TYPE_HALFVEC) - size = HALFVEC_SIZE(dimensions); + return HALFVEC_SIZE(dimensions); else if (type == IVFFLAT_TYPE_BIT) - size = VARBITTOTALLEN(dimensions); + return VARBITTOTALLEN(dimensions); else elog(ERROR, "Unsupported type"); - - /* Ensure items are aligned to prevent UB */ - /* Only need 4 byte alignment, but prefer MAXALIGN */ - return MAXALIGN(size); } /* diff --git a/src/ivfutils.c b/src/ivfutils.c index 26e085d..3fdcba9 100644 --- a/src/ivfutils.c +++ b/src/ivfutils.c @@ -18,6 +18,9 @@ VectorArrayInit(int maxlen, int dimensions, Size itemsize) { VectorArray res = palloc(sizeof(VectorArrayData)); + /* Ensure items are aligned to prevent UB */ + itemsize = MAXALIGN(itemsize); + res->length = 0; res->maxlen = maxlen; res->dim = dimensions;