From 29be3a6d9eee68b3964f34c498e64ce32452a57f Mon Sep 17 00:00:00 2001 From: Arthur Schreiber Date: Tue, 14 Jan 2014 21:33:35 +0100 Subject: Align git_signature_dup. This changes git_signature_dup to actually honor oom conditions raised by the call to git__strdup. It also aligns it with the error code return pattern used everywhere else. --- include/git2/signature.h | 7 ++++--- src/blame.c | 8 ++++---- src/reflog.c | 2 +- src/signature.c | 29 +++++++++++++++++------------ 4 files changed, 26 insertions(+), 20 deletions(-) diff --git a/include/git2/signature.h b/include/git2/signature.h index 2fa46d032..a1dd1ec7a 100644 --- a/include/git2/signature.h +++ b/include/git2/signature.h @@ -68,10 +68,11 @@ GIT_EXTERN(int) git_signature_default(git_signature **out, git_repository *repo) * * Call `git_signature_free()` to free the data. * - * @param sig signature to duplicated - * @return a copy of sig, NULL on out of memory + * @param dest pointer where to store the copy + * @param entry signature to duplicate + * @return 0 or an error code */ -GIT_EXTERN(git_signature *) git_signature_dup(const git_signature *sig); +GIT_EXTERN(int) git_signature_dup(git_signature **dest, const git_signature *sig); /** * Free an existing signature. diff --git a/src/blame.c b/src/blame.c index b885de214..0f2d906d2 100644 --- a/src/blame.c +++ b/src/blame.c @@ -76,8 +76,8 @@ static git_blame_hunk* dup_hunk(git_blame_hunk *hunk) git_oid_cpy(&newhunk->orig_commit_id, &hunk->orig_commit_id); git_oid_cpy(&newhunk->final_commit_id, &hunk->final_commit_id); newhunk->boundary = hunk->boundary; - newhunk->final_signature = git_signature_dup(hunk->final_signature); - newhunk->orig_signature = git_signature_dup(hunk->orig_signature); + git_signature_dup(&newhunk->final_signature, hunk->final_signature); + git_signature_dup(&newhunk->orig_signature, hunk->orig_signature); return newhunk; } @@ -269,8 +269,8 @@ static git_blame_hunk* hunk_from_entry(git_blame__entry *e) e->lno+1, e->num_lines, e->s_lno+1, e->suspect->path); git_oid_cpy(&h->final_commit_id, git_commit_id(e->suspect->commit)); git_oid_cpy(&h->orig_commit_id, git_commit_id(e->suspect->commit)); - h->final_signature = git_signature_dup(git_commit_author(e->suspect->commit)); - h->orig_signature = git_signature_dup(git_commit_author(e->suspect->commit)); + git_signature_dup(&h->final_signature, git_commit_author(e->suspect->commit)); + git_signature_dup(&h->orig_signature, git_commit_author(e->suspect->commit)); h->boundary = e->is_boundary ? 1 : 0; return h; } diff --git a/src/reflog.c b/src/reflog.c index 9b2b201bf..8e41621ea 100644 --- a/src/reflog.c +++ b/src/reflog.c @@ -82,7 +82,7 @@ int git_reflog_append(git_reflog *reflog, const git_oid *new_oid, const git_sign entry = git__calloc(1, sizeof(git_reflog_entry)); GITERR_CHECK_ALLOC(entry); - if ((entry->committer = git_signature_dup(committer)) == NULL) + if ((git_signature_dup(&entry->committer, committer)) < 0) goto cleanup; if (msg != NULL) { diff --git a/src/signature.c b/src/signature.c index ec51a42e9..f658d6035 100644 --- a/src/signature.c +++ b/src/signature.c @@ -82,23 +82,28 @@ int git_signature_new(git_signature **sig_out, const char *name, const char *ema return 0; } -git_signature *git_signature_dup(const git_signature *sig) +int git_signature_dup(git_signature **dest, const git_signature *source) { - git_signature *new; + git_signature *signature; - if (sig == NULL) - return NULL; + if (source == NULL) + return 0; + + signature = git__calloc(1, sizeof(git_signature)); + GITERR_CHECK_ALLOC(signature); + + signature->name = git__strdup(source->name); + GITERR_CHECK_ALLOC(signature->name); - new = git__calloc(1, sizeof(git_signature)); - if (new == NULL) - return NULL; + signature->email = git__strdup(source->email); + GITERR_CHECK_ALLOC(signature->email); - new->name = git__strdup(sig->name); - new->email = git__strdup(sig->email); - new->when.time = sig->when.time; - new->when.offset = sig->when.offset; + signature->when.time = source->when.time; + signature->when.offset = source->when.offset; - return new; + *dest = signature; + + return 0; } int git_signature_now(git_signature **sig_out, const char *name, const char *email) -- cgit v1.2.1 From 529f342aba858e81f370aca9570fa149ebba674b Mon Sep 17 00:00:00 2001 From: Arthur Schreiber Date: Tue, 14 Jan 2014 21:33:59 +0100 Subject: Align git_tree_entry_dup. --- include/git2/tree.h | 7 ++++--- src/tree.c | 17 ++++++++--------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/include/git2/tree.h b/include/git2/tree.h index 422365674..6350ada9b 100644 --- a/include/git2/tree.h +++ b/include/git2/tree.h @@ -150,10 +150,11 @@ GIT_EXTERN(int) git_tree_entry_bypath( * Create a copy of a tree entry. The returned copy is owned by the user, * and must be freed explicitly with `git_tree_entry_free()`. * - * @param entry A tree entry to duplicate - * @return a copy of the original entry or NULL on error (alloc failure) + * @param dest pointer where to store the copy + * @param entry tree entry to duplicate + * @return 0 or an error code */ -GIT_EXTERN(git_tree_entry *) git_tree_entry_dup(const git_tree_entry *entry); +GIT_EXTERN(int) git_tree_entry_dup(git_tree_entry **dest, const git_tree_entry *source); /** * Free a user-owned tree entry diff --git a/src/tree.c b/src/tree.c index 4d77ff778..fc105ed5e 100644 --- a/src/tree.c +++ b/src/tree.c @@ -204,22 +204,22 @@ void git_tree_entry_free(git_tree_entry *entry) git__free(entry); } -git_tree_entry *git_tree_entry_dup(const git_tree_entry *entry) +int git_tree_entry_dup(git_tree_entry **dest, const git_tree_entry *source) { size_t total_size; git_tree_entry *copy; - assert(entry); + assert(source); - total_size = sizeof(git_tree_entry) + entry->filename_len + 1; + total_size = sizeof(git_tree_entry) + source->filename_len + 1; copy = git__malloc(total_size); - if (!copy) - return NULL; + GITERR_CHECK_ALLOC(copy); - memcpy(copy, entry, total_size); + memcpy(copy, source, total_size); - return copy; + *dest = copy; + return 0; } void git_tree__free(void *_tree) @@ -853,8 +853,7 @@ int git_tree_entry_bypath( case '\0': /* If there are no more components in the path, return * this entry */ - *entry_out = git_tree_entry_dup(entry); - return 0; + return git_tree_entry_dup(entry_out, entry); } if (git_tree_lookup(&subtree, root->object.repo, &entry->oid) < 0) -- cgit v1.2.1