Commit 5df4932d8e7905f8e04dbf16e3879da18a091cec

Stefan Sperling 2018-11-05T18:25:15

improve handling of struct got_object_qid allocations

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);