Skip to content

Commit 486620a

Browse files
avargitster
authored andcommitted
name-rev: don't xstrdup() an already dup'd string
When "add_to_tip_table()" is called with a non-zero "shorten_unambiguous" we always return an xstrdup()'d string, which we'd then xstrdup() again, leaking memory. See [1] and [2] for how this leak came about. We could xstrdup() only if "shorten_unambiguous" wasn't true, but let's instead inline this code, so that information on whether we need to xstrdup() is contained within add_to_tip_table(). 1. 98c5c4a (name-rev: allow to specify a subpath for --refs option, 2013-06-18) 2. b23e0b9 (name-rev: allow converting the exact object name at the tip of a ref, 2013-07-07) Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7615cf9 commit 486620a

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

builtin/name-rev.c

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -273,17 +273,6 @@ static int subpath_matches(const char *path, const char *filter)
273273
return -1;
274274
}
275275

276-
static const char *name_ref_abbrev(const char *refname, int shorten_unambiguous)
277-
{
278-
if (shorten_unambiguous)
279-
refname = shorten_unambiguous_ref(refname, 0);
280-
else if (skip_prefix(refname, "refs/heads/", &refname))
281-
; /* refname already advanced */
282-
else
283-
skip_prefix(refname, "refs/", &refname);
284-
return refname;
285-
}
286-
287276
struct name_ref_data {
288277
int tags_only;
289278
int name_only;
@@ -309,11 +298,19 @@ static void add_to_tip_table(const struct object_id *oid, const char *refname,
309298
int shorten_unambiguous, struct commit *commit,
310299
timestamp_t taggerdate, int from_tag, int deref)
311300
{
312-
refname = name_ref_abbrev(refname, shorten_unambiguous);
301+
char *short_refname = NULL;
302+
303+
if (shorten_unambiguous)
304+
short_refname = shorten_unambiguous_ref(refname, 0);
305+
else if (skip_prefix(refname, "refs/heads/", &refname))
306+
; /* refname already advanced */
307+
else
308+
skip_prefix(refname, "refs/", &refname);
313309

314310
ALLOC_GROW(tip_table.table, tip_table.nr + 1, tip_table.alloc);
315311
oidcpy(&tip_table.table[tip_table.nr].oid, oid);
316-
tip_table.table[tip_table.nr].refname = xstrdup(refname);
312+
tip_table.table[tip_table.nr].refname = short_refname ?
313+
short_refname : xstrdup(refname);
317314
tip_table.table[tip_table.nr].commit = commit;
318315
tip_table.table[tip_table.nr].taggerdate = taggerdate;
319316
tip_table.table[tip_table.nr].from_tag = from_tag;

0 commit comments

Comments
 (0)