From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:50587) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SUHRR-0003WZ-ET for qemu-devel@nongnu.org; Tue, 15 May 2012 09:05:35 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SUHRH-0006Ft-IM for qemu-devel@nongnu.org; Tue, 15 May 2012 09:05:28 -0400 Received: from mx.meyering.net ([88.168.87.75]:59192 helo=hx.meyering.net) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SUHRH-0006FQ-4d for qemu-devel@nongnu.org; Tue, 15 May 2012 09:05:19 -0400 From: jim@meyering.net Date: Tue, 15 May 2012 15:04:36 +0200 Message-Id: <1337087078-24056-2-git-send-email-jim@meyering.net> In-Reply-To: <1337087078-24056-1-git-send-email-jim@meyering.net> References: <1337087078-24056-1-git-send-email-jim@meyering.net> Subject: [Qemu-devel] [PATCH 1/3] 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 | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/envlist.c b/envlist.c index f2303cd..2bbd99c 100644 --- a/envlist.c +++ b/envlist.c @@ -235,7 +235,14 @@ 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); + if ((*(penv++) = strdup(entry->ev_var)) == NULL) { + char **e = env; + while (e != penv) { + free(*e++); + } + free(env); + return NULL; + } } *penv = NULL; /* NULL terminate the list */ -- 1.7.10.2.484.gcd07cc5