name-rev: avoid leaking memory in the `deref` case

When the `name_rev()` function is asked to dereference the tip name, it allocates memory. But when it turns out that another tip already described the commit better than the current one, we forgot to release the memory. Pointed out by Coverity. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed May 4, 2017 at 15:59 UTC 5308224633cf138f436357b2a8a87a546373af72
1 file changed +5 -2
builtin/name-rev.c
+5 -2
@@ -28,6 +28,7 @@ static void name_rev(struct commit *commit,
28 struct rev_name *name = (struct rev_name *)commit->util;
29 struct commit_list *parents;
30 int parent_number = 1;
31 + char *to_free = NULL;
32
33 parse_commit(commit);
34
@@ -35,7 +36,7 @@ static void name_rev(struct commit *commit,
36 return;
37
38 if (deref) {
38 - tip_name = xstrfmt("%s^0", tip_name);
39 + tip_name = to_free = xstrfmt("%s^0", tip_name);
40
41 if (generation)
42 die("generation: %d, but deref?", generation);
@@ -53,8 +54,10 @@ copy_data:
54 name->taggerdate = taggerdate;
55 name->generation = generation;
56 name->distance = distance;
56 - } else
57 + } else {
58 + free(to_free);
59 return;
60 + }
61
62 for (parents = commit->parents;
63 parents;