convert trivial cases to ALLOC_ARRAY

Each of these cases can be converted to use ALLOC_ARRAY or
REALLOC_ARRAY, which has two advantages:

  1. It automatically checks the array-size multiplication
     for overflow.

  2. It always uses sizeof(*array) for the element-size,
     so that it can never go out of sync with the declared
     type of the array.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Jeff King
2016-02-22 17:44:25 -05:00
committed by Junio C Hamano
parent 850d2fec53
commit b32fa95fd8
34 changed files with 75 additions and 64 deletions

View File

@ -406,7 +406,8 @@ void parse_pathspec(struct pathspec *pathspec,
n++;
pathspec->nr = n;
pathspec->items = item = xmalloc(sizeof(*item) * n);
ALLOC_ARRAY(pathspec->items, n);
item = pathspec->items;
pathspec->_raw = argv;
prefixlen = prefix ? strlen(prefix) : 0;
@ -483,7 +484,7 @@ const char **get_pathspec(const char *prefix, const char **pathspec)
void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
{
*dst = *src;
dst->items = xmalloc(sizeof(struct pathspec_item) * dst->nr);
ALLOC_ARRAY(dst->items, dst->nr);
memcpy(dst->items, src->items,
sizeof(struct pathspec_item) * dst->nr);
}