10000 Merge pull request #6814 from libgit2/ethomson/alloc · libgit2/libgit2@f5c6b6a · GitHub
[go: up one dir, main page]

Skip to content

Commit f5c6b6a

Browse files
authored
Merge pull request #6814 from libgit2/ethomson/alloc
Update git_array allocator to obey strict aliasing rules
2 parents 9a2afb7 + 54218b5 commit f5c6b6a

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

src/util/array.h

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,39 +41,40 @@
4141

4242
#define GIT_ERROR_CHECK_ARRAY(a) GIT_ERROR_CHECK_ALLOC((a).ptr)
4343

44-
45-
typedef git_array_t(char) git_array_generic_t;
46-
47-
/* use a generic array for growth, return 0 on success */
48-
GIT_INLINE(int) git_array_grow(void *_a, size_t item_size)
44+
GIT_INLINE(void *) git_array__alloc(void *arr, size_t *size, size_t *asize, size_t item_size)
4945
{
50-
volatile git_array_generic_t *a = _a;
5146
size_t new_size;
52-
char *new_array;
47+
void *new_array;
48+
49+
if (*size 8000 < *asize)
50+
return arr;
5351

54-
if (a->size < 8) {
52+
if (*size < 8) {
5553
new_size = 8;
5654
} else {
57-
if (GIT_MULTIPLY_SIZET_OVERFLOW(&new_size, a->size, 3))
55+
if (GIT_MULTIPLY_SIZET_OVERFLOW(&new_size, *asize, 3))
5856
goto on_oom;
57+
5958
new_size /= 2;
6059
}
6160

62-
if ((new_array = git__reallocarray(a->ptr, new_size, item_size)) == NULL)
61+
if ((new_array = git__reallocarray(arr, new_size, item_size)) == NULL)
6362
goto on_oom;
6463

65-
a->ptr = new_array;
66-
a->asize = new_size;
67-
return 0;
64+
*asize = new_size;
65+
66+
return new_array;
6867

6968
on_oom:
70-
git_array_clear(*a);
71-
return -1;
69+
git__free(arr);
70+
*size = 0;
71+
*asize = 0;
72+
return NULL;
7273
}
7374

7475
#define git_array_alloc(a) \
75-
(((a).size < (a).asize || git_array_grow(&(a), sizeof(*(a).ptr)) == 0) ? \
76-
&(a).ptr[(a).size++] : (void *)NULL)
76+
(((a).size < (a).asize || \
77+
((a).ptr = git_array__alloc((a).ptr, &(a).size, &(a).asize, sizeof(*(a).ptr))) != NULL) ? &(a).ptr[(a).size++] : (void *)NULL)
7778

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

0 commit comments

Comments
 (0)
0