summaryrefslogtreecommitdiff
path: root/src/notes.c
diff options
context:
space:
mode:
authorCarlos Martín Nieto <cmn@dwim.me>2015-03-04 01:23:20 +0100
committerCarlos Martín Nieto <cmn@dwim.me>2015-03-17 20:50:02 +0100
commit385449b1df593440abcf6636c81eb5ea19b1d1a1 (patch)
tree71e4417be6058d6df44e4e105f4c60b56d889359 /src/notes.c
parent9bbc8f350b80a5a6e94651ec667cf9e5d545b317 (diff)
downloadlibgit2-385449b1df593440abcf6636c81eb5ea19b1d1a1.tar.gz
note: use a git_buf to return the default namespacecmn/notes-buf
The caller has otherwise no way to know how long the string will be allocated or ability to free it. This fixes #2944.
Diffstat (limited to 'src/notes.c')
-rw-r--r--src/notes.c61
1 files changed, 40 insertions, 21 deletions
diff --git a/src/notes.c b/src/notes.c
index 1850e81c7..ef4b41b31 100644
--- a/src/notes.c
+++ b/src/notes.c
@@ -388,7 +388,7 @@ cleanup:
return error;
}
-static int note_get_default_ref(const char **out, git_repository *repo)
+static int note_get_default_ref(char **out, git_repository *repo)
{
git_config *cfg;
int ret = git_repository_config__weakptr(&cfg, repo);
@@ -399,27 +399,31 @@ static int note_get_default_ref(const char **out, git_repository *repo)
return ret;
}
-static int normalize_namespace(const char **notes_ref, git_repository *repo)
+static int normalize_namespace(char **out, git_repository *repo, const char *notes_ref)
{
- if (*notes_ref)
+ if (notes_ref) {
+ *out = git__strdup(notes_ref);
+ GITERR_CHECK_ALLOC(*out);
return 0;
+ }
- return note_get_default_ref(notes_ref, repo);
+ return note_get_default_ref(out, repo);
}
static int retrieve_note_tree_and_commit(
git_tree **tree_out,
git_commit **commit_out,
+ char **notes_ref_out,
git_repository *repo,
- const char **notes_ref)
+ const char *notes_ref)
{
int error;
git_oid oid;
- if ((error = normalize_namespace(notes_ref, repo)) < 0)
+ if ((error = normalize_namespace(notes_ref_out, repo, notes_ref)) < 0)
return error;
- if ((error = git_reference_name_to_id(&oid, repo, *notes_ref)) < 0)
+ if ((error = git_reference_name_to_id(&oid, repo, *notes_ref_out)) < 0)
return error;
if (git_commit_lookup(commit_out, repo, &oid) < 0)
@@ -432,10 +436,10 @@ static int retrieve_note_tree_and_commit(
}
int git_note_read(git_note **out, git_repository *repo,
- const char *notes_ref, const git_oid *oid)
+ const char *notes_ref_in, const git_oid *oid)
{
int error;
- char *target = NULL;
+ char *target = NULL, *notes_ref = NULL;
git_tree *tree = NULL;
git_commit *commit = NULL;
@@ -443,9 +447,10 @@ int git_note_read(git_note **out, git_repository *repo,
GITERR_CHECK_ALLOC(target);
if (!(error = retrieve_note_tree_and_commit(
- &tree, &commit, repo, &notes_ref)))
+ &tree, &commit, &notes_ref, repo, notes_ref_in)))
error = note_lookup(out, repo, commit, tree, target);
+ git__free(notes_ref);
git__free(target);
git_tree_free(tree);
git_commit_free(commit);
@@ -455,7 +460,7 @@ int git_note_read(git_note **out, git_repository *repo,
int git_note_create(
git_oid *out,
git_repository *repo,
- const char *notes_ref,
+ const char *notes_ref_in,
const git_signature *author,
const git_signature *committer,
const git_oid *oid,
@@ -463,14 +468,14 @@ int git_note_create(
int allow_note_overwrite)
{
int error;
- char *target = NULL;
+ char *target = NULL, *notes_ref = NULL;
git_commit *commit = NULL;
git_tree *tree = NULL;
target = git_oid_allocfmt(oid);
GITERR_CHECK_ALLOC(target);
- error = retrieve_note_tree_and_commit(&tree, &commit, repo, &notes_ref);
+ error = retrieve_note_tree_and_commit(&tree, &commit, &notes_ref, repo, notes_ref_in);
if (error < 0 && error != GIT_ENOTFOUND)
goto cleanup;
@@ -479,18 +484,19 @@ int git_note_create(
note, tree, target, &commit, allow_note_overwrite);
cleanup:
+ git__free(notes_ref);
git__free(target);
git_commit_free(commit);
git_tree_free(tree);
return error;
}
-int git_note_remove(git_repository *repo, const char *notes_ref,
+int git_note_remove(git_repository *repo, const char *notes_ref_in,
const git_signature *author, const git_signature *committer,
const git_oid *oid)
{
int error;
- char *target = NULL;
+ char *target = NULL, *notes_ref;
git_commit *commit = NULL;
git_tree *tree = NULL;
@@ -498,20 +504,31 @@ int git_note_remove(git_repository *repo, const char *notes_ref,
GITERR_CHECK_ALLOC(target);
if (!(error = retrieve_note_tree_and_commit(
- &tree, &commit, repo, &notes_ref)))
+ &tree, &commit, &notes_ref, repo, notes_ref_in)))
error = note_remove(
repo, author, committer, notes_ref, tree, target, &commit);
+ git__free(notes_ref);
git__free(target);
git_commit_free(commit);
git_tree_free(tree);
return error;
}
-int git_note_default_ref(const char **out, git_repository *repo)
+int git_note_default_ref(git_buf *out, git_repository *repo)
{
- assert(repo);
- return note_get_default_ref(out, repo);
+ char *default_ref;
+ int error;
+
+ assert(out && repo);
+
+ git_buf_sanitize(out);
+
+ if ((error = note_get_default_ref(&default_ref, repo)) < 0)
+ return error;
+
+ git_buf_attach(out, default_ref, strlen(default_ref));
+ return 0;
}
const git_signature *git_note_committer(const git_note *note)
@@ -635,13 +652,14 @@ void git_note_iterator_free(git_note_iterator *it)
int git_note_iterator_new(
git_note_iterator **it,
git_repository *repo,
- const char *notes_ref)
+ const char *notes_ref_in)
{
int error;
git_commit *commit = NULL;
git_tree *tree = NULL;
+ char *notes_ref;
- error = retrieve_note_tree_and_commit(&tree, &commit, repo, &notes_ref);
+ error = retrieve_note_tree_and_commit(&tree, &commit, &notes_ref, repo, notes_ref_in);
if (error < 0)
goto cleanup;
@@ -649,6 +667,7 @@ int git_note_iterator_new(
git_iterator_free(*it);
cleanup:
+ git__free(notes_ref);
git_tree_free(tree);
git_commit_free(commit);