[PATCH v2 2/2] reftable/basics: widen allocation size arguments to uintmax_t
- From
- Matthew Suozzo via GitGitGadget <gitgitgadget@gmail.com>
- Date
- Oct 21, 2026, 21:03 UTC
- Message-ID
- <8f4a8389d0c022de216d1086117fd23d2a8260a9.1792616615.git.gitgitgadget@gmail.com>
- In-Reply-To
- <pull.2255.v2.git.1792616615.gitgitgadget@gmail.com>
From: Matthew Suozzo <m@suozzo.co>
reftable_alloc_size() takes its arguments as size_t, but reftable_obj_record_decode() passes it a uint64_t count. On 32-bit platforms, a count wider than 32 bits is truncated before the overflow check, so the decoder can get a small allocation for a large count and write past its end. reftable_calloc() takes size_t arguments the same way.
Make reftable_alloc_size() and reftable_calloc() take uintmax_t arguments, so the check sees the full count.
Assisted-by: Claude Opus 5.5 Helped-by: Patrick Steinhardt <ps@pks.im> Helped-by: Aniket Gupta <aniketgupta3001@gmail.com> Signed-off-by: Matthew Suozzo <m@suozzo.co> --- reftable/basics.c | 9 +++++---- reftable/basics.h | 5 +++-- t/unit-tests/u-reftable-basics.c | 18 ++++++++++++++++++ 3 files changed, 26 insertions(+), 6 deletions(-)
diff --git a/reftable/basics.c b/reftable/basics.c index f0442a46cf..6103b72b44 100644 --- a/reftable/basics.c +++ b/reftable/basics.c @@ -44,18 +44,19 @@ void reftable_free(void *p) free(p); } -void *reftable_calloc(size_t nelem, size_t elsize) +void *reftable_calloc(uintmax_t nelem, uintmax_t elsize) { + size_t size; void *p; - if (nelem && elsize > SIZE_MAX / nelem) + if (reftable_alloc_size(nelem, elsize, &size) < 0) return NULL; - p = reftable_malloc(nelem * elsize); + p = reftable_malloc(size); if (!p) return NULL; - memset(p, 0, nelem * elsize); + memset(p, 0, size); return p; } diff --git a/reftable/basics.h b/reftable/basics.h index e4b83b2b03..29d2b799ca 100644 --- a/reftable/basics.h +++ b/reftable/basics.h @@ -184,10 +184,11 @@ size_t names_length(const char **names); void *reftable_malloc(size_t sz); void *reftable_realloc(void *p, size_t sz); void reftable_free(void *p); -void *reftable_calloc(size_t nelem, size_t elsize); +void *reftable_calloc(uintmax_t nelem, uintmax_t elsize); char *reftable_strdup(const char *str); -static inline int reftable_alloc_size(size_t nelem, size_t elsize, size_t *out) +static inline int reftable_alloc_size(uintmax_t nelem, uintmax_t elsize, + size_t *out) { if (nelem && elsize > SIZE_MAX / nelem) return -1; diff --git a/t/unit-tests/u-reftable-basics.c b/t/unit-tests/u-reftable-basics.c index c5d83b6714..37bf1ade5a 100644 --- a/t/unit-tests/u-reftable-basics.c +++ b/t/unit-tests/u-reftable-basics.c @@ -200,6 +200,24 @@ void test_reftable_basics__put_get_be16(void) cl_assert_equal_i(in, out); } +void test_reftable_basics__alloc_size(void) +{ + size_t out; + + cl_assert_equal_i(reftable_alloc_size(3, 5, &out), 0); + cl_assert_equal_i(out, 15); + cl_assert(reftable_alloc_size(SIZE_MAX, 2, &out) < 0); + + /* Counts that do not fit in a size_t. */ + if (SIZE_MAX < UINTMAX_MAX) { + cl_assert(reftable_alloc_size((uintmax_t)SIZE_MAX + 1, + 1, &out) < 0); + cl_assert(reftable_alloc_size(1, (uintmax_t)SIZE_MAX + 1, + &out) < 0); + cl_assert(reftable_calloc((uintmax_t)SIZE_MAX + 1, 1) == NULL); + } +} + void test_reftable_basics__alloc_grow(void) { int *arr = NULL, *old_arr;
-- gitgitgadget