Skip to content

Commit d0cf51e

Browse files
mhaggergitster
authored andcommitted
do_one_ref(): save and restore value of current_ref
If do_one_ref() is called recursively, then the inner call should not permanently overwrite the value stored in current_ref by the outer call. Aside from the tiny optimization loss, peel_ref() expects the value of current_ref not to change across a call to peel_entry(). But in the presence of replace references that assumption could be violated by a recursive call to do_one_ref: do_for_each_entry() do_one_ref() builtin/describe.c:get_name() peel_ref() peel_entry() peel_object () deref_tag_noverify() parse_object() lookup_replace_object() do_lookup_replace_object() prepare_replace_object() do_for_each_ref() do_for_each_entry() do_for_each_entry_in_dir() do_one_ref() The inner call to do_one_ref() was unconditionally setting current_ref to NULL when it was done, causing peel_ref() to perform an invalid memory access. So change do_one_ref() to save the old value of current_ref before overwriting it, and restore the old value afterward rather than setting it to NULL. Reported-by: Mantas Mikulėnas <[email protected]> Signed-off-by: Michael Haggerty <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2f1ef15 commit d0cf51e

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

refs.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,18 +630,22 @@ struct ref_entry_cb {
630630
static int do_one_ref(struct ref_entry *entry, void *cb_data)
631631
{
632632
struct ref_entry_cb *data = cb_data;
633+
struct ref_entry *old_current_ref;
633634
int retval;
635+
634636
if (prefixcmp(entry->name, data->base))
635637
return 0;
636638

637639
if (!(data->flags & DO_FOR_EACH_INCLUDE_BROKEN) &&
638640
!ref_resolves_to_object(entry))
639641
return 0;
640642

643+
/* Store the old value, in case this is a recursive call: */
644+
old_current_ref = current_ref;
641645
current_ref = entry;
642646
retval = data->fn(entry->name + data->trim, entry->u.value.sha1,
643647
entry->flag, data->cb_data);
644-
current_ref = NULL;
648+
current_ref = old_current_ref;
645649
return retval;
646650
}
647651

0 commit comments

Comments
 (0)