From 3603cb0978b7ef21ff9cd63693ebd6d27bc2bc53 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Tue, 10 Feb 2015 23:13:49 -0500 Subject: git__*allocarray: safer realloc and malloc Introduce git__reallocarray that checks the product of the number of elements and element size for overflow before allocation. Also introduce git__mallocarray that behaves like calloc, but without the `c`. (It does not zero memory, for those truly worried about every cycle.) --- src/vector.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) (limited to 'src/vector.c') diff --git a/src/vector.c b/src/vector.c index b636032b1..27eafebc8 100644 --- a/src/vector.c +++ b/src/vector.c @@ -29,14 +29,9 @@ GIT_INLINE(size_t) compute_new_size(git_vector *v) GIT_INLINE(int) resize_vector(git_vector *v, size_t new_size) { - size_t new_bytes; void *new_contents; - /* Check for overflow */ - GITERR_CHECK_ALLOC_MULTIPLY(new_size, sizeof(void *)); - new_bytes = new_size * sizeof(void *); - - new_contents = git__realloc(v->contents, new_bytes); + new_contents = git__reallocarray(v->contents, new_size, sizeof(void *)); GITERR_CHECK_ALLOC(new_contents); v->_alloc_size = new_size; -- cgit v1.2.1