From e6a7c75298001996790059e3ae70c627b3204dd9 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Tue, 14 Feb 2017 12:26:01 -0500 Subject: show-branch: drop head_len variable We copy the result of resolving HEAD into a buffer and keep track of its length. But we never actually use the length for anything besides the copy. Let's stop passing it around. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano diff --git a/builtin/show-branch.c b/builtin/show-branch.c index 2566935..f8ce537 100644 --- a/builtin/show-branch.c +++ b/builtin/show-branch.c @@ -470,7 +470,7 @@ static void snarf_refs(int head, int remotes) } } -static int rev_is_head(char *head, int headlen, char *name, +static int rev_is_head(char *head, char *name, unsigned char *head_sha1, unsigned char *sha1) { if ((!head[0]) || @@ -623,7 +623,6 @@ int cmd_show_branch(int ac, const char **av, const char *prefix) enum rev_sort_order sort_order = REV_SORT_IN_GRAPH_ORDER; char head[128]; const char *head_p; - int head_len; struct object_id head_oid; int merge_base = 0; int independent = 0; @@ -790,11 +789,10 @@ int cmd_show_branch(int ac, const char **av, const char *prefix) head_p = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, head_oid.hash, NULL); if (head_p) { - head_len = strlen(head_p); + size_t head_len = strlen(head_p); memcpy(head, head_p, head_len + 1); } else { - head_len = 0; head[0] = 0; } @@ -805,7 +803,6 @@ int cmd_show_branch(int ac, const char **av, const char *prefix) * HEAD points at. */ if (rev_is_head(head, - head_len, ref_name[i], head_oid.hash, NULL)) has_head++; @@ -864,7 +861,6 @@ int cmd_show_branch(int ac, const char **av, const char *prefix) for (i = 0; i < num_rev; i++) { int j; int is_head = rev_is_head(head, - head_len, ref_name[i], head_oid.hash, rev[i]->object.oid.hash); -- cgit v0.10.2-6-g49f6 From d9e557a320bd4695bccad873e176562489b2d199 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Tue, 14 Feb 2017 12:27:45 -0500 Subject: show-branch: store resolved head in heap buffer We resolve HEAD and copy the result to a fixed-size buffer with memcpy, never checking that it actually fits. This bug dates back to 8098a178b (Add git-symbolic-ref, 2005-09-30). Before that we used readlink(), which took a maximum buffer size. We can fix this by using resolve_refdup(), which duplicates the buffer on the heap. That also lets us just check for a NULL pointer to see if we have resolved HEAD, and drop the extra head_p variable. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano diff --git a/builtin/show-branch.c b/builtin/show-branch.c index f8ce537..705e492 100644 --- a/builtin/show-branch.c +++ b/builtin/show-branch.c @@ -473,8 +473,7 @@ static void snarf_refs(int head, int remotes) static int rev_is_head(char *head, char *name, unsigned char *head_sha1, unsigned char *sha1) { - if ((!head[0]) || - (head_sha1 && sha1 && hashcmp(head_sha1, sha1))) + if (!head || (head_sha1 && sha1 && hashcmp(head_sha1, sha1))) return 0; if (starts_with(head, "refs/heads/")) head += 11; @@ -621,8 +620,7 @@ int cmd_show_branch(int ac, const char **av, const char *prefix) int all_heads = 0, all_remotes = 0; int all_mask, all_revs; enum rev_sort_order sort_order = REV_SORT_IN_GRAPH_ORDER; - char head[128]; - const char *head_p; + char *head; struct object_id head_oid; int merge_base = 0; int independent = 0; @@ -786,17 +784,10 @@ int cmd_show_branch(int ac, const char **av, const char *prefix) snarf_refs(all_heads, all_remotes); } - head_p = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, - head_oid.hash, NULL); - if (head_p) { - size_t head_len = strlen(head_p); - memcpy(head, head_p, head_len + 1); - } - else { - head[0] = 0; - } + head = resolve_refdup("HEAD", RESOLVE_REF_READING, + head_oid.hash, NULL); - if (with_current_branch && head_p) { + if (with_current_branch && head) { int has_head = 0; for (i = 0; !has_head && i < ref_name_cnt; i++) { /* We are only interested in adding the branch -- cgit v0.10.2-6-g49f6 From d3cc5f4c44eeacca556f9ae0972837a84e72f288 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Wed, 15 Feb 2017 16:40:52 -0500 Subject: show-branch: use skip_prefix to drop magic numbers We make several starts_with() calls, only to advance pointers. This is exactly what skip_prefix() is for, which lets us avoid manually-counted magic numbers. Helped-by: Pranit Bauva Signed-off-by: Jeff King Signed-off-by: Junio C Hamano diff --git a/builtin/show-branch.c b/builtin/show-branch.c index 705e492..c438853 100644 --- a/builtin/show-branch.c +++ b/builtin/show-branch.c @@ -275,8 +275,7 @@ static void show_one_commit(struct commit *commit, int no_name) pp_commit_easy(CMIT_FMT_ONELINE, commit, &pretty); pretty_str = pretty.buf; } - if (starts_with(pretty_str, "[PATCH] ")) - pretty_str += 8; + skip_prefix(pretty_str, "[PATCH] ", &pretty_str); if (!no_name) { if (name && name->head_name) { @@ -470,17 +469,14 @@ static void snarf_refs(int head, int remotes) } } -static int rev_is_head(char *head, char *name, +static int rev_is_head(const char *head, const char *name, unsigned char *head_sha1, unsigned char *sha1) { if (!head || (head_sha1 && sha1 && hashcmp(head_sha1, sha1))) return 0; - if (starts_with(head, "refs/heads/")) - head += 11; - if (starts_with(name, "refs/heads/")) - name += 11; - else if (starts_with(name, "heads/")) - name += 6; + skip_prefix(head, "refs/heads/", &head); + if (!skip_prefix(name, "refs/heads/", &name)) + skip_prefix(name, "heads/", &name); return !strcmp(head, name); } @@ -799,8 +795,9 @@ int cmd_show_branch(int ac, const char **av, const char *prefix) has_head++; } if (!has_head) { - int offset = starts_with(head, "refs/heads/") ? 11 : 0; - append_one_rev(head + offset); + const char *name = head; + skip_prefix(name, "refs/heads/", &name); + append_one_rev(name); } } -- cgit v0.10.2-6-g49f6