Skip to content

Commit

Permalink
Add assert for fetch_att and store_att_byval
Browse files Browse the repository at this point in the history
These two functions throw an error if you pass in a strange combination
of typbyval and typlen, so we assert on bad values to get a backtrace.
  • Loading branch information
mkindahl committed Oct 18, 2024
1 parent b65083e commit 39365be
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
10 changes: 10 additions & 0 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,16 @@ extern TSDLLEXPORT List *ts_get_reloptions(Oid relid);
.value = 0, .isnull = true \
}

static inline Datum
ts_fetch_att(const void *T, bool attbyval, int attlen)
{
/* Length should be set to something sensible, otherwise an error will be
* raised by fetch_att, so we assert this here to get a stack for
* violations. */
Assert(!attbyval || (attlen > 0 && attlen <= 8));
return fetch_att(T, attbyval, attlen);
}

static inline int64
int64_min(int64 a, int64 b)
{
Expand Down
8 changes: 7 additions & 1 deletion tsl/src/compression/algorithms/datum_serialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <compat/compat.h>
#include "datum_serialize.h"
#include "src/utils.h"
#include <compression/compression.h>

typedef struct DatumSerializer
Expand Down Expand Up @@ -167,6 +168,11 @@ datum_to_bytes_and_advance(DatumSerializer *serializer, char *start, Size *max_s
start = align_and_zero(start, serializer->type_align, max_size);
data_length = serializer->type_len;
check_allowed_data_len(data_length, *max_size);

/* Data length should be set to something sensible, otherwise an error
* will be raised inside store_att_byval, so we assert here to get a
* stack. */
Assert(data_length > 0 && data_length <= 8);
store_att_byval(start, datum, data_length);
}
else if (serializer->type_len == -1)
Expand Down Expand Up @@ -322,7 +328,7 @@ bytes_to_datum_and_advance(DatumDeserializer *deserializer, const char **ptr)
CheckCompressedData((VARATT_IS_1B(*ptr) && VARSIZE_1B(*ptr) >= VARHDRSZ_SHORT) ||
(VARSIZE_4B(*ptr) > VARHDRSZ));
}
res = fetch_att(*ptr, deserializer->type_by_val, deserializer->type_len);
res = ts_fetch_att(*ptr, deserializer->type_by_val, deserializer->type_len);
*ptr = att_addlength_pointer(*ptr, deserializer->type_len, *ptr);
return res;
}
Expand Down
3 changes: 2 additions & 1 deletion tsl/src/hypercore/arrow_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "arrow_array.h"
#include "compression/arrow_c_data_interface.h"
#include "compression/compression.h"
#include "src/utils.h"

#define TYPLEN_VARLEN (-1)

Expand Down Expand Up @@ -470,7 +471,7 @@ arrow_get_datum_fixlen(const ArrowArray *array, Oid typid, int16 typlen, uint16
/* In order to handle fixed-length values of arbitrary size that are byref
* and byval, we use fetch_all() rather than rolling our own. This is
* taken from utils/adt/rangetypes.c */
Datum datum = fetch_att(&values[index * typlen], apriv->typbyval, typlen);
Datum datum = ts_fetch_att(&values[index * typlen], apriv->typbyval, typlen);

TS_DEBUG_LOG("retrieved fixlen value %s row %u from offset %u"
" in memory context %s",
Expand Down
7 changes: 3 additions & 4 deletions tsl/src/nodes/decompress_chunk/pred_vector_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
#include <postgres.h>

#include "compression/arrow_c_data_interface.h"

#include "vector_predicates.h"

#include "compression/compression.h"
#include "src/utils.h"
#include "vector_predicates.h"

/*
* Vectorized implementation of ScalarArrayOpExpr. Applies scalar_predicate for
Expand Down Expand Up @@ -77,7 +76,7 @@ vector_array_predicate(VectorPredicate *vector_const_predicate, bool is_or,
}
return;
}
Datum constvalue = fetch_att(array_data, typbyval, typlen);
Datum constvalue = ts_fetch_att(array_data, typbyval, typlen);
array_data = att_addlength_pointer(array_data, typlen, array_data);
array_data = (const char *) att_align_nominal(array_data, typalign);

Expand Down

0 comments on commit 39365be

Please sign in to comment.