Skip to content

Commit 6593e14

Browse files
pks-tgitster
authored andcommitted
reftable/basics: handle allocation failures in reftable_calloc()
Handle allocation failures in `reftable_calloc()`. While at it, remove our use of `st_mult()` that would cause us to die on an overflow. From the caller's point of view there is not much of a difference between arguments that are too large to be multiplied and a request that is too big to handle by the allocator: in both cases the allocation cannot be fulfilled. And in neither of these cases do we want the reftable library to die. While we could use `unsigned_mult_overflows()` to handle the overflow gracefully, we instead open-code it to further our goal of converting the reftable codebase to become a standalone library that can be reused by external projects. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7f0969f commit 6593e14

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

reftable/basics.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,16 @@ void reftable_free(void *p)
3737

3838
void *reftable_calloc(size_t nelem, size_t elsize)
3939
{
40-
size_t sz = st_mult(nelem, elsize);
41-
void *p = reftable_malloc(sz);
42-
memset(p, 0, sz);
40+
void *p;
41+
42+
if (nelem && elsize > SIZE_MAX / nelem)
43+
return NULL;
44+
45+
p = reftable_malloc(nelem * elsize);
46+
if (!p)
47+
return NULL;
48+
49+
memset(p, 0, nelem * elsize);
4350
return p;
4451
}
4552

0 commit comments

Comments
 (0)