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
This commit is contained in:
Junio C Hamano 2017-03-21 15:03:29 -07:00
commit 93abd17871

View file

@ -275,8 +275,7 @@ static void show_one_commit(struct commit *commit, int no_name)
pp_commit_easy(CMIT_FMT_ONELINE, commit, &pretty); pp_commit_easy(CMIT_FMT_ONELINE, commit, &pretty);
pretty_str = pretty.buf; pretty_str = pretty.buf;
} }
if (starts_with(pretty_str, "[PATCH] ")) skip_prefix(pretty_str, "[PATCH] ", &pretty_str);
pretty_str += 8;
if (!no_name) { if (!no_name) {
if (name && name->head_name) { if (name && name->head_name) {
@ -470,18 +469,14 @@ static void snarf_refs(int head, int remotes)
} }
} }
static int rev_is_head(char *head, int headlen, char *name, static int rev_is_head(const char *head, const char *name,
unsigned char *head_sha1, unsigned char *sha1) unsigned char *head_sha1, unsigned char *sha1)
{ {
if ((!head[0]) || if (!head || (head_sha1 && sha1 && hashcmp(head_sha1, sha1)))
(head_sha1 && sha1 && hashcmp(head_sha1, sha1)))
return 0; return 0;
if (starts_with(head, "refs/heads/")) skip_prefix(head, "refs/heads/", &head);
head += 11; if (!skip_prefix(name, "refs/heads/", &name))
if (starts_with(name, "refs/heads/")) skip_prefix(name, "heads/", &name);
name += 11;
else if (starts_with(name, "heads/"))
name += 6;
return !strcmp(head, name); return !strcmp(head, name);
} }
@ -620,9 +615,7 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
int all_heads = 0, all_remotes = 0; int all_heads = 0, all_remotes = 0;
int all_mask, all_revs; int all_mask, all_revs;
enum rev_sort_order sort_order = REV_SORT_IN_GRAPH_ORDER; enum rev_sort_order sort_order = REV_SORT_IN_GRAPH_ORDER;
char head[128]; char *head;
const char *head_p;
int head_len;
struct object_id head_oid; struct object_id head_oid;
int merge_base = 0; int merge_base = 0;
int independent = 0; int independent = 0;
@ -787,32 +780,24 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
snarf_refs(all_heads, all_remotes); snarf_refs(all_heads, all_remotes);
} }
head_p = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, head = resolve_refdup("HEAD", RESOLVE_REF_READING,
head_oid.hash, NULL); head_oid.hash, NULL);
if (head_p) {
head_len = strlen(head_p);
memcpy(head, head_p, head_len + 1);
}
else {
head_len = 0;
head[0] = 0;
}
if (with_current_branch && head_p) { if (with_current_branch && head) {
int has_head = 0; int has_head = 0;
for (i = 0; !has_head && i < ref_name_cnt; i++) { for (i = 0; !has_head && i < ref_name_cnt; i++) {
/* We are only interested in adding the branch /* We are only interested in adding the branch
* HEAD points at. * HEAD points at.
*/ */
if (rev_is_head(head, if (rev_is_head(head,
head_len,
ref_name[i], ref_name[i],
head_oid.hash, NULL)) head_oid.hash, NULL))
has_head++; has_head++;
} }
if (!has_head) { if (!has_head) {
int offset = starts_with(head, "refs/heads/") ? 11 : 0; const char *name = head;
append_one_rev(head + offset); skip_prefix(name, "refs/heads/", &name);
append_one_rev(name);
} }
} }
@ -866,7 +851,6 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
for (i = 0; i < num_rev; i++) { for (i = 0; i < num_rev; i++) {
int j; int j;
int is_head = rev_is_head(head, int is_head = rev_is_head(head,
head_len,
ref_name[i], ref_name[i],
head_oid.hash, head_oid.hash,
rev[i]->object.oid.hash); rev[i]->object.oid.hash);