From patchwork Tue May 15 13:04:36 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jim Meyering X-Patchwork-Id: 159314 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 13B13B6FB4 for ; Tue, 15 May 2012 23:05:44 +1000 (EST) Received: from localhost ([::1]:38695 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SUHRd-0003sO-Tc for incoming@patchwork.ozlabs.org; Tue, 15 May 2012 09:05:41 -0400 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 Received: from hx.meyering.net (hx.meyering.net [127.0.0.1]) by hx.meyering.net (8.14.5/8.14.5) with ESMTP id q4FD5FJw024124; Tue, 15 May 2012 15:05:16 +0200 Received: (from meyering@localhost) by hx.meyering.net (8.14.5/8.14.5/Submit) id q4FD5Eht024122; Tue, 15 May 2012 15:05:14 +0200 From: jim@meyering.net To: qemu-devel@nongnu.org Date: Tue, 15 May 2012 15:04:36 +0200 Message-Id: <1337087078-24056-2-git-send-email-jim@meyering.net> X-Mailer: git-send-email 1.7.10.2.484.gcd07cc5 In-Reply-To: <1337087078-24056-1-git-send-email-jim@meyering.net> References: <1337087078-24056-1-git-send-email-jim@meyering.net> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 88.168.87.75 Cc: Jim Meyering Subject: [Qemu-devel] [PATCH 1/3] envlist.c: handle strdup failure X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 */