From nobody Wed May 8 17:39:18 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1623766621; cv=none; d=zohomail.com; s=zohoarc; b=J8EiwB3G0U7pzYiXuC41G7ognXX8eM3OF3ayCvucFzF7KcGWuulyZOqoCFx7WihkIqpAuOoX//vimnd5Xvu9fbqieVAfHHJPrQKmz3w37f7jPlPHsK1gIpqgSOddyPV3McAPfpgqkXa3PIxorfnKUHXt8tUy3ZerIHDmpa/WnBA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1623766621; h=Content-Type:Content-Transfer-Encoding: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=CWyyZJAvw9x38ZEnHP9cxu4fak5FxP6114mfMk8flGs=; b=UlrpOo5z80TWf2DiWm6sp65dysSVNd8lIa3bbzZrmOaoUP0/diIcZ+XACIF5gaW6wZ46JwLRngR4JYbomIT07ocQuAaUToe78ZMkvvG1i5EfgkQaM4Xik0u0LDsnKiwak4nNo3ZtYncwueIUf0CBDgVKu9HHzMbIGUXZ8EYtdPo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.zohomail.com with SMTPS id 162376662125912.065332745543401; Tue, 15 Jun 2021 07:17:01 -0700 (PDT) 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-238-riqnPdDtNK2KcRNDGm9yDQ-1; Tue, 15 Jun 2021 10:16:58 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 0F915192D78E; Tue, 15 Jun 2021 14:16:52 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id BC3D860861; Tue, 15 Jun 2021 14:16:51 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id E8E701809CAD; Tue, 15 Jun 2021 14:16:49 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 15FEGnpq008786 for ; Tue, 15 Jun 2021 10:16:49 -0400 Received: by smtp.corp.redhat.com (Postfix) id 690755C22A; Tue, 15 Jun 2021 14:16:49 +0000 (UTC) Received: from speedmetal.redhat.com (unknown [10.40.208.13]) by smtp.corp.redhat.com (Postfix) with ESMTP id C23B95C1C2 for ; Tue, 15 Jun 2021 14:16:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1623766620; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=CWyyZJAvw9x38ZEnHP9cxu4fak5FxP6114mfMk8flGs=; b=H3VNhqcjhnll4xfQnqErHBYSD56cK0YLegC3pSv6cM+wwe20xUnDkVdy4FovJ/GlF1xkVI rfE25thfwAeo1dt17xdelQ1WBudsr+f5UJIMbhI7kkJMayPGDJjROwOi5PRJ+BLAqS3MKJ Szf1uIsUGQZULHQZqm8NdBugImGr8RY= X-MC-Unique: riqnPdDtNK2KcRNDGm9yDQ-1 From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 1/2] virStorageBackendRBDGetVolNames: Fix memory leak in 'rbd_list2' version Date: Tue, 15 Jun 2021 16:16:42 +0200 Message-Id: <3a08c4e91811212471b4b90ac009914ee1fbfd17.1623766567.git.pkrempa@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" The 'rbd_image_spec_t' struct has two string members 'id' and 'name'. We only stole the 'name' members thus the 'id's as well as the whole list would be leaked on success. Restructure the code so that we copy out the image names and call rbd_image_spec_list_cleanup on success rather than on error. The error path is then handled by using g_autofree for 'images'. Since we no longer have a error path after allocating the returned string list we can completely remove it's cleanup. Signed-off-by: Peter Krempa Reviewed-by: J=C3=A1n Tomko --- src/storage/storage_backend_rbd.c | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/storage/storage_backend_rbd.c b/src/storage/storage_backen= d_rbd.c index ce3ab11dd6..371ebfaf1b 100644 --- a/src/storage/storage_backend_rbd.c +++ b/src/storage/storage_backend_rbd.c @@ -579,9 +579,8 @@ static char ** virStorageBackendRBDGetVolNames(virStorageBackendRBDState *ptr) { char **names =3D NULL; - size_t nnames =3D 0; int rc; - rbd_image_spec_t *images =3D NULL; + g_autofree rbd_image_spec_t *images =3D NULL; size_t nimages =3D 16; size_t i; @@ -593,23 +592,18 @@ virStorageBackendRBDGetVolNames(virStorageBackendRBDS= tate *ptr) break; if (rc !=3D -ERANGE) { virReportSystemError(errno, "%s", _("Unable to list RBD images= ")); - goto error; + return NULL; } } names =3D g_new0(char *, nimages + 1); - nnames =3D nimages; for (i =3D 0; i < nimages; i++) - names[i] =3D g_steal_pointer(&images[i].name); + names[i] =3D g_strdup(images[i].name); - return names; - - error: - virStringListFreeCount(names, nnames); rbd_image_spec_list_cleanup(images, nimages); - VIR_FREE(images); - return NULL; + + return names; } #else /* ! WITH_RBD_LIST2 */ --=20 2.31.1 From nobody Wed May 8 17:39:18 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1623766627; cv=none; d=zohomail.com; s=zohoarc; b=CpCQgZ7F37v47g2vunhzVx56GAtDgwGnuEa+64tMBjUFhjHDEqXYwA3TijceMf4qQlXI1ITEhWVG/ttYBz/d4jBPI9jjbdyvpY5JAijE6SbLaGrC7aGAZscfxLeJp+uvWVFGxO3s9Q1Mjn4lVQlpVHQtsiQlTYtZe11OTAZAomg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1623766627; h=Content-Type:Content-Transfer-Encoding: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=6vxU8GMgiL2u/eZKDe57PUAlVmU9m7VYd7mjFHrr9rM=; b=Xg4AMFjvbkMhXDTuV0y1zoc2U0MNRH0arhrEdSUvt4RXJSLZys/ZjBJLtnGps+1TOmfRRNbuigEnOi/TYlCjrbrUNo6hE824ThHNmL+a/C9W/k42t6NifskLfeP+h5TnHJUqD0lumNgCgafYuCgs1zGy/bG3DndgSJHKpcc0zfQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.zohomail.com with SMTPS id 162376662702612.509656806899898; Tue, 15 Jun 2021 07:17:07 -0700 (PDT) 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-567-6XhFnkjiMqakxPY4TPWeNA-1; Tue, 15 Jun 2021 10:17:03 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 5166F57229; Tue, 15 Jun 2021 14:16:57 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 22513100238C; Tue, 15 Jun 2021 14:16:57 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 9346E46F58; Tue, 15 Jun 2021 14:16:56 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 15FEGs0I008886 for ; Tue, 15 Jun 2021 10:16:54 -0400 Received: by smtp.corp.redhat.com (Postfix) id BB4A45C1C2; Tue, 15 Jun 2021 14:16:54 +0000 (UTC) Received: from speedmetal.redhat.com (unknown [10.40.208.13]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2EB595C22A for ; Tue, 15 Jun 2021 14:16:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1623766626; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=6vxU8GMgiL2u/eZKDe57PUAlVmU9m7VYd7mjFHrr9rM=; b=hXCcbAvXBr9GaJ1EVaRg4j6Xyx4NVaL72DCABafuO5jY6wrJgC0GAPk/KegGzYWTnjJPDc /MbUcedwB2nnv36+t2cmu8lX8dL6JO5twJxD7JozgXIidXMFqnDN+F3bhFOJQZ6TOnunS9 N1u+JLA3ppi7L0tM+MOY/INTpitp+yY= X-MC-Unique: 6XhFnkjiMqakxPY4TPWeNA-1 From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 2/2] virStorageBackendRBDGetVolNames: Refactor cleanup in 'rbd_list' version Date: Tue, 15 Jun 2021 16:16:43 +0200 Message-Id: <9a297292ad2f4e88b54c96e53f1e8fae8b57cf9e.1623766567.git.pkrempa@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" Use automatic memory freeing for the string list so that we can remove the cleanup section. Signed-off-by: Peter Krempa Reviewed-by: J=C3=A1n Tomko --- src/storage/storage_backend_rbd.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/storage/storage_backend_rbd.c b/src/storage/storage_backen= d_rbd.c index 371ebfaf1b..a4e8115dc4 100644 --- a/src/storage/storage_backend_rbd.c +++ b/src/storage/storage_backend_rbd.c @@ -611,7 +611,7 @@ virStorageBackendRBDGetVolNames(virStorageBackendRBDSta= te *ptr) static char ** virStorageBackendRBDGetVolNames(virStorageBackendRBDState *ptr) { - char **names =3D NULL; + g_auto(GStrv) names =3D NULL; size_t nnames =3D 0; int rc; size_t max_size =3D 1024; @@ -626,7 +626,7 @@ virStorageBackendRBDGetVolNames(virStorageBackendRBDSta= te *ptr) break; if (rc !=3D -ERANGE) { virReportSystemError(errno, "%s", _("Unable to list RBD images= ")); - goto error; + return NULL; } VIR_FREE(namebuf); } @@ -640,18 +640,14 @@ virStorageBackendRBDGetVolNames(virStorageBackendRBDS= tate *ptr) namedup =3D g_strdup(name); if (VIR_APPEND_ELEMENT(names, nnames, namedup) < 0) - goto error; + return NULL; name +=3D strlen(name) + 1; } VIR_EXPAND_N(names, nnames, 1); - return names; - - error: - virStringListFreeCount(names, nnames); - return NULL; + return g_steal_pointer(&names); } #endif /* ! WITH_RBD_LIST2 */ --=20 2.31.1