summaryrefslogtreecommitdiff
path: root/src/array.h
diff options
context:
space:
mode:
authorPeter Pettersson <boretrk@hotmail.com>2021-08-25 22:12:57 +0200
committerPeter Pettersson <boretrk@hotmail.com>2021-08-25 22:12:57 +0200
commitf062eb62116f7c3d7c71054a658dd023b319f280 (patch)
tree5a3dd25d2061adcbc269475312a355443446516d /src/array.h
parent7f1dd7030664fa15caeb6e20578bcb9543026ba7 (diff)
downloadlibgit2-f062eb62116f7c3d7c71054a658dd023b319f280.tar.gz
git_array_alloc: return objects of correct type
Diffstat (limited to 'src/array.h')
-rw-r--r--src/array.h16
1 files changed, 8 insertions, 8 deletions
diff --git a/src/array.h b/src/array.h
index e649d845b..e97688b36 100644
--- a/src/array.h
+++ b/src/array.h
@@ -41,8 +41,8 @@
typedef git_array_t(char) git_array_generic_t;
-/* use a generic array for growth so this can return the new item */
-GIT_INLINE(void *) git_array_grow(void *_a, size_t item_size)
+/* use a generic array for growth, return 0 on success */
+GIT_INLINE(int) git_array_grow(void *_a, size_t item_size)
{
volatile git_array_generic_t *a = _a;
size_t new_size;
@@ -59,18 +59,18 @@ GIT_INLINE(void *) git_array_grow(void *_a, size_t item_size)
if ((new_array = git__reallocarray(a->ptr, new_size, item_size)) == NULL)
goto on_oom;
- a->ptr = new_array; a->asize = new_size; a->size++;
- return a->ptr + (a->size - 1) * item_size;
+ a->ptr = new_array;
+ a->asize = new_size;
+ return 0;
on_oom:
git_array_clear(*a);
- return NULL;
+ return -1;
}
#define git_array_alloc(a) \
- (((a).size >= (a).asize) ? \
- git_array_grow(&(a), sizeof(*(a).ptr)) : \
- ((a).ptr ? &(a).ptr[(a).size++] : (void *)NULL))
+ (((a).size < (a).asize || git_array_grow(&(a), sizeof(*(a).ptr)) == 0) ? \
+ &(a).ptr[(a).size++] : (void *)NULL)
#define git_array_last(a) ((a).size ? &(a).ptr[(a).size - 1] : (void *)NULL)