From nobody Sun Oct 5 19:09:41 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; 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; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1611196916; cv=none; d=zohomail.com; s=zohoarc; b=VsWG9q+Be5/5vRzXqwPr0IHhK17UuWP+KwLHOGgRDlZsVhYt9qwzF9XbFbwYfqFxNsB7iBlN2FzhYxKm2vOoDomwbGVU2uI16BH4N9z4D8SDm0Fv4BZiwoaiAAmmEMQNOTAYHhzvudqg+iDZy7Sl66QyXfsXLQBbqWEbwBO3rrc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1611196916; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=ebwr6/jXj5l/ZGro+AvIlQw3OXBOf0H15vnVN50FwjA=; b=K7ZoNZaruOU0UF1DtdgP3fMRQsuvUtIu+nI/IlPD7JqIXd+CEyxbTP+0Ka+a073Dx6siBDhF0I4GlKQw/AwszObDECaabDHYV5ZXWGqB+cM8xqRGLmHR7y5ul+rVpBOTix7GAcJLrMdeRFoX8Cw2pNbGfuOGd17kkt0sHcuSYWo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; 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; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 16111969168111.1210506766951767; Wed, 20 Jan 2021 18:41:56 -0800 (PST) Received: from localhost ([::1]:35136 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1l2Pv0-00070B-P1 for importer@patchew.org; Wed, 20 Jan 2021 21:41:54 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:40742) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1l2PqS-0001T6-Nk for qemu-devel@nongnu.org; Wed, 20 Jan 2021 21:37:12 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:34664) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1l2PqO-0003hC-Tm for qemu-devel@nongnu.org; Wed, 20 Jan 2021 21:37:11 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-535-XRpqsJSwO2uNzXOYtRY2Fw-1; Wed, 20 Jan 2021 21:37:04 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 8AB4F8030A0; Thu, 21 Jan 2021 02:37:02 +0000 (UTC) Received: from blue.redhat.com (ovpn-113-116.phx2.redhat.com [10.3.113.116]) by smtp.corp.redhat.com (Postfix) with ESMTP id E366A5D764; Thu, 21 Jan 2021 02:37:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1611196628; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ebwr6/jXj5l/ZGro+AvIlQw3OXBOf0H15vnVN50FwjA=; b=S+l1s+cY/YDjCl74W29zItg3SNRRhtx12gfmwLeg9qedwzmjI6I+VPFPmUGOybR6n2qaGs +asxVQM0gtI+HwQA23JhNrN+PZ0m/74Neaz8lynlvHCsTCC5LpNHcn7bgSa1u7cioa8PSk gh+TdthAfxO08e0ow1vW51LvS88f5Ms= X-MC-Unique: XRpqsJSwO2uNzXOYtRY2Fw-1 From: Eric Blake To: qemu-devel@nongnu.org Subject: [PULL 02/13] qemu-nbd: Fix a memleak in nbd_client_thread() Date: Wed, 20 Jan 2021 20:36:46 -0600 Message-Id: <20210121023657.1186241-3-eblake@redhat.com> In-Reply-To: <20210121023657.1186241-1-eblake@redhat.com> References: <20210121023657.1186241-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eblake@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable 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=216.205.24.124; envelope-from=eblake@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -29 X-Spam_score: -3.0 X-Spam_bar: --- X-Spam_report: (-3.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.167, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable 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 , Vladimir Sementsov-Ogievskiy , "open list:Network Block Dev..." , Euler Robot Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" From: Alex Chen 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 Message-Id: <20201208134944.27962-1-alex.chen@huawei.com> --- qemu-nbd.c | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/qemu-nbd.c b/qemu-nbd.c index 47587a709e6b..0d513cb38c88 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; @@ -278,26 +278,24 @@ static void *nbd_client_thread(void *arg) goto out; } - 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; } 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; } - 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; } /* update partition table */ @@ -311,24 +309,20 @@ static void *nbd_client_thread(void *arg) dup2(STDOUT_FILENO, STDERR_FILENO); } - ret =3D nbd_client(fd); - if (ret) { - goto out_fd; + if (nbd_client(fd) < 0) { + goto out; } - close(fd); - object_unref(OBJECT(sioc)); - g_free(info.name); - kill(getpid(), SIGTERM); - return (void *) EXIT_SUCCESS; -out_fd: - close(fd); -out_socket: + ret =3D EXIT_SUCCESS; + + 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 2.30.0