Commit 29be3a6d9eee68b3964f34c498e64ce32452a57f

Arthur Schreiber 2014-01-14T21:33:35

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.

diff --git a/include/git2/signature.h b/include/git2/signature.h
index 2fa46d0..a1dd1ec 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 b885de2..0f2d906 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 9b2b201..8e41621 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 ec51a42..f658d60 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)