Commit 3b2153feb111409d820ed41a5cf8c72343866dfb

Edward Thomson 2020-04-05T14:42:44

commit: use GIT_ASSERT

diff --git a/src/commit.c b/src/commit.c
index cf9902d..96259d5 100644
--- a/src/commit.c
+++ b/src/commit.c
@@ -53,7 +53,8 @@ static int git_commit__create_buffer_internal(
 	size_t i = 0;
 	const git_oid *parent;
 
-	assert(out && tree);
+	GIT_ASSERT_ARG(out);
+	GIT_ASSERT_ARG(tree);
 
 	git_oid__writebuf(out, "tree ", tree);
 
@@ -229,7 +230,8 @@ int git_commit_create_v(
 	int error = 0;
 	commit_parent_varargs data;
 
-	assert(tree && git_tree_owner(tree) == repo);
+	GIT_ASSERT_ARG(tree);
+	GIT_ASSERT_ARG(git_tree_owner(tree) == repo);
 
 	data.total = parent_count;
 	va_start(data.args, parent_count);
@@ -306,7 +308,8 @@ int git_commit_create(
 {
 	commit_parent_data data = { parent_count, parents, repo };
 
-	assert(tree && git_tree_owner(tree) == repo);
+	GIT_ASSERT_ARG(tree);
+	GIT_ASSERT_ARG(git_tree_owner(tree) == repo);
 
 	return git_commit__create_internal(
 		id, repo, update_ref, author, committer,
@@ -337,7 +340,8 @@ int git_commit_amend(
 	git_reference *ref;
 	int error;
 
-	assert(id && commit_to_amend);
+	GIT_ASSERT_ARG(id);
+	GIT_ASSERT_ARG(commit_to_amend);
 
 	repo = git_commit_owner(commit_to_amend);
 
@@ -356,7 +360,7 @@ int git_commit_amend(
 		git_oid_cpy(&tree_id, git_tree_id(old_tree));
 		git_tree_free(old_tree);
 	} else {
-		assert(git_tree_owner(tree) == repo);
+		GIT_ASSERT_ARG(git_tree_owner(tree) == repo);
 		git_oid_cpy(&tree_id, git_tree_id(tree));
 	}
 
@@ -392,7 +396,8 @@ static int commit_parse(git_commit *commit, const char *data, size_t size, unsig
 	size_t header_len;
 	git_signature dummy_sig;
 
-	assert(commit && data);
+	GIT_ASSERT_ARG(commit);
+	GIT_ASSERT_ARG(data);
 
 	buffer = buffer_start;
 
@@ -506,28 +511,28 @@ int git_commit__parse(void *_commit, git_odb_object *odb_obj)
 	return git_commit__parse_ext(_commit, odb_obj, 0);
 }
 
-#define GIT_COMMIT_GETTER(_rvalue, _name, _return) \
+#define GIT_COMMIT_GETTER(_rvalue, _name, _return, _invalid) \
 	_rvalue git_commit_##_name(const git_commit *commit) \
 	{\
-		assert(commit); \
+		GIT_ASSERT_ARG_WITH_RETVAL(commit, _invalid); \
 		return _return; \
 	}
 
-GIT_COMMIT_GETTER(const git_signature *, author, commit->author)
-GIT_COMMIT_GETTER(const git_signature *, committer, commit->committer)
-GIT_COMMIT_GETTER(const char *, message_raw, commit->raw_message)
-GIT_COMMIT_GETTER(const char *, message_encoding, commit->message_encoding)
-GIT_COMMIT_GETTER(const char *, raw_header, commit->raw_header)
-GIT_COMMIT_GETTER(git_time_t, time, commit->committer->when.time)
-GIT_COMMIT_GETTER(int, time_offset, commit->committer->when.offset)
-GIT_COMMIT_GETTER(unsigned int, parentcount, (unsigned int)git_array_size(commit->parent_ids))
-GIT_COMMIT_GETTER(const git_oid *, tree_id, &commit->tree_id)
+GIT_COMMIT_GETTER(const git_signature *, author, commit->author, NULL)
+GIT_COMMIT_GETTER(const git_signature *, committer, commit->committer, NULL)
+GIT_COMMIT_GETTER(const char *, message_raw, commit->raw_message, NULL)
+GIT_COMMIT_GETTER(const char *, message_encoding, commit->message_encoding, NULL)
+GIT_COMMIT_GETTER(const char *, raw_header, commit->raw_header, NULL)
+GIT_COMMIT_GETTER(git_time_t, time, commit->committer->when.time, INT64_MIN)
+GIT_COMMIT_GETTER(int, time_offset, commit->committer->when.offset, -1)
+GIT_COMMIT_GETTER(unsigned int, parentcount, (unsigned int)git_array_size(commit->parent_ids), 0)
+GIT_COMMIT_GETTER(const git_oid *, tree_id, &commit->tree_id, NULL)
 
 const char *git_commit_message(const git_commit *commit)
 {
 	const char *message;
 
-	assert(commit);
+	GIT_ASSERT_ARG_WITH_RETVAL(commit, NULL);
 
 	message = commit->raw_message;
 
@@ -544,7 +549,7 @@ const char *git_commit_summary(git_commit *commit)
 	const char *msg, *space;
 	bool space_contains_newline = false;
 
-	assert(commit);
+	GIT_ASSERT_ARG_WITH_RETVAL(commit, NULL);
 
 	if (!commit->summary) {
 		for (msg = git_commit_message(commit), space = NULL; *msg; ++msg) {
@@ -587,7 +592,7 @@ const char *git_commit_body(git_commit *commit)
 {
 	const char *msg, *end;
 
-	assert(commit);
+	GIT_ASSERT_ARG_WITH_RETVAL(commit, NULL);
 
 	if (!commit->body) {
 		/* search for end of summary */
@@ -612,14 +617,14 @@ const char *git_commit_body(git_commit *commit)
 
 int git_commit_tree(git_tree **tree_out, const git_commit *commit)
 {
-	assert(commit);
+	GIT_ASSERT_ARG(commit);
 	return git_tree_lookup(tree_out, commit->object.repo, &commit->tree_id);
 }
 
 const git_oid *git_commit_parent_id(
 	const git_commit *commit, unsigned int n)
 {
-	assert(commit);
+	GIT_ASSERT_ARG_WITH_RETVAL(commit, NULL);
 
 	return git_array_get(commit->parent_ids, n);
 }
@@ -628,7 +633,7 @@ int git_commit_parent(
 	git_commit **parent, const git_commit *commit, unsigned int n)
 {
 	const git_oid *parent_id;
-	assert(commit);
+	GIT_ASSERT_ARG(commit);
 
 	parent_id = git_commit_parent_id(commit, n);
 	if (parent_id == NULL) {
@@ -647,7 +652,8 @@ int git_commit_nth_gen_ancestor(
 	git_commit *current, *parent = NULL;
 	int error;
 
-	assert(ancestor && commit);
+	GIT_ASSERT_ARG(ancestor);
+	GIT_ASSERT_ARG(commit);
 
 	if (git_commit_dup(&current, (git_commit *)commit) < 0)
 		return -1;
@@ -840,7 +846,8 @@ int git_commit_create_buffer(git_buf *out,
 	git_array_oid_t parents_arr = GIT_ARRAY_INIT;
 	const git_oid *tree_id;
 
-	assert(tree && git_tree_owner(tree) == repo);
+	GIT_ASSERT_ARG(tree);
+	GIT_ASSERT_ARG(git_tree_owner(tree) == repo);
 
 	tree_id = git_tree_id(tree);
 
@@ -859,11 +866,13 @@ int git_commit_create_buffer(git_buf *out,
 /**
  * Append to 'out' properly marking continuations when there's a newline in 'content'
  */
-static void format_header_field(git_buf *out, const char *field, const char *content)
+static int format_header_field(git_buf *out, const char *field, const char *content)
 {
 	const char *lf;
 
-	assert(out && field && content);
+	GIT_ASSERT_ARG(out);
+	GIT_ASSERT_ARG(field);
+	GIT_ASSERT_ARG(content);
 
 	git_buf_puts(out, field);
 	git_buf_putc(out, ' ');
@@ -876,6 +885,8 @@ static void format_header_field(git_buf *out, const char *field, const char *con
 
 	git_buf_puts(out, content);
 	git_buf_putc(out, '\n');
+
+	return git_buf_oom(out) ? -1 : 0;
 }
 
 static const git_oid *commit_parent_from_commit(size_t n, void *payload)
@@ -926,7 +937,9 @@ int git_commit_create_with_signature(
 
 	if (signature != NULL) {
 		field = signature_field ? signature_field : "gpgsig";
-		format_header_field(&commit, field, signature);
+
+		if ((error = format_header_field(&commit, field, signature)) < 0)
+			goto cleanup;
 	}
 
 	git_buf_puts(&commit, header_end);