From nobody Sun Feb 8 12:20:42 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=pass(p=reject dis=none) header.from=lists.libvirt.org ARC-Seal: i=1; a=rsa-sha256; t=1770133413; cv=none; d=zohomail.com; s=zohoarc; b=gAeBKp8FcLRak0ghPqFo8O9W4/fywvMKk18ssxIq70qUMCNhYsvVI+XcJIhCf82f8oXihqe8BBgkFIdOekWi8JgYliNX8+c/1u2Ydv+3/9TLOXSGgJ1A5R77yIAAUQVn4iBaIA7qkrxogJbLRSZz5H9KqTvNpe3jbRAm0XKgRO8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1770133413; h=Content-Type:Content-Transfer-Encoding:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Owner:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Subject:Subject:To:To:Message-Id:Cc; bh=qoa5PJKKd6gl41GaTVM72CuA/36rXJNaHz9b7VS/MeI=; b=a+hOMH6Vj6wisInXXZtc7HKc6bbaUcw+XGY9lct4OqbNx3Ic/UThWkQv75CNrpSBAqPcgp/dVdiOpcQ2sIygNCG3vZRzEsiS/3oAMUHYE/EXmdeo73/gH5i5z0zAPamp60IZQ1L0NFRR3KXvJ7UYeBhDZ7HMdOcNFY5MXk2NqVI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1770133413211994.5356201095256; Tue, 3 Feb 2026 07:43:33 -0800 (PST) Received: by lists.libvirt.org (Postfix, from userid 993) id 63AD343DF2; Tue, 3 Feb 2026 10:43:32 -0500 (EST) Received: from [172.19.199.6] (lists.libvirt.org [8.43.85.245]) by lists.libvirt.org (Postfix) with ESMTP id A9B3E43E38; Tue, 3 Feb 2026 10:40:20 -0500 (EST) Received: by lists.libvirt.org (Postfix, from userid 993) id 1AA1C41ADE; Tue, 3 Feb 2026 10:40:17 -0500 (EST) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (3072 bits) server-digest SHA256) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id 89BB941A1D for ; Tue, 3 Feb 2026 10:40:07 -0500 (EST) Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-642-r-9wpemjPIqAAMIBQGs7_A-1; Tue, 03 Feb 2026 10:40:06 -0500 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 3C677180035C for ; Tue, 3 Feb 2026 15:40:05 +0000 (UTC) Received: from moe (unknown [10.43.3.236]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 9AE701956048 for ; Tue, 3 Feb 2026 15:40:04 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-5.0 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED,RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED,SPF_PASS autolearn=unavailable autolearn_force=no version=4.0.1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1770133207; h=from:from: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; bh=qoa5PJKKd6gl41GaTVM72CuA/36rXJNaHz9b7VS/MeI=; b=So5Rz4kZCE0xZzlKrY839L4zVj2f2/XWPLuZDbnONlkQLq2cUYWMbTaetkP0jKczk5YY1Q X/dYs5HPWWU8pzZ9UBvmCS1pcElfg8iNQtjcfBETS1oqQ98v4an1dJNScq6y1GrebD4bYY eBMbbLJPNaY9DfMSfBrPZQDr9PEvpek= X-MC-Unique: r-9wpemjPIqAAMIBQGs7_A-1 X-Mimecast-MFC-AGG-ID: r-9wpemjPIqAAMIBQGs7_A_1770133205 To: devel@lists.libvirt.org Subject: [PATCH 4/9] bhyve_command: Avoid memleak in bhyveBuildNetArgStr() Date: Tue, 3 Feb 2026 16:39:52 +0100 Message-ID: <6ae8e4c87dcc88c42b4499d2d5ceee3f56fdd99a.1770133151.git.mprivozn@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: jZ049Dt80MEdkRsPV7DT4OP9x3Xt5G6hP5LQnU0M4lQ_1770133205 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Message-ID-Hash: JBRLYY7RZMMUXFBPKI3VJDQ6I3DDFXSN X-Message-ID-Hash: JBRLYY7RZMMUXFBPKI3VJDQ6I3DDFXSN X-MailFrom: mprivozn@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; header-match-devel.lists.libvirt.org-0; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: Michal Privoznik via Devel Reply-To: Michal Privoznik X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1770133415335158500 Content-Type: text/plain; charset="utf-8"; x-default="true" From: Michal Privoznik Inside of bhyveBuildNetArgStr() there is @nic_model which is allocated, appended into cmd line and then freed under cleanup label. Firstly, There are few cases where instead of jumping onto the label there's a return statement (this alone can lead to a memory leak), but more importantly - the variable doesn't need dynamically allocated string. It's the same story with @brname. After making them both const strings, the return statements can be used more freely (up until first possible allocation). 6 bytes in 1 blocks are definitely lost in loss record 4 of 508 at 0x4883224: malloc (vg_replace_malloc.c:451) by 0x4EE6562: g_malloc (in /usr/local/lib/libglib-2.0.so.0.8400.4) by 0x4F0100F: g_strdup (in /usr/local/lib/libglib-2.0.so.0.8400.4) by 0x401BC02: g_strdup_inline (gstrfuncs.h:321) by 0x401BC02: bhyveBuildNetArgStr (bhyve_command.c:64) by 0x401B362: virBhyveProcessBuildBhyveCmd (bhyve_command.c:1033) by 0x4015F15: testCompareXMLToArgvFiles (bhyvexml2argvtest.c:72) by 0x4015BB9: testCompareXMLToArgvHelper (bhyvexml2argvtest.c:144) by 0x4016598: virTestRun (testutils.c:143) by 0x4015121: mymain (bhyvexml2argvtest.c:275) by 0x4018892: virTestMain (testutils.c:913) by 0x4013DC6: main (bhyvexml2argvtest.c:352) Signed-off-by: Michal Privoznik --- src/bhyve/bhyve_command.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/bhyve/bhyve_command.c b/src/bhyve/bhyve_command.c index c9bfe22c8c..ff079da9ef 100644 --- a/src/bhyve/bhyve_command.c +++ b/src/bhyve/bhyve_command.c @@ -51,17 +51,17 @@ bhyveBuildNetArgStr(const virDomainDef *def, { char macaddr[VIR_MAC_STRING_BUFLEN]; char *realifname =3D NULL; - char *brname =3D NULL; - char *nic_model =3D NULL; + const char *brname =3D NULL; + const char *nic_model =3D NULL; int ret =3D -1; virDomainNetType actualType =3D virDomainNetGetActualType(net); g_autoptr(virConnect) netconn =3D NULL; =20 if (net->model =3D=3D VIR_DOMAIN_NET_MODEL_VIRTIO) { - nic_model =3D g_strdup("virtio-net"); + nic_model =3D "virtio-net"; } else if (net->model =3D=3D VIR_DOMAIN_NET_MODEL_E1000) { if ((bhyveDriverGetBhyveCaps(driver) & BHYVE_CAP_NET_E1000) !=3D 0= ) { - nic_model =3D g_strdup("e1000"); + nic_model =3D "e1000"; } else { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", _("NIC model 'e1000' is not supported by given = bhyve binary")); @@ -75,9 +75,9 @@ bhyveBuildNetArgStr(const virDomainDef *def, =20 if (net->type =3D=3D VIR_DOMAIN_NET_TYPE_NETWORK) { if (!netconn && !(netconn =3D virGetConnectNetwork())) - goto cleanup; + return -1; if (virDomainNetAllocateActualDevice(netconn, def, net) < 0) - goto cleanup; + return -1; } /* final validation now that actual type is known */ if (virDomainActualNetDefValidate(net) < 0) @@ -88,11 +88,11 @@ bhyveBuildNetArgStr(const virDomainDef *def, switch (actualType) { case VIR_DOMAIN_NET_TYPE_NETWORK: case VIR_DOMAIN_NET_TYPE_BRIDGE: - brname =3D g_strdup(virDomainNetGetActualBridgeName(net)); + brname =3D virDomainNetGetActualBridgeName(net); if (!brname) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("No bridge name specified")); - goto cleanup; + return -1; } break; case VIR_DOMAIN_NET_TYPE_USER: @@ -116,7 +116,7 @@ bhyveBuildNetArgStr(const virDomainDef *def, virReportError(VIR_ERR_CONFIG_UNSUPPORTED, _("Unsupported network type %1$s"), virDomainNetTypeToString(actualType)); - goto cleanup; + return -1; } =20 if (!dryRun) { @@ -156,9 +156,7 @@ bhyveBuildNetArgStr(const virDomainDef *def, cleanup: if (ret < 0) VIR_FREE(net->ifname); - VIR_FREE(brname); VIR_FREE(realifname); - VIR_FREE(nic_model); =20 return ret; } --=20 2.52.0