From patchwork Thu Jan 21 02:36:46 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Blake X-Patchwork-Id: 1429571 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) 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; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=S+l1s+cY; dkim-atps=neutral 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 4DLmqP2kPnz9sVF for ; Thu, 21 Jan 2021 13:41:21 +1100 (AEDT) Received: from localhost ([::1]:59388 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1l2PuR-0005Hi-Ab for incoming@patchwork.ozlabs.org; Wed, 20 Jan 2021 21:41:19 -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 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= 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 X-MC-Unique: XRpqsJSwO2uNzXOYtRY2Fw-1 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) 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 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+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" 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 = arg; NBDExportInfo info = { .request_sizes = false, .name = g_strdup("") }; QIOChannelSocket *sioc; - int fd; - int ret; + int fd = -1; + int ret = EXIT_FAILURE; pthread_t show_parts_thread; Error *local_error = NULL; @@ -278,26 +278,24 @@ static void *nbd_client_thread(void *arg) goto out; } - ret = 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 = 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 = 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 = 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 = EXIT_SUCCESS; + + out: + if (fd >= 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 */