commit: use GIT_ASSERT
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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
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(¤t, (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);