From: David Gould Date: Tue, 11 Sep 2012 14:32:47 +0000 (+0100) Subject: run-command.c: fix broken list iteration in clear_child_for_cleanup X-Git-Tag: v1.7.12.2~11^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=bdee397d7c2345d467548ef9446a2a63b72c5449;p=thirdparty%2Fgit.git 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 Acked-by: Jeff King Signed-off-by: Junio C Hamano --- diff --git a/run-command.c b/run-command.c index 0204aaf7e8..d1d58d3e21 100644 --- a/run-command.c +++ b/run-command.c @@ -49,13 +49,14 @@ static void mark_child_for_cleanup(pid_t pid) static void clear_child_for_cleanup(pid_t pid) { - struct child_to_clean **last, *p; + struct child_to_clean **pp; - last = &children_to_clean; - for (p = children_to_clean; p; p = p->next) { - if (p->pid == pid) { - *last = p->next; - free(p); + for (pp = &children_to_clean; *pp; pp = &(*pp)->next) { + struct child_to_clean *clean_me = *pp; + + if (clean_me->pid == pid) { + *pp = clean_me->next; + free(clean_me); return; } }