Skip to content

Commit 93abd17

Browse files
committed
Merge branch 'jk/show-branch-lift-name-len-limit' into maint
"git show-branch" expected there were only very short branch names in the repository and used a fixed-length buffer to hold them without checking for overflow. * jk/show-branch-lift-name-len-limit: show-branch: use skip_prefix to drop magic numbers show-branch: store resolved head in heap buffer show-branch: drop head_len variable
2 parents f63df94 + d3cc5f4 commit 93abd17

File tree

1 file changed

+13
-29
lines changed

1 file changed

+13
-29
lines changed

builtin/show-branch.c

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,7 @@ static void show_one_commit(struct commit *commit, int no_name)
275275
pp_commit_easy(CMIT_FMT_ONELINE, commit, &pretty);
276276
pretty_str = pretty.buf;
277277
}
278-
if (starts_with(pretty_str, "[PATCH] "))
279-
pretty_str += 8;
278+
skip_prefix(pretty_str, "[PATCH] ", &pretty_str);
280279

281280
if (!no_name) {
282281
if (name && name->head_name) {
@@ -470,18 +469,14 @@ static void snarf_refs(int head, int remotes)
470469
}
471470
}
472471

473-
static int rev_is_head(char *head, int headlen, char *name,
472+
static int rev_is_head(const char *head, const char *name,
474473
unsigned char *head_sha1, unsigned char *sha1)
475474
{
476-
if ((!head[0]) ||
477-
(head_sha1 && sha1 && hashcmp(head_sha1, sha1)))
475+
if (!head || (head_sha1 && sha1 && hashcmp(head_sha1, sha1)))
478476
return 0;
479-
if (starts_with(head, "refs/heads/"))
480-
head += 11;
481-
if (starts_with(name, "refs/heads/"))
482-
name += 11;
483-
else if (starts_with(name, "heads/"))
484-
name += 6;
477+
skip_prefix(head, "refs/heads/", &head);
478+
if (!skip_prefix(name, "refs/heads/", &name))
479+
skip_prefix(name, "heads/", &name);
485480
return !strcmp(head, name);
486481
}
487482

@@ -620,9 +615,7 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
620615
int all_heads = 0, all_remotes = 0;
621616
int all_mask, all_revs;
622617
enum rev_sort_order sort_order = REV_SORT_IN_GRAPH_ORDER;
623-
char head[128];
624-
const char *head_p;
625-
int head_len;
618+
char *head;
626619
struct object_id head_oid;
627620
int merge_base = 0;
628621
int independent = 0;
@@ -787,32 +780,24 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
787780
snarf_refs(all_heads, all_remotes);
788781
}
789782

790-
head_p = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
791-
head_oid.hash, NULL);
792-
if (head_p) {
793-
head_len = strlen(head_p);
794-
memcpy(head, head_p, head_len + 1);
795-
}
796-
else {
797-
head_len = 0;
798-
head[0] = 0;
799-
}
783+
head = resolve_refdup("HEAD", RESOLVE_REF_READING,
784+
head_oid.hash, NULL);
800785

801-
if (with_current_branch && head_p) {
786+
if (with_current_branch && head) {
802787
int has_head = 0;
803788
for (i = 0; !has_head && i < ref_name_cnt; i++) {
804789
/* We are only interested in adding the branch
805790
* HEAD points at.
806791
*/
807792
if (rev_is_head(head,
808-
head_len,
809793
ref_name[i],
810794
head_oid.hash, NULL))
811795
has_head++;
812796
}
813797
if (!has_head) {
814-
int offset = starts_with(head, "refs/heads/") ? 11 : 0;
815-
append_one_rev(head + offset);
798+
const char *name = head;
799+
skip_prefix(name, "refs/heads/", &name);
800+
append_one_rev(name);
816801
}
817802
}
818803

@@ -866,7 +851,6 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
866851
for (i = 0; i < num_rev; i++) {
867852
int j;
868853
int is_head = rev_is_head(head,
869-
head_len,
870854
ref_name[i],
871855
head_oid.hash,
872856
rev[i]->object.oid.hash);

0 commit comments

Comments
 (0)