improve handling of struct got_object_qid allocations
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
diff --git a/lib/commit_graph.c b/lib/commit_graph.c
index bd8091d..f6669fd 100644
--- a/lib/commit_graph.c
+++ b/lib/commit_graph.c
@@ -244,18 +244,12 @@ add_node_to_iter_list(struct got_commit_graph *graph,
static const struct got_error *
add_vertex(struct got_object_id_queue *ids, struct got_object_id *id)
{
+ const struct got_error *err = NULL;
struct got_object_qid *qid;
- qid = calloc(1, sizeof(*qid));
- if (qid == NULL)
- return got_error_from_errno();
-
- qid->id = got_object_id_dup(id);
- if (qid->id == NULL) {
- const struct got_error *err = got_error_from_errno();
- got_object_qid_free(qid);
+ err = got_object_qid_alloc(&qid, id);
+ if (err)
return err;
- }
SIMPLEQ_INSERT_TAIL(ids, qid, entry);
return NULL;
diff --git a/lib/got_lib_object_parse.h b/lib/got_lib_object_parse.h
index b427544..f537052 100644
--- a/lib/got_lib_object_parse.h
+++ b/lib/got_lib_object_parse.h
@@ -14,6 +14,7 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
+const struct got_error *got_object_qid_alloc_partial(struct got_object_qid **);
struct got_commit_object *got_object_commit_alloc_partial(void);
struct got_tree_entry *got_alloc_tree_entry_partial(void);
const struct got_error *got_object_read_header_privsep(struct got_object**,
diff --git a/lib/object_parse.c b/lib/object_parse.c
index 46c648c..ebec640 100644
--- a/lib/object_parse.c
+++ b/lib/object_parse.c
@@ -63,6 +63,26 @@
#define GOT_COMMIT_TAG_COMMITTER "committer "
const struct got_error *
+got_object_qid_alloc_partial(struct got_object_qid **qid)
+{
+ const struct got_error *err = NULL;
+
+ *qid = malloc(sizeof(**qid));
+ if (*qid == NULL)
+ return got_error_from_errno();
+
+ (*qid)->id = malloc(sizeof(*((*qid)->id)));
+ if ((*qid)->id == NULL) {
+ err = got_error_from_errno();
+ got_object_qid_free(*qid);
+ *qid = NULL;
+ return err;
+ }
+
+ return NULL;
+}
+
+const struct got_error *
got_object_id_str(char **outbuf, struct got_object_id *id)
{
static const size_t len = SHA1_DIGEST_STRING_LENGTH;
@@ -135,16 +155,9 @@ got_object_commit_add_parent(struct got_commit_object *commit,
const struct got_error *err = NULL;
struct got_object_qid *qid;
- qid = malloc(sizeof(*qid));
- if (qid == NULL)
- return got_error_from_errno();
-
- qid->id = malloc(sizeof(*qid->id));
- if (qid->id == NULL) {
- err = got_error_from_errno();
- got_object_qid_free(qid);
+ err = got_object_qid_alloc_partial(&qid);
+ if (err)
return err;
- }
if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
err = got_error(GOT_ERR_BAD_OBJ_DATA);
diff --git a/lib/privsep.c b/lib/privsep.c
index 9f128d5..eac5aa1 100644
--- a/lib/privsep.c
+++ b/lib/privsep.c
@@ -592,18 +592,9 @@ got_privsep_recv_commit(struct got_commit_object **commit, struct imsgbuf *ibuf)
for (i = 0; i < icommit.nparents; i++) {
struct got_object_qid *qid;
- qid = calloc(1, sizeof(*qid));
- if (qid == NULL) {
- err = got_error_from_errno();
- break;
- }
- qid->id = calloc(1, sizeof(*qid->id));
- if (qid->id == NULL) {
- err = got_error_from_errno();
- free(qid);
+ err = got_object_qid_alloc_partial(&qid);
+ if (err)
break;
- }
-
memcpy(qid->id, data + len + i * SHA1_DIGEST_LENGTH,
sizeof(*qid->id));
SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, qid, entry);