Skip to content

Commit 45489a1

Browse files
authored
Merge pull request libgit2#6008 from boretrk/array
git_array_alloc: return objects of correct type
2 parents 34e685c + f062eb6 commit 45489a1

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/array.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141

4242
typedef git_array_t(char) git_array_generic_t;
4343

44-
/* use a generic array for growth so this can return the new item */
45-
GIT_INLINE(void *) git_array_grow(void *_a, size_t item_size)
44+
/* use a generic array for growth, return 0 on success */
45+
GIT_INLINE(int) git_array_grow(void *_a, size_t item_size)
4646
{
4747
volatile git_array_generic_t *a = _a;
4848
size_t new_size;
@@ -59,18 +59,18 @@ GIT_INLINE(void *) git_array_grow(void *_a, size_t item_size)
5959
if ((new_array = git__reallocarray(a->ptr, new_size, item_size)) == NULL)
6060
goto on_oom;
6161

62-
a->ptr = new_array; a->asize = new_size; a->size++;
63-
return a->ptr + (a->size - 1) * item_size;
62+
a->ptr = new_array;
63+
a->asize = new_size;
64+
return 0;
6465

6566
on_oom:
6667
git_array_clear(*a);
67-
return NULL;
68+
return -1;
6869
}
6970

7071
#define git_array_alloc(a) \
71-
(((a).size >= (a).asize) ? \
72-
git_array_grow(&(a), sizeof(*(a).ptr)) : \
73-
((a).ptr ? &(a).ptr[(a).size++] : (void *)NULL))
72+
(((a).size < (a).asize || git_array_grow(&(a), sizeof(*(a).ptr)) == 0) ? \
73+
&(a).ptr[(a).size++] : (void *)NULL)
7474

7575
#define git_array_last(a) ((a).size ? &(a).ptr[(a).size - 1] : (void *)NULL)
7676

0 commit comments

Comments
 (0)