Skip to content

Commit c689332

Browse files
pcloudsgitster
authored andcommitted
Convert many resolve_ref() calls to read_ref*() and ref_exists()
resolve_ref() may return a pointer to a static buffer, which is not safe for long-term use because if another resolve_ref() call happens, the buffer may be changed. Many call sites though do not care about this buffer. They simply check if the return value is NULL or not. Convert all these call sites to new wrappers to reduce resolve_ref() calls from 57 to 34. If we change resolve_ref() prototype later on to avoid passing static buffer out, this helps reduce changes. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bc1bbe0 commit c689332

File tree

12 files changed

+36
-32
lines changed

12 files changed

+36
-32
lines changed

builtin/branch.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ static int delete_branches(int argc, const char **argv, int force, int kinds)
186186
free(name);
187187

188188
name = xstrdup(mkpath(fmt, bname.buf));
189-
if (!resolve_ref(name, sha1, 1, NULL)) {
189+
if (read_ref(name, sha1)) {
190190
error(_("%sbranch '%s' not found."),
191191
remote, bname.buf);
192192
ret = 1;
@@ -565,7 +565,6 @@ static int print_ref_list(int kinds, int detached, int verbose, int abbrev, stru
565565
static void rename_branch(const char *oldname, const char *newname, int force)
566566
{
567567
struct strbuf oldref = STRBUF_INIT, newref = STRBUF_INIT, logmsg = STRBUF_INIT;
568-
unsigned char sha1[20];
569568
struct strbuf oldsection = STRBUF_INIT, newsection = STRBUF_INIT;
570569
int recovery = 0;
571570

@@ -577,7 +576,7 @@ static void rename_branch(const char *oldname, const char *newname, int force)
577576
* Bad name --- this could be an attempt to rename a
578577
* ref that we used to allow to be created by accident.
579578
*/
580-
if (resolve_ref(oldref.buf, sha1, 1, NULL))
579+
if (ref_exists(oldref.buf))
581580
recovery = 1;
582581
else
583582
die(_("Invalid branch name: '%s'"), oldname);

builtin/checkout.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ static int checkout_paths(struct tree *source_tree, const char **pathspec,
288288
commit_locked_index(lock_file))
289289
die(_("unable to write new index file"));
290290

291-
resolve_ref("HEAD", rev, 0, &flag);
291+
read_ref_full("HEAD", rev, 0, &flag);
292292
head = lookup_commit_reference_gently(rev, 1);
293293

294294
errs |= post_checkout_hook(head, head, 0);
@@ -866,7 +866,7 @@ static int parse_branchname_arg(int argc, const char **argv,
866866
setup_branch_path(new);
867867

868868
if (!check_refname_format(new->path, 0) &&
869-
resolve_ref(new->path, branch_rev, 1, NULL))
869+
!read_ref(new->path, branch_rev))
870870
hashcpy(rev, branch_rev);
871871
else
872872
new->path = NULL; /* not an existing branch */

builtin/merge.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ static struct object *want_commit(const char *name)
420420
static void merge_name(const char *remote, struct strbuf *msg)
421421
{
422422
struct object *remote_head;
423-
unsigned char branch_head[20], buf_sha[20];
423+
unsigned char branch_head[20];
424424
struct strbuf buf = STRBUF_INIT;
425425
struct strbuf bname = STRBUF_INIT;
426426
const char *ptr;
@@ -479,7 +479,7 @@ static void merge_name(const char *remote, struct strbuf *msg)
479479
strbuf_addstr(&truname, "refs/heads/");
480480
strbuf_addstr(&truname, remote);
481481
strbuf_setlen(&truname, truname.len - len);
482-
if (resolve_ref(truname.buf, buf_sha, 1, NULL)) {
482+
if (ref_exists(truname.buf)) {
483483
strbuf_addf(msg,
484484
"%s\t\tbranch '%s'%s of .\n",
485485
sha1_to_hex(remote_head->sha1),

builtin/remote.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,7 @@ static int get_ref_states(const struct ref *remote_refs, struct ref_states *stat
343343
states->tracked.strdup_strings = 1;
344344
states->stale.strdup_strings = 1;
345345
for (ref = fetch_map; ref; ref = ref->next) {
346-
unsigned char sha1[20];
347-
if (!ref->peer_ref || read_ref(ref->peer_ref->name, sha1))
346+
if (!ref->peer_ref || !ref_exists(ref->peer_ref->name))
348347
string_list_append(&states->new, abbrev_branch(ref->name));
349348
else
350349
string_list_append(&states->tracked, abbrev_branch(ref->name));
@@ -710,7 +709,7 @@ static int mv(int argc, const char **argv)
710709
int flag = 0;
711710
unsigned char sha1[20];
712711

713-
resolve_ref(item->string, sha1, 1, &flag);
712+
read_ref_full(item->string, sha1, 1, &flag);
714713
if (!(flag & REF_ISSYMREF))
715714
continue;
716715
if (delete_ref(item->string, NULL, REF_NODEREF))
@@ -1220,10 +1219,9 @@ static int set_head(int argc, const char **argv)
12201219
usage_with_options(builtin_remote_sethead_usage, options);
12211220

12221221
if (head_name) {
1223-
unsigned char sha1[20];
12241222
strbuf_addf(&buf2, "refs/remotes/%s/%s", argv[0], head_name);
12251223
/* make sure it's valid */
1226-
if (!resolve_ref(buf2.buf, sha1, 1, NULL))
1224+
if (!ref_exists(buf2.buf))
12271225
result |= error("Not a valid ref: %s", buf2.buf);
12281226
else if (create_symref(buf.buf, buf2.buf, "remote set-head"))
12291227
result |= error("Could not setup %s", buf.buf);

builtin/replace.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ static int for_each_replace_name(const char **argv, each_replace_name_fn fn)
5858
had_error = 1;
5959
continue;
6060
}
61-
if (!resolve_ref(ref, sha1, 1, NULL)) {
61+
if (read_ref(ref, sha1)) {
6262
error("replace ref '%s' not found.", *p);
6363
had_error = 1;
6464
continue;
@@ -97,7 +97,7 @@ static int replace_object(const char *object_ref, const char *replace_ref,
9797
if (check_refname_format(ref, 0))
9898
die("'%s' is not a valid ref name.", ref);
9999

100-
if (!resolve_ref(ref, prev, 1, NULL))
100+
if (read_ref(ref, prev))
101101
hashclr(prev);
102102
else if (!force)
103103
die("replace ref '%s' already exists", ref);

builtin/show-ref.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ int cmd_show_ref(int argc, const char **argv, const char *prefix)
225225
unsigned char sha1[20];
226226

227227
if (!prefixcmp(*pattern, "refs/") &&
228-
resolve_ref(*pattern, sha1, 1, NULL)) {
228+
!read_ref(*pattern, sha1)) {
229229
if (!quiet)
230230
show_one(*pattern, sha1);
231231
}

builtin/tag.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ static int for_each_tag_name(const char **argv, each_tag_name_fn fn)
174174
had_error = 1;
175175
continue;
176176
}
177-
if (!resolve_ref(ref, sha1, 1, NULL)) {
177+
if (read_ref(ref, sha1)) {
178178
error(_("tag '%s' not found."), *p);
179179
had_error = 1;
180180
continue;
@@ -518,7 +518,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
518518
if (strbuf_check_tag_ref(&ref, tag))
519519
die(_("'%s' is not a valid tag name."), tag);
520520

521-
if (!resolve_ref(ref.buf, prev, 1, NULL))
521+
if (read_ref(ref.buf, prev))
522522
hashclr(prev);
523523
else if (!force)
524524
die(_("tag '%s' already exists"), tag);

bundle.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ int create_bundle(struct bundle_header *header, const char *path,
320320
continue;
321321
if (dwim_ref(e->name, strlen(e->name), sha1, &ref) != 1)
322322
continue;
323-
if (!resolve_ref(e->name, sha1, 1, &flag))
323+
if (read_ref_full(e->name, sha1, 1, &flag))
324324
flag = 0;
325325
display_ref = (flag & REF_ISSYMREF) ? e->name : ref;
326326

cache.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,8 @@ static inline int get_sha1_with_context(const char *str, unsigned char *sha1, st
832832
extern int get_sha1_hex(const char *hex, unsigned char *sha1);
833833

834834
extern char *sha1_to_hex(const unsigned char *sha1); /* static buffer result! */
835+
extern int read_ref_full(const char *filename, unsigned char *sha1,
836+
int reading, int *flags);
835837
extern int read_ref(const char *filename, unsigned char *sha1);
836838

837839
/*

notes-merge.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ int notes_merge(struct notes_merge_options *o,
568568
o->local_ref, o->remote_ref);
569569

570570
/* Dereference o->local_ref into local_sha1 */
571-
if (!resolve_ref(o->local_ref, local_sha1, 0, NULL))
571+
if (read_ref_full(o->local_ref, local_sha1, 0, NULL))
572572
die("Failed to resolve local notes ref '%s'", o->local_ref);
573573
else if (!check_refname_format(o->local_ref, 0) &&
574574
is_null_sha1(local_sha1))

0 commit comments

Comments
 (0)