Skip to content

Commit 05e717d

Browse files
rjustogitster
authored andcommitted
rev-parse: fix a leak with --abbrev-ref
To handle "--abbrev-ref" we use shorten_unambiguous_ref(). This function takes a refname and returns a shortened refname, which is a newly allocated string that needs to be freed. Unfortunately, the refname variable is reused to receive the shortened one. Therefore, we lose the original refname, which needs to be freed as well, producing a leak. This leak can be reviewed with: $ for a in {1..10}; do git branch foo_${a}; done $ git rev-parse --abbrev-ref refs/heads/foo_{1..10} Direct leak of 171 byte(s) in 10 object(s) allocated from: ... in xstrdup wrapper.c ... in expand_ref refs.c ... in repo_dwim_ref refs.c ... in show_rev builtin/rev-parse.c ... in cmd_rev_parse builtin/rev-parse.c ... in run_builtin git.c We have this leak since a45d346 (rev-parse: --abbrev-ref option to shorten ref name, 2009-04-13) when "--abbrev-ref" was introduced. Let's fix it. Signed-off-by: Rubén Justo <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 861c56f commit 05e717d

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

builtin/rev-parse.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,12 @@ static void show_rev(int type, const struct object_id *oid, const char *name)
156156
*/
157157
break;
158158
case 1: /* happy */
159-
if (abbrev_ref)
159+
if (abbrev_ref) {
160+
char *old = full;
160161
full = shorten_unambiguous_ref(full,
161162
abbrev_ref_strict);
163+
free(old);
164+
}
162165
show_with_type(type, full);
163166
break;
164167
default: /* ambiguous */

0 commit comments

Comments
 (0)