From patchwork Fri Oct 4 03:43:46 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Mao Zhongyi X-Patchwork-Id: 1171540 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=cmss.chinamobile.com Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 46kwkm0fy2z9sPd for ; Fri, 4 Oct 2019 13:45:34 +1000 (AEST) Received: from localhost ([::1]:41998 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iGEX3-0003od-Fj for incoming@patchwork.ozlabs.org; Thu, 03 Oct 2019 23:45:29 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:43984) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iGEVv-0003mv-6C for qemu-devel@nongnu.org; Thu, 03 Oct 2019 23:44:20 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iGEVt-0001kK-Jh for qemu-devel@nongnu.org; Thu, 03 Oct 2019 23:44:18 -0400 Received: from cmccmta2.chinamobile.com ([221.176.66.80]:3893) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1iGEVs-0001i3-W3 for qemu-devel@nongnu.org; Thu, 03 Oct 2019 23:44:17 -0400 Received: from spf.mail.chinamobile.com (unknown[172.16.121.1]) by rmmx-syy-dmz-app06-12006 (RichMail) with SMTP id 2ee65d96c006b7a-b2957; Fri, 04 Oct 2019 11:44:07 +0800 (CST) X-RM-TRANSID: 2ee65d96c006b7a-b2957 X-RM-TagInfo: emlType=0 X-RM-SPAM-FLAG: 00000000 Received: from maozy-host.lan (unknown[180.108.8.156]) by rmsmtp-syy-appsvr01-12001 (RichMail) with SMTP id 2ee15d96bff81ac-a1b01; Fri, 04 Oct 2019 11:44:06 +0800 (CST) X-RM-TRANSID: 2ee15d96bff81ac-a1b01 From: Mao Zhongyi To: qemu-devel@nongnu.org Subject: [PATCH v3 1/3] tests/migration: mem leak fix Date: Fri, 4 Oct 2019 11:43:46 +0800 Message-Id: X-Mailer: git-send-email 2.17.1 In-Reply-To: References: In-Reply-To: References: MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 221.176.66.80 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: tony.nguyen@bt.com, alex.bennee@linaro.org, armbru@redhat.com, Mao Zhongyi , laurent@vivier.eu Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" ‘data’ has the possibility of memory leaks, so use the glib macros g_autofree recommended by CODING_STYLE.rst to automatically release the memory that returned from g_malloc(). Signed-off-by: Mao Zhongyi Reviewed-by: Alex Bennée --- tests/migration/stress.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/tests/migration/stress.c b/tests/migration/stress.c index d9aa4afe92..9e128eef50 100644 --- a/tests/migration/stress.c +++ b/tests/migration/stress.c @@ -170,10 +170,10 @@ static unsigned long long now(void) static int stressone(unsigned long long ramsizeMB) { size_t pagesPerMB = 1024 * 1024 / PAGE_SIZE; - char *ram = malloc(ramsizeMB * 1024 * 1024); + g_autofree char *ram = g_malloc(ramsizeMB * 1024 * 1024); char *ramptr; size_t i, j, k; - char *data = malloc(PAGE_SIZE); + g_autofree char *data = g_malloc(PAGE_SIZE); char *dataptr; size_t nMB = 0; unsigned long long before, after; @@ -186,7 +186,6 @@ static int stressone(unsigned long long ramsizeMB) if (!data) { fprintf(stderr, "%s (%d): ERROR: cannot allocate %d bytes of RAM: %s\n", argv0, gettid(), PAGE_SIZE, strerror(errno)); - free(ram); return -1; } @@ -198,8 +197,6 @@ static int stressone(unsigned long long ramsizeMB) memset(ram, 0xfe, ramsizeMB * 1024 * 1024); if (random_bytes(data, PAGE_SIZE) < 0) { - free(ram); - free(data); return -1; } @@ -227,9 +224,6 @@ static int stressone(unsigned long long ramsizeMB) } } } - - free(data); - free(ram); }