diff options
author | Edward Thomson <ethomson@edwardthomson.com> | 2021-11-29 13:36:36 -0500 |
---|---|---|
committer | Edward Thomson <ethomson@edwardthomson.com> | 2021-11-30 22:45:13 -0500 |
commit | fc1a3f456540dfc4dc143b322b943a4264658d56 (patch) | |
tree | 3404931f494be61997af4fc9fe9034884169d73f /src/commit.c | |
parent | 6fdb1b2f55da9593576b096ee2eecce61995fb51 (diff) | |
download | libgit2-fc1a3f456540dfc4dc143b322b943a4264658d56.tar.gz |
object: return GIT_EINVALID on parse errors
Return `GIT_EINVALID` on parse errors so that direct callers of parse
functions can determine when there was a failure to parse the object.
The object parser functions will swallow this error code to prevent it
from propagating down the chain to end-users. (`git_merge` should not
return `GIT_EINVALID` when a commit it tries to look up is not valid,
this would be too vague to be useful.)
The only public function that this affects is
`git_signature_from_buffer`, which is now documented as returning
`GIT_EINVALID` when appropriate.
Diffstat (limited to 'src/commit.c')
-rw-r--r-- | src/commit.c | 19 |
1 files changed, 11 insertions, 8 deletions
diff --git a/src/commit.c b/src/commit.c index 752d98b02..ceaccb331 100644 --- a/src/commit.c +++ b/src/commit.c @@ -395,6 +395,7 @@ static int commit_parse(git_commit *commit, const char *data, size_t size, unsig git_oid parent_id; size_t header_len; git_signature dummy_sig; + int error; GIT_ASSERT_ARG(commit); GIT_ASSERT_ARG(data); @@ -431,14 +432,14 @@ static int commit_parse(git_commit *commit, const char *data, size_t size, unsig commit->author = git__malloc(sizeof(git_signature)); GIT_ERROR_CHECK_ALLOC(commit->author); - if (git_signature__parse(commit->author, &buffer, buffer_end, "author ", '\n') < 0) - return -1; + if ((error = git_signature__parse(commit->author, &buffer, buffer_end, "author ", '\n')) < 0) + return error; } /* Some tools create multiple author fields, ignore the extra ones */ while (!git__prefixncmp(buffer, buffer_end - buffer, "author ")) { - if (git_signature__parse(&dummy_sig, &buffer, buffer_end, "author ", '\n') < 0) - return -1; + if ((error = git_signature__parse(&dummy_sig, &buffer, buffer_end, "author ", '\n')) < 0) + return error; git__free(dummy_sig.name); git__free(dummy_sig.email); @@ -448,8 +449,8 @@ static int commit_parse(git_commit *commit, const char *data, size_t size, unsig commit->committer = git__malloc(sizeof(git_signature)); GIT_ERROR_CHECK_ALLOC(commit->committer); - if (git_signature__parse(commit->committer, &buffer, buffer_end, "committer ", '\n') < 0) - return -1; + if ((error = git_signature__parse(commit->committer, &buffer, buffer_end, "committer ", '\n')) < 0) + return error; if (flags & GIT_COMMIT_PARSE_QUICK) return 0; @@ -493,7 +494,7 @@ static int commit_parse(git_commit *commit, const char *data, size_t size, unsig bad_buffer: git_error_set(GIT_ERROR_OBJECT, "failed to parse bad commit object"); - return -1; + return GIT_EINVALID; } int git_commit__parse_raw(void *commit, const char *data, size_t size) @@ -971,8 +972,10 @@ int git_commit_create_with_signature( /* The first step is to verify that all the tree and parents exist */ parsed = git__calloc(1, sizeof(git_commit)); GIT_ERROR_CHECK_ALLOC(parsed); - if ((error = commit_parse(parsed, commit_content, strlen(commit_content), 0)) < 0) + if (commit_parse(parsed, commit_content, strlen(commit_content), 0) < 0) { + error = -1; goto cleanup; + } if ((error = validate_tree_and_parents(&parents, repo, &parsed->tree_id, commit_parent_from_commit, parsed, NULL, true)) < 0) goto cleanup; |