Skip to content

Commit 8cad474

Browse files
pcloudsgitster
authored andcommitted
Rename resolve_ref() to resolve_ref_unsafe()
resolve_ref() may return a pointer to a shared buffer and can be overwritten by the next resolve_ref() calls. Callers need to pay attention, not to keep the pointer when the next call happens. Rename with "_unsafe" suffix to warn developers (or reviewers) before introducing new call sites. This patch is generated using the following command git grep -l 'resolve_ref(' -- '*.[ch]'|xargs sed -i 's/resolve_ref(/resolve_ref_unsafe(/g' Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 96ec7b1 commit 8cad474

File tree

13 files changed

+26
-26
lines changed

13 files changed

+26
-26
lines changed

branch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ int validate_new_branchname(const char *name, struct strbuf *ref,
182182
const char *head;
183183
unsigned char sha1[20];
184184

185-
head = resolve_ref("HEAD", sha1, 0, NULL);
185+
head = resolve_ref_unsafe("HEAD", sha1, 0, NULL);
186186
if (!is_bare_repository() && head && !strcmp(head, ref->buf))
187187
die("Cannot force update the current branch.");
188188
}

builtin/branch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ static char *resolve_symref(const char *src, const char *prefix)
252252
int flag;
253253
const char *dst, *cp;
254254

255-
dst = resolve_ref(src, sha1, 0, &flag);
255+
dst = resolve_ref_unsafe(src, sha1, 0, &flag);
256256
if (!(dst && (flag & REF_ISSYMREF)))
257257
return NULL;
258258
if (prefix && (cp = skip_prefix(dst, prefix)))

builtin/commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1304,7 +1304,7 @@ static void print_summary(const char *prefix, const unsigned char *sha1,
13041304
rev.diffopt.break_opt = 0;
13051305
diff_setup_done(&rev.diffopt);
13061306

1307-
head = resolve_ref("HEAD", junk_sha1, 0, NULL);
1307+
head = resolve_ref_unsafe("HEAD", junk_sha1, 0, NULL);
13081308
printf("[%s%s ",
13091309
!prefixcmp(head, "refs/heads/") ?
13101310
head + 11 :

builtin/fsck.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ static int fsck_head_link(void)
563563
if (verbose)
564564
fprintf(stderr, "Checking HEAD link\n");
565565

566-
head_points_at = resolve_ref("HEAD", head_sha1, 0, &flag);
566+
head_points_at = resolve_ref_unsafe("HEAD", head_sha1, 0, &flag);
567567
if (!head_points_at)
568568
return error("Invalid HEAD");
569569
if (!strcmp(head_points_at, "HEAD"))

builtin/log.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,7 @@ static char *find_branch_name(struct rev_info *rev)
10401040
if (positive < 0)
10411041
return NULL;
10421042
strbuf_addf(&buf, "refs/heads/%s", rev->cmdline.rev[positive].name);
1043-
branch = resolve_ref(buf.buf, branch_sha1, 1, NULL);
1043+
branch = resolve_ref_unsafe(buf.buf, branch_sha1, 1, NULL);
10441044
if (!branch ||
10451045
prefixcmp(branch, "refs/heads/") ||
10461046
hashcmp(rev->cmdline.rev[positive].item->sha1, branch_sha1))
@@ -1268,7 +1268,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
12681268

12691269
rev.pending.objects[0].item->flags |= UNINTERESTING;
12701270
add_head_to_pending(&rev);
1271-
ref = resolve_ref("HEAD", sha1, 1, NULL);
1271+
ref = resolve_ref_unsafe("HEAD", sha1, 1, NULL);
12721272
if (ref && !prefixcmp(ref, "refs/heads/"))
12731273
branch_name = xstrdup(ref + strlen("refs/heads/"));
12741274
else

builtin/receive-pack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ static void check_aliased_update(struct command *cmd, struct string_list *list)
572572
int flag;
573573

574574
strbuf_addf(&buf, "%s%s", get_git_namespace(), cmd->ref_name);
575-
dst_name = resolve_ref(buf.buf, sha1, 0, &flag);
575+
dst_name = resolve_ref_unsafe(buf.buf, sha1, 0, &flag);
576576
strbuf_release(&buf);
577577

578578
if (!(flag & REF_ISSYMREF))

builtin/remote.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ static int read_remote_branches(const char *refname,
573573
strbuf_addf(&buf, "refs/remotes/%s/", rename->old);
574574
if (!prefixcmp(refname, buf.buf)) {
575575
item = string_list_append(rename->remote_branches, xstrdup(refname));
576-
symref = resolve_ref(refname, orig_sha1, 1, &flag);
576+
symref = resolve_ref_unsafe(refname, orig_sha1, 1, &flag);
577577
if (flag & REF_ISSYMREF)
578578
item->util = xstrdup(symref);
579579
else

builtin/show-branch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
789789
}
790790
}
791791

792-
head_p = resolve_ref("HEAD", head_sha1, 1, NULL);
792+
head_p = resolve_ref_unsafe("HEAD", head_sha1, 1, NULL);
793793
if (head_p) {
794794
head_len = strlen(head_p);
795795
memcpy(head, head_p, head_len + 1);

builtin/symbolic-ref.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ static void check_symref(const char *HEAD, int quiet)
1212
{
1313
unsigned char sha1[20];
1414
int flag;
15-
const char *refs_heads_master = resolve_ref(HEAD, sha1, 0, &flag);
15+
const char *refs_heads_master = resolve_ref_unsafe(HEAD, sha1, 0, &flag);
1616

1717
if (!refs_heads_master)
1818
die("No such ref: %s", HEAD);

cache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -865,7 +865,7 @@ extern int read_ref(const char *filename, unsigned char *sha1);
865865
*
866866
* errno is sometimes set on errors, but not always.
867867
*/
868-
extern const char *resolve_ref(const char *ref, unsigned char *sha1, int reading, int *flag);
868+
extern const char *resolve_ref_unsafe(const char *ref, unsigned char *sha1, int reading, int *flag);
869869
extern char *resolve_refdup(const char *ref, unsigned char *sha1, int reading, int *flag);
870870

871871
extern int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref);

0 commit comments

Comments
 (0)