From patchwork Tue Dec 6 11:01:02 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 129627 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 78C291007D4 for ; Tue, 6 Dec 2011 22:08:08 +1100 (EST) Received: from localhost ([::1]:60529 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RXsou-0007Za-NO for incoming@patchwork.ozlabs.org; Tue, 06 Dec 2011 06:04:20 -0500 Received: from eggs.gnu.org ([140.186.70.92]:41470) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RXsoO-0005t4-9z for qemu-devel@nongnu.org; Tue, 06 Dec 2011 06:03:49 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RXsoI-0005vq-6Y for qemu-devel@nongnu.org; Tue, 06 Dec 2011 06:03:48 -0500 Received: from e06smtp10.uk.ibm.com ([195.75.94.106]:46954) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RXsoH-0005vD-QW for qemu-devel@nongnu.org; Tue, 06 Dec 2011 06:03:42 -0500 Received: from /spool/local by e06smtp10.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 6 Dec 2011 11:03:41 -0000 Received: from d06nrmr1307.portsmouth.uk.ibm.com ([9.149.38.129]) by e06smtp10.uk.ibm.com ([192.168.101.140]) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Tue, 6 Dec 2011 11:03:28 -0000 Received: from d06av06.portsmouth.uk.ibm.com (d06av06.portsmouth.uk.ibm.com [9.149.37.217]) by d06nrmr1307.portsmouth.uk.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id pB6B3RWZ2527354 for ; Tue, 6 Dec 2011 11:03:27 GMT Received: from d06av06.portsmouth.uk.ibm.com (loopback [127.0.0.1]) by d06av06.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id pB6B3Jxl010859 for ; Tue, 6 Dec 2011 04:03:19 -0700 Received: from localhost (stefanha-thinkpad.manchester-maybrook.uk.ibm.com [9.174.219.31]) by d06av06.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id pB6B3IDQ010847; Tue, 6 Dec 2011 04:03:18 -0700 From: Stefan Hajnoczi To: Date: Tue, 6 Dec 2011 11:01:02 +0000 Message-Id: <1323169274-31657-8-git-send-email-stefanha@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.7.3 In-Reply-To: <1323169274-31657-1-git-send-email-stefanha@linux.vnet.ibm.com> References: <1323169274-31657-1-git-send-email-stefanha@linux.vnet.ibm.com> x-cbid: 11120611-4966-0000-0000-000000C30F96 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 195.75.94.106 Cc: Anthony Liguori , Zhi Hui Li , Stefan Hajnoczi Subject: [Qemu-devel] [PATCH 07/19] win32: fix memory leak 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: Zhi Hui Li string is allocated by g_malloc, will not be used after putenv, should be free before return. Paolo Bonzini confirmed this is safe under Wine: "1) the underlying Win32 APIs require separate arguments for the variable and value; 2) even though in the end Wine stores the environment as name=value (http://source.winehq.org/source/dlls/ntdll/env.c), it does so in a single consecutive block of memory, not as a char* array like POSIX does. While (2) might apply only to Wine, (1) surely applies to Windows as well." Tested-by: Stefan Weil Reviewed-by: Paolo Bonzini Signed-off-by: Li Zhi Hui Signed-off-by: Stefan Hajnoczi --- os-win32.c | 7 +++++++ 1 files changed, 7 insertions(+), 0 deletions(-) diff --git a/os-win32.c b/os-win32.c index 8ad5fa1..8523d8d 100644 --- a/os-win32.c +++ b/os-win32.c @@ -44,6 +44,13 @@ int setenv(const char *name, const char *value, int overwrite) char *string = g_malloc(length); snprintf(string, length, "%s=%s", name, value); result = putenv(string); + + /* Windows takes a copy and does not continue to use our string. + * Therefore it can be safely freed on this platform. POSIX code + * typically has to leak the string because according to the spec it + * becomes part of the environment. + */ + g_free(string); } return result; }