From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:34765) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SWljY-0000Lm-Az for qemu-devel@nongnu.org; Tue, 22 May 2012 05:50:29 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SWljU-0000HM-VB for qemu-devel@nongnu.org; Tue, 22 May 2012 05:50:27 -0400 Received: from mx.meyering.net ([88.168.87.75]:39500 helo=hx.meyering.net) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SWljU-0000H6-J8 for qemu-devel@nongnu.org; Tue, 22 May 2012 05:50:24 -0400 From: Jim Meyering Date: Tue, 22 May 2012 11:50:10 +0200 Message-Id: <1337680210-20569-3-git-send-email-jim@meyering.net> In-Reply-To: <1337680210-20569-1-git-send-email-jim@meyering.net> References: <1337680210-20569-1-git-send-email-jim@meyering.net> Subject: [Qemu-devel] [PATCHv2 2/2] envlist.c: handle strdup failure List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Jim Meyering From: Jim Meyering Without this, envlist_to_environ may silently fail to copy all strings into the destination buffer, and both callers would leak any env strings allocated after a failing strdup, because the freeing code stops at the first NULL pointer. Signed-off-by: Jim Meyering --- envlist.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/envlist.c b/envlist.c index be0addb..7532091 100644 --- a/envlist.c +++ b/envlist.c @@ -234,8 +234,16 @@ envlist_to_environ(const envlist_t *envlist, size_t *count) return (NULL); for (entry = envlist->el_entries.lh_first; entry != NULL; - entry = entry->ev_link.le_next) { - *(penv++) = strdup(entry->ev_var); + entry = entry->ev_link.le_next, penv++) { + *penv = strdup(entry->ev_var); + if (*penv == NULL) { + char **e = env; + while (e <= penv) { + free(*e++); + } + free(env); + return NULL; + } } *penv = NULL; /* NULL terminate the list */ -- 1.7.10.2.552.gaa3bb87