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))

refs.c

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ static void get_ref_dir(const char *submodule, const char *base,
334334
flag |= REF_ISBROKEN;
335335
}
336336
} else
337-
if (!resolve_ref(ref, sha1, 1, &flag)) {
337+
if (read_ref_full(ref, sha1, 1, &flag)) {
338338
hashclr(sha1);
339339
flag |= REF_ISBROKEN;
340340
}
@@ -612,13 +612,18 @@ struct ref_filter {
612612
void *cb_data;
613613
};
614614

615-
int read_ref(const char *ref, unsigned char *sha1)
615+
int read_ref_full(const char *ref, unsigned char *sha1, int reading, int *flags)
616616
{
617-
if (resolve_ref(ref, sha1, 1, NULL))
617+
if (resolve_ref(ref, sha1, reading, flags))
618618
return 0;
619619
return -1;
620620
}
621621

622+
int read_ref(const char *ref, unsigned char *sha1)
623+
{
624+
return read_ref_full(ref, sha1, 1, NULL);
625+
}
626+
622627
#define DO_FOR_EACH_INCLUDE_BROKEN 01
623628
static int do_one_ref(const char *base, each_ref_fn fn, int trim,
624629
int flags, void *cb_data, struct ref_entry *entry)
@@ -663,7 +668,7 @@ int peel_ref(const char *ref, unsigned char *sha1)
663668
goto fallback;
664669
}
665670

666-
if (!resolve_ref(ref, base, 1, &flag))
671+
if (read_ref_full(ref, base, 1, &flag))
667672
return -1;
668673

669674
if ((flag & REF_ISPACKED)) {
@@ -746,7 +751,7 @@ static int do_head_ref(const char *submodule, each_ref_fn fn, void *cb_data)
746751
return 0;
747752
}
748753

749-
if (resolve_ref("HEAD", sha1, 1, &flag))
754+
if (!read_ref_full("HEAD", sha1, 1, &flag))
750755
return fn("HEAD", sha1, flag, cb_data);
751756

752757
return 0;
@@ -826,7 +831,7 @@ int head_ref_namespaced(each_ref_fn fn, void *cb_data)
826831
int flag;
827832

828833
strbuf_addf(&buf, "%sHEAD", get_git_namespace());
829-
if (resolve_ref(buf.buf, sha1, 1, &flag))
834+
if (!read_ref_full(buf.buf, sha1, 1, &flag))
830835
ret = fn(buf.buf, sha1, flag, cb_data);
831836
strbuf_release(&buf);
832837

@@ -1022,7 +1027,7 @@ int refname_match(const char *abbrev_name, const char *full_name, const char **r
10221027
static struct ref_lock *verify_lock(struct ref_lock *lock,
10231028
const unsigned char *old_sha1, int mustexist)
10241029
{
1025-
if (!resolve_ref(lock->ref_name, lock->old_sha1, mustexist, NULL)) {
1030+
if (read_ref_full(lock->ref_name, lock->old_sha1, mustexist, NULL)) {
10261031
error("Can't verify ref %s", lock->ref_name);
10271032
unlock_ref(lock);
10281033
return NULL;
@@ -1377,7 +1382,8 @@ int rename_ref(const char *oldref, const char *newref, const char *logmsg)
13771382
goto rollback;
13781383
}
13791384

1380-
if (resolve_ref(newref, sha1, 1, &flag) && delete_ref(newref, sha1, REF_NODEREF)) {
1385+
if (!read_ref_full(newref, sha1, 1, &flag) &&
1386+
delete_ref(newref, sha1, REF_NODEREF)) {
13811387
if (errno==EISDIR) {
13821388
if (remove_empty_directories(git_path("%s", newref))) {
13831389
error("Directory not empty: %s", newref);
@@ -1929,7 +1935,7 @@ static int do_for_each_reflog(const char *base, each_ref_fn fn, void *cb_data)
19291935
retval = do_for_each_reflog(log, fn, cb_data);
19301936
} else {
19311937
unsigned char sha1[20];
1932-
if (!resolve_ref(log, sha1, 0, NULL))
1938+
if (read_ref_full(log, sha1, 0, NULL))
19331939
retval = error("bad ref for %s", log);
19341940
else
19351941
retval = fn(log, sha1, 0, cb_data);
@@ -2072,7 +2078,6 @@ char *shorten_unambiguous_ref(const char *ref, int strict)
20722078
*/
20732079
for (j = 0; j < rules_to_fail; j++) {
20742080
const char *rule = ref_rev_parse_rules[j];
2075-
unsigned char short_objectname[20];
20762081
char refname[PATH_MAX];
20772082

20782083
/* skip matched rule */
@@ -2086,7 +2091,7 @@ char *shorten_unambiguous_ref(const char *ref, int strict)
20862091
*/
20872092
mksnpath(refname, sizeof(refname),
20882093
rule, short_name_len, short_name);
2089-
if (!read_ref(refname, short_objectname))
2094+
if (ref_exists(refname))
20902095
break;
20912096
}
20922097

remote.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,13 +1507,13 @@ int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs)
15071507
* nothing to report.
15081508
*/
15091509
base = branch->merge[0]->dst;
1510-
if (!resolve_ref(base, sha1, 1, NULL))
1510+
if (read_ref(base, sha1))
15111511
return 0;
15121512
theirs = lookup_commit_reference(sha1);
15131513
if (!theirs)
15141514
return 0;
15151515

1516-
if (!resolve_ref(branch->refname, sha1, 1, NULL))
1516+
if (read_ref(branch->refname, sha1))
15171517
return 0;
15181518
ours = lookup_commit_reference(sha1);
15191519
if (!ours)

0 commit comments

Comments
 (0)