diff options
| author | Edward Thomson <ethomson@edwardthomson.com> | 2021-07-19 15:51:53 -0400 |
|---|---|---|
| committer | Edward Thomson <ethomson@edwardthomson.com> | 2021-07-19 15:51:53 -0400 |
| commit | 419ffdde6aa3c4dce60c0ab219d0ca48eaea8f81 (patch) | |
| tree | 2d42629a73116f1b838999272c740ca91a5f30e3 /src/pack-objects.c | |
| parent | 48e6b02be9b0639fdc25e17514f11d7ef141dfbc (diff) | |
| download | libgit2-419ffdde6aa3c4dce60c0ab219d0ca48eaea8f81.tar.gz | |
packbuilder: don't try to malloc(0)
Diffstat (limited to 'src/pack-objects.c')
| -rw-r--r-- | src/pack-objects.c | 26 |
1 files changed, 16 insertions, 10 deletions
diff --git a/src/pack-objects.c b/src/pack-objects.c index 2c964b125..faff310b4 100644 --- a/src/pack-objects.c +++ b/src/pack-objects.c @@ -517,13 +517,18 @@ static int cb_tag_foreach(const char *name, git_oid *oid, void *data) return 0; } -static git_pobject **compute_write_order(git_packbuilder *pb) +static int compute_write_order(git_pobject ***out, git_packbuilder *pb) { size_t i, wo_end, last_untagged; git_pobject **wo; + *out = NULL; + + if (!pb->nr_objects) + return 0; + if ((wo = git__mallocarray(pb->nr_objects, sizeof(*wo))) == NULL) - return NULL; + return -1; for (i = 0; i < pb->nr_objects; i++) { git_pobject *po = pb->object_list + i; @@ -552,7 +557,7 @@ static git_pobject **compute_write_order(git_packbuilder *pb) */ if (git_tag_foreach(pb->repo, &cb_tag_foreach, pb) < 0) { git__free(wo); - return NULL; + return -1; } /* @@ -609,10 +614,11 @@ static git_pobject **compute_write_order(git_packbuilder *pb) if (wo_end != pb->nr_objects) { git__free(wo); git_error_set(GIT_ERROR_INVALID, "invalid write order"); - return NULL; + return -1; } - return wo; + *out = wo; + return 0; } static int write_pack(git_packbuilder *pb, @@ -625,15 +631,15 @@ static int write_pack(git_packbuilder *pb, struct git_pack_header ph; git_oid entry_oid; size_t i = 0; - int error = 0; + int error; - write_order = compute_write_order(pb); - if (write_order == NULL) - return -1; + if ((error = compute_write_order(&write_order, pb)) < 0) + return error; if (!git__is_uint32(pb->nr_objects)) { git_error_set(GIT_ERROR_INVALID, "too many objects"); - return -1; + error = -1; + goto done; } /* Write pack header */ |
