Skip to content

Commit 737922a

Browse files
kjkgitster
authored andcommitted
alloc_ref_from_str(): factor out a common pattern of alloc_ref from string
Also fix an underallocation in walker.c::interpret_target(). Signed-off-by: Krzysztof Kowalczyk <kkowalczyk@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 1f8115b commit 737922a

File tree

6 files changed

+19
-27
lines changed

6 files changed

+19
-27
lines changed

builtin-fetch.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -508,10 +508,8 @@ static void find_non_local_tags(struct transport *transport,
508508
will_fetch(head, ref->old_sha1))) {
509509
path_list_insert(ref_name, &new_refs);
510510

511-
rm = alloc_ref(strlen(ref_name) + 1);
512-
strcpy(rm->name, ref_name);
513-
rm->peer_ref = alloc_ref(strlen(ref_name) + 1);
514-
strcpy(rm->peer_ref->name, ref_name);
511+
rm = alloc_ref_from_str(ref_name);
512+
rm->peer_ref = alloc_ref_from_str(ref_name);
515513
hashcpy(rm->old_sha1, ref_sha1);
516514

517515
**tail = rm;

http-push.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1761,8 +1761,7 @@ static void one_remote_ref(char *refname)
17611761
struct ref *ref;
17621762
struct object *obj;
17631763

1764-
ref = alloc_ref(strlen(refname) + 1);
1765-
strcpy(ref->name, refname);
1764+
ref = alloc_ref_from_str(refname);
17661765

17671766
if (http_fetch_ref(remote->url, ref) != 0) {
17681767
fprintf(stderr,
@@ -1894,8 +1893,7 @@ static void add_remote_info_ref(struct remote_ls_ctx *ls)
18941893
char *ref_info;
18951894
struct ref *ref;
18961895

1897-
ref = alloc_ref(strlen(ls->dentry_name) + 1);
1898-
strcpy(ref->name, ls->dentry_name);
1896+
ref = alloc_ref_from_str(ls->dentry_name);
18991897

19001898
if (http_fetch_ref(remote->url, ref) != 0) {
19011899
fprintf(stderr,

remote.c

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,13 @@ struct ref *alloc_ref(unsigned namelen)
691691
return ret;
692692
}
693693

694+
struct ref *alloc_ref_from_str(const char* str)
695+
{
696+
struct ref *ret = alloc_ref(strlen(str) + 1);
697+
strcpy(ret->name, str);
698+
return ret;
699+
}
700+
694701
static struct ref *copy_ref(const struct ref *ref)
695702
{
696703
struct ref *ret = xmalloc(sizeof(struct ref) + strlen(ref->name) + 1);
@@ -797,7 +804,6 @@ static struct ref *try_explicit_object_name(const char *name)
797804
{
798805
unsigned char sha1[20];
799806
struct ref *ref;
800-
int len;
801807

802808
if (!*name) {
803809
ref = alloc_ref(20);
@@ -807,21 +813,14 @@ static struct ref *try_explicit_object_name(const char *name)
807813
}
808814
if (get_sha1(name, sha1))
809815
return NULL;
810-
len = strlen(name) + 1;
811-
ref = alloc_ref(len);
812-
memcpy(ref->name, name, len);
816+
ref = alloc_ref_from_str(name);
813817
hashcpy(ref->new_sha1, sha1);
814818
return ref;
815819
}
816820

817821
static struct ref *make_linked_ref(const char *name, struct ref ***tail)
818822
{
819-
struct ref *ret;
820-
size_t len;
821-
822-
len = strlen(name) + 1;
823-
ret = alloc_ref(len);
824-
memcpy(ret->name, name, len);
823+
struct ref *ret = alloc_ref_from_str(name);
825824
tail_link_ref(ret, tail);
826825
return ret;
827826
}
@@ -1125,9 +1124,7 @@ static struct ref *get_local_ref(const char *name)
11251124
return NULL;
11261125

11271126
if (!prefixcmp(name, "refs/")) {
1128-
ret = alloc_ref(strlen(name) + 1);
1129-
strcpy(ret->name, name);
1130-
return ret;
1127+
return alloc_ref_from_str(name);
11311128
}
11321129

11331130
if (!prefixcmp(name, "heads/") ||

remote.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ struct refspec {
5454

5555
struct ref *alloc_ref(unsigned namelen);
5656

57+
struct ref *alloc_ref_from_str(const char* str);
58+
5759
struct ref *copy_ref_list(const struct ref *ref);
5860

5961
int check_ref_type(const struct ref *ref, int flags);

transport.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -504,8 +504,7 @@ static struct ref *get_refs_via_curl(struct transport *transport)
504504

505505
strbuf_release(&buffer);
506506

507-
ref = alloc_ref(strlen("HEAD") + 1);
508-
strcpy(ref->name, "HEAD");
507+
ref = alloc_ref_from_str("HEAD");
509508
if (!walker->fetch_ref(walker, ref) &&
510509
!resolve_remote_symref(ref, refs)) {
511510
ref->next = refs;
@@ -546,9 +545,8 @@ static struct ref *get_refs_from_bundle(struct transport *transport)
546545
die ("Could not read bundle '%s'.", transport->url);
547546
for (i = 0; i < data->header.references.nr; i++) {
548547
struct ref_list_entry *e = data->header.references.list + i;
549-
struct ref *ref = alloc_ref(strlen(e->name) + 1);
548+
struct ref *ref = alloc_ref_from_str(e->name);
550549
hashcpy(ref->old_sha1, e->sha1);
551-
strcpy(ref->name, e->name);
552550
ref->next = result;
553551
result = ref;
554552
}

walker.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,7 @@ static int interpret_target(struct walker *walker, char *target, unsigned char *
190190
if (!get_sha1_hex(target, sha1))
191191
return 0;
192192
if (!check_ref_format(target)) {
193-
struct ref *ref = alloc_ref(strlen(target));
194-
strcpy(ref->name, target);
193+
struct ref *ref = alloc_ref_from_str(target);
195194
if (!walker->fetch_ref(walker, ref)) {
196195
hashcpy(sha1, ref->old_sha1);
197196
free(ref);

0 commit comments

Comments
 (0)