From nobody Mon Nov 17 23:51:27 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1607007276; cv=none; d=zohomail.com; s=zohoarc; b=K7njXfhHPfk0/GCKypr0qR+B0nLLOBxyyW5CYqjhbzpABKyA5UtByuvwakCPqtoP2bUl/ZOUye/xJG/FWSPIkqS0PdEZlHzhI17Gsq9QArd6duG38ZRfopyxtLu7+lFoWEMUWDGpFtdx1W8h7MiJo7otkmZnKlWOFIeE7EvrjzE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1607007276; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=CIha/sGhbz1wBBMPMZCFKNKmfE+BB0Zi4tpr1Otv8IA=; b=KBOGhm7hmbv4Z8kkbXSJAyxllgkeLo9cDRjMjjpkS8jjGXpb+AT6crygMVfbHoOQ/AlfSoMOv+HfVS1iGZME/uHAbl8LIj4r8ZYXAXwFbGPhikZs4/IznpDIyk0TdBWazsuQWrAGbl3WSUarUqMWddS8/fZCtyfMaR1kc8A8/S4= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1607007276460511.42627230186713; Thu, 3 Dec 2020 06:54:36 -0800 (PST) Received: from localhost ([::1]:58632 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kkpLc-0002HW-SV for importer@patchew.org; Thu, 03 Dec 2020 09:12:40 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:35332) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kkpKx-0001fY-M8; Thu, 03 Dec 2020 09:11:59 -0500 Received: from szxga04-in.huawei.com ([45.249.212.190]:2822) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kkpKu-0001J3-UV; Thu, 03 Dec 2020 09:11:59 -0500 Received: from DGGEMS411-HUB.china.huawei.com (unknown [172.30.72.60]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4CmyS01ksQzkjTs; Thu, 3 Dec 2020 22:11:12 +0800 (CST) Received: from huawei.com (10.175.124.27) by DGGEMS411-HUB.china.huawei.com (10.3.19.211) with Microsoft SMTP Server id 14.3.487.0; Thu, 3 Dec 2020 22:11:41 +0800 From: Alex Chen To: Subject: [PATCH v2] qemu-nbd: Fix a memleak in nbd_client_thread() Date: Thu, 3 Dec 2020 13:58:55 +0000 Message-ID: <20201203135855.70136-1-alex.chen@huawei.com> X-Mailer: git-send-email 2.19.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.175.124.27] X-CFilter-Loop: Reflected Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=45.249.212.190; envelope-from=alex.chen@huawei.com; helo=szxga04-in.huawei.com X-Spam_score_int: -41 X-Spam_score: -4.2 X-Spam_bar: ---- X-Spam_report: (-4.2 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action 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: alex.chen@huawei.com, qemu-trivial@nongnu.org, qemu-devel@nongnu.org, qemu-block@nongnu.org, zhang.zhanghailiang@huawei.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" When the qio_channel_socket_connect_sync() fails we should goto 'out_socket' label to free the 'sioc' instead of goto 'out' label. In addition, there's a lot of redundant code in the successful branch and the error branch, optimize it. Reported-by: Euler Robot Signed-off-by: Alex Chen Signed-off-by: Eric Blake Reviewed-by: Vladimir Sementsov-Ogievskiy --- qemu-nbd.c | 38 +++++++++++++++----------------------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/qemu-nbd.c b/qemu-nbd.c index a7075c5419..9583ee1af6 100644 --- a/qemu-nbd.c +++ b/qemu-nbd.c @@ -265,8 +265,8 @@ static void *nbd_client_thread(void *arg) char *device =3D arg; NBDExportInfo info =3D { .request_sizes =3D false, .name =3D g_strdup(= "") }; QIOChannelSocket *sioc; - int fd; - int ret; + int fd =3D -1; + int ret =3D EXIT_FAILURE; pthread_t show_parts_thread; Error *local_error =3D NULL; =20 @@ -278,26 +278,24 @@ static void *nbd_client_thread(void *arg) goto out; } =20 - ret =3D nbd_receive_negotiate(NULL, QIO_CHANNEL(sioc), - NULL, NULL, NULL, &info, &local_error); - if (ret < 0) { + if (nbd_receive_negotiate(NULL, QIO_CHANNEL(sioc), + NULL, NULL, NULL, &info, &local_error) < 0) { if (local_error) { error_report_err(local_error); } - goto out_socket; + goto out; } =20 fd =3D open(device, O_RDWR); if (fd < 0) { /* Linux-only, we can use %m in printf. */ error_report("Failed to open %s: %m", device); - goto out_socket; + goto out; } =20 - ret =3D nbd_init(fd, sioc, &info, &local_error); - if (ret < 0) { + if (nbd_init(fd, sioc, &info, &local_error) < 0) { error_report_err(local_error); - goto out_fd; + goto out; } =20 /* update partition table */ @@ -311,24 +309,18 @@ static void *nbd_client_thread(void *arg) dup2(STDOUT_FILENO, STDERR_FILENO); } =20 - ret =3D nbd_client(fd); - if (ret) { - goto out_fd; + if (nbd_client(fd) =3D=3D 0) { + ret =3D EXIT_SUCCESS; } - close(fd); - object_unref(OBJECT(sioc)); - g_free(info.name); - kill(getpid(), SIGTERM); - return (void *) EXIT_SUCCESS; =20 -out_fd: - close(fd); -out_socket: + out: + if (fd >=3D 0) { + close(fd); + } object_unref(OBJECT(sioc)); -out: g_free(info.name); kill(getpid(), SIGTERM); - return (void *) EXIT_FAILURE; + return (void *) (intptr_t) ret; } #endif /* HAVE_NBD_DEVICE */ =20 --=20 2.19.1