diff options
| author | Etienne Samson <samson.etienne@gmail.com> | 2018-09-26 19:15:35 +0000 |
|---|---|---|
| committer | Etienne Samson <samson.etienne@gmail.com> | 2018-09-26 21:17:17 +0200 |
| commit | fa48d2ea7d2d5dc9620e5c9f05ba8d788775582b (patch) | |
| tree | a59eab4a87b36b7fe575f8793fd6fb516dc1d1fe | |
| parent | c70713d6e4af563696563e410864eb4a6507757d (diff) | |
| download | libgit2-fa48d2ea7d2d5dc9620e5c9f05ba8d788775582b.tar.gz | |
vector: do not malloc 0-length vectors on dup
| -rw-r--r-- | src/vector.c | 18 | ||||
| -rw-r--r-- | tests/core/vector.c | 19 |
2 files changed, 29 insertions, 8 deletions
diff --git a/src/vector.c b/src/vector.c index b12fa942d..aac4863d4 100644 --- a/src/vector.c +++ b/src/vector.c @@ -50,22 +50,24 @@ int git_vector_size_hint(git_vector *v, size_t size_hint) int git_vector_dup(git_vector *v, const git_vector *src, git_vector_cmp cmp) { - size_t bytes; - assert(v && src); - GITERR_CHECK_ALLOC_MULTIPLY(&bytes, src->length, sizeof(void *)); - - v->_alloc_size = src->length; + v->_alloc_size = 0; + v->contents = NULL; v->_cmp = cmp ? cmp : src->_cmp; v->length = src->length; v->flags = src->flags; if (cmp != src->_cmp) git_vector_set_sorted(v, 0); - v->contents = git__malloc(bytes); - GITERR_CHECK_ALLOC(v->contents); - memcpy(v->contents, src->contents, bytes); + if (src->length) { + size_t bytes; + GITERR_CHECK_ALLOC_MULTIPLY(&bytes, src->length, sizeof(void *)); + v->contents = git__malloc(bytes); + GITERR_CHECK_ALLOC(v->contents); + v->_alloc_size = src->length; + memcpy(v->contents, src->contents, bytes); + } return 0; } diff --git a/tests/core/vector.c b/tests/core/vector.c index 4e6f6fc7f..6b8ea574d 100644 --- a/tests/core/vector.c +++ b/tests/core/vector.c @@ -407,3 +407,22 @@ void test_core_vector__reverse(void) git_vector_free(&v); } + +void test_core_vector__dup_empty_vector(void) +{ + git_vector v = GIT_VECTOR_INIT; + git_vector dup = GIT_VECTOR_INIT; + void *dummy = 0xDEAFBEEB; + + cl_assert_equal_i(0, v.length); + + cl_git_pass(git_vector_dup(&dup, &v, v._cmp)); + cl_assert_equal_i(0, dup._alloc_size); + cl_assert_equal_i(0, dup.length); + + cl_git_pass(git_vector_insert(&dup, dummy)); + cl_assert_equal_i(8, dup._alloc_size); + cl_assert_equal_i(1, dup.length); + + git_vector_free(&dup); +} |
