diff options
| author | Edward Thomson <ethomson@microsoft.com> | 2015-02-10 23:13:49 -0500 |
|---|---|---|
| committer | Edward Thomson <ethomson@edwardthomson.com> | 2015-02-12 22:54:47 -0500 |
| commit | 3603cb0978b7ef21ff9cd63693ebd6d27bc2bc53 (patch) | |
| tree | 20f97cffa9b4f44d3ab90bc1686a64d57223a322 /src/pack-objects.c | |
| parent | 15d54fdd345dadc2854200ce5b9aad0949e3949b (diff) | |
| download | libgit2-3603cb0978b7ef21ff9cd63693ebd6d27bc2bc53.tar.gz | |
git__*allocarray: safer realloc and malloc
Introduce git__reallocarray that checks the product of the number
of elements and element size for overflow before allocation. Also
introduce git__mallocarray that behaves like calloc, but without the
`c`. (It does not zero memory, for those truly worried about every
cycle.)
Diffstat (limited to 'src/pack-objects.c')
| -rw-r--r-- | src/pack-objects.c | 14 |
1 files changed, 5 insertions, 9 deletions
diff --git a/src/pack-objects.c b/src/pack-objects.c index aea4770af..cd0deb29a 100644 --- a/src/pack-objects.c +++ b/src/pack-objects.c @@ -205,9 +205,8 @@ int git_packbuilder_insert(git_packbuilder *pb, const git_oid *oid, GITERR_CHECK_ALLOC_MULTIPLY(pb->nr_alloc + 1024, 3 / 2); pb->nr_alloc = (pb->nr_alloc + 1024) * 3 / 2; - GITERR_CHECK_ALLOC_MULTIPLY(pb->nr_alloc, sizeof(*po)); - pb->object_list = git__realloc(pb->object_list, - pb->nr_alloc * sizeof(*po)); + pb->object_list = git__reallocarray(pb->object_list, + pb->nr_alloc, sizeof(*po)); GITERR_CHECK_ALLOC(pb->object_list); rehash(pb); } @@ -505,8 +504,7 @@ static git_pobject **compute_write_order(git_packbuilder *pb) unsigned int i, wo_end, last_untagged; git_pobject **wo; - if (GIT_ALLOC_OVERFLOW_MULTIPLY(pb->nr_objects, sizeof(*wo)) || - (wo = git__malloc(pb->nr_objects * sizeof(*wo))) == NULL) { + if ((wo = git__mallocarray(pb->nr_objects, sizeof(*wo))) == NULL) { giterr_set_oom(); return NULL; } @@ -1102,8 +1100,7 @@ static int ll_find_deltas(git_packbuilder *pb, git_pobject **list, return 0; } - GITERR_CHECK_ALLOC_MULTIPLY(pb->nr_threads, sizeof(*p)); - p = git__malloc(pb->nr_threads * sizeof(*p)); + p = git__mallocarray(pb->nr_threads, sizeof(*p)); GITERR_CHECK_ALLOC(p); /* Partition the work among the threads */ @@ -1254,8 +1251,7 @@ static int prepare_pack(git_packbuilder *pb) if (pb->progress_cb) pb->progress_cb(GIT_PACKBUILDER_DELTAFICATION, 0, pb->nr_objects, pb->progress_cb_payload); - GITERR_CHECK_ALLOC_MULTIPLY(pb->nr_objects, sizeof(*delta_list)); - delta_list = git__malloc(pb->nr_objects * sizeof(*delta_list)); + delta_list = git__mallocarray(pb->nr_objects, sizeof(*delta_list)); GITERR_CHECK_ALLOC(delta_list); for (i = 0; i < pb->nr_objects; ++i) { |
