run-command.c: fix broken list iteration in clear_child_for_cleanup

Iterate through children_to_clean using 'next' fields but with an extra level of indirection. This allows us to update the chain when we remove a child and saves us managing several variables around the loop mechanism. Signed-off-by: David Gould <david@optimisefitness.com> Acked-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

David Gould committed Sep 11, 2012 at 15:32 UTC bdee397d7c2345d467548ef9446a2a63b72c5449
1 file changed +7 -6
run-command.c
+7 -6
@@ -49,13 +49,14 @@ static void mark_child_for_cleanup(pid_t pid)
49
50 static void clear_child_for_cleanup(pid_t pid)
51 {
52 - struct child_to_clean **last, *p;
52 + struct child_to_clean **pp;
53
54 - last = &children_to_clean;
55 - for (p = children_to_clean; p; p = p->next) {
56 - if (p->pid == pid) {
57 - *last = p->next;
58 - free(p);
54 + for (pp = &children_to_clean; *pp; pp = &(*pp)->next) {
55 + struct child_to_clean *clean_me = *pp;
56 +
57 + if (clean_me->pid == pid) {
58 + *pp = clean_me->next;
59 + free(clean_me);
60 return;
61 }
62 }