Received: from eggs.gnu.org ([208.118.235.92]:45970) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1T2RET-000846-H0 for qemu-devel@nongnu.org; Fri, 17 Aug 2012 14:25:18 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1T2RES-0000Ez-GH for qemu-devel@nongnu.org; Fri, 17 Aug 2012 14:25:17 -0400 Received: from mx.meyering.net ([88.168.87.75]:39780 helo=hx.meyering.net) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1T2RES-0000Eo-4P for qemu-devel@nongnu.org; Fri, 17 Aug 2012 14:25:16 -0400 From: Jim Meyering Date: Fri, 17 Aug 2012 20:24:51 +0200 Message-Id: <1345227891-32449-3-git-send-email-jim@meyering.net> In-Reply-To: <1345227891-32449-1-git-send-email-jim@meyering.net> References: <1345227891-32449-1-git-send-email-jim@meyering.net> Subject: [Qemu-devel] [PATCHv4 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 230596f..cf3f2d8 100644 --- a/envlist.c +++ b/envlist.c @@ -245,8 +245,16 @@ envlist_to_environ(const envlist_t *envlist, size_t *count) } 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.12.rc2