Skip to content

Commit 367f624

Browse files
authored
Merge pull request libgit2#4803 from tiennou/fix/4802
index: release the snapshot instead of freeing the index
2 parents b3e6ef9 + e0afd1c commit 367f624

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

src/index.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3550,7 +3550,7 @@ int git_index_snapshot_new(git_vector *snap, git_index *index)
35503550
error = git_vector_dup(snap, &index->entries, index->entries._cmp);
35513551

35523552
if (error < 0)
3553-
git_index_free(index);
3553+
git_index_snapshot_release(snap, index);
35543554

35553555
return error;
35563556
}

src/vector.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ GIT_INLINE(int) resize_vector(git_vector *v, size_t new_size)
3232
{
3333
void *new_contents;
3434

35+
if (new_size == 0)
36+
return 0;
37+
3538
new_contents = git__reallocarray(v->contents, new_size, sizeof(void *));
3639
GITERR_CHECK_ALLOC(new_contents);
3740

@@ -50,22 +53,24 @@ int git_vector_size_hint(git_vector *v, size_t size_hint)
5053

5154
int git_vector_dup(git_vector *v, const git_vector *src, git_vector_cmp cmp)
5255
{
53-
size_t bytes;
54-
5556
assert(v && src);
5657

57-
GITERR_CHECK_ALLOC_MULTIPLY(&bytes, src->length, sizeof(void *));
58-
59-
v->_alloc_size = src->length;
58+
v->_alloc_size = 0;
59+
v->contents = NULL;
6060
v->_cmp = cmp ? cmp : src->_cmp;
6161
v->length = src->length;
6262
v->flags = src->flags;
6363
if (cmp != src->_cmp)
6464
git_vector_set_sorted(v, 0);
65-
v->contents = git__malloc(bytes);
66-
GITERR_CHECK_ALLOC(v->contents);
6765

68-
memcpy(v->contents, src->contents, bytes);
66+
if (src->length) {
67+
size_t bytes;
68+
GITERR_CHECK_ALLOC_MULTIPLY(&bytes, src->length, sizeof(void *));
69+
v->contents = git__malloc(bytes);
70+
GITERR_CHECK_ALLOC(v->contents);
71+
v->_alloc_size = src->length;
72+
memcpy(v->contents, src->contents, bytes);
73+
}
6974

7075
return 0;
7176
}

tests/core/vector.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,3 +407,22 @@ void test_core_vector__reverse(void)
407407

408408
git_vector_free(&v);
409409
}
410+
411+
void test_core_vector__dup_empty_vector(void)
412+
{
413+
git_vector v = GIT_VECTOR_INIT;
414+
git_vector dup = GIT_VECTOR_INIT;
415+
void *dummy = 0xDEAFBEEB;
416+
417+
cl_assert_equal_i(0, v.length);
418+
419+
cl_git_pass(git_vector_dup(&dup, &v, v._cmp));
420+
cl_assert_equal_i(0, dup._alloc_size);
421+
cl_assert_equal_i(0, dup.length);
422+
423+
cl_git_pass(git_vector_insert(&dup, dummy));
424+
cl_assert_equal_i(8, dup._alloc_size);
425+
cl_assert_equal_i(1, dup.length);
426+
427+
git_vector_free(&dup);
428+
}

0 commit comments

Comments
 (0)