From nobody Sun Feb 8 18:44:53 2026 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=1614696613; cv=none; d=zohomail.com; s=zohoarc; b=R7GAMdSAxGDP6U4cN+sIFqwOAXW58Jp5adGNC/IoouD92I5G+a4L32jSk9Z9iYTlRir2JuVpIZW99kZOjdCZXNOy1mADc1ms/fEgKNIjnIPT05I0IOhNLh4yIhapt0lMuP2svT3hwcia97s+75lVsRPFKNODlf6xUs1ukbDRScc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1614696613; 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=iXWBdR5wO1UmRKWocksAJcVbTJA2jjCffPbGCfSazdk=; b=GkdeYIl2u7DkV9dUWPt7Y1y3gPinabIz+K7VkrnhVOt3uowhntiwKLAOrluLmkEoOV0fCJf55wvsQZ/HVpq74NcnJ+KaRyVghCEcR2NyUbmFL3D4LP/tMwb4sy8itxNYqihbQT10TZghW4ExFjU5J6x7cTCUoyKOybslFwCkwzo= 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) header.from= 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 1614696613779194.6273356858926; Tue, 2 Mar 2021 06:50:13 -0800 (PST) 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-533-FHateh3eO7WYwGuF7cBlZA-1; Tue, 02 Mar 2021 09:50:10 -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 8F668801989; Tue, 2 Mar 2021 14:50:01 +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 4307C5D768; Tue, 2 Mar 2021 14:50:01 +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 F082318089C9; Tue, 2 Mar 2021 14:50:00 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 122EnljJ014490 for ; Tue, 2 Mar 2021 09:49:47 -0500 Received: by smtp.corp.redhat.com (Postfix) id 03D6B5D723; Tue, 2 Mar 2021 14:49:47 +0000 (UTC) Received: from speedmetal.lan (unknown [10.40.208.53]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2AA605D722 for ; Tue, 2 Mar 2021 14:49:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1614696612; 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=iXWBdR5wO1UmRKWocksAJcVbTJA2jjCffPbGCfSazdk=; b=gx/oY8pqom0GLp6Szhu/QFTDABYNr6Codc5G7KF5AVPhQD/um4Z91FLSsm3Hb6IVrKyKpq p+5YgXkMFrLU0WNPlbjWftb4wextoBZ9/C3tWxF3ge54xjjMkNCcitiT7tfT4MEbawTxSS 2cAzjwPz8zHIQzSYoZXnK+XkkDDWyEU= X-MC-Unique: FHateh3eO7WYwGuF7cBlZA-1 From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 08/16] xenParseVif: Refactor parser Date: Tue, 2 Mar 2021 15:49:25 +0100 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 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.15 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 g_strsplit to split the string and avoid use of stack'd strings. Signed-off-by: Peter Krempa --- src/libxl/xen_common.c | 136 ++++++++++++++--------------------------- 1 file changed, 46 insertions(+), 90 deletions(-) diff --git a/src/libxl/xen_common.c b/src/libxl/xen_common.c index 781483f496..09c74dcb53 100644 --- a/src/libxl/xen_common.c +++ b/src/libxl/xen_common.c @@ -1141,104 +1141,61 @@ xenParseVif(char *entry, const char *vif_typename) { virDomainNetDefPtr net =3D NULL; virDomainNetDefPtr ret =3D NULL; - char *script =3D NULL; - char model[10]; - char type[10]; - char ip[128]; - char mac[18]; - char bridge[50]; - char vifname[50]; - char rate[50]; - char *key; - - bridge[0] =3D '\0'; - mac[0] =3D '\0'; - ip[0] =3D '\0'; - model[0] =3D '\0'; - type[0] =3D '\0'; - vifname[0] =3D '\0'; - rate[0] =3D '\0'; - - key =3D entry; - while (key) { - char *data; - char *nextkey =3D strchr(key, ','); - - if (!(data =3D strchr(key, '=3D'))) + g_auto(GStrv) keyvals =3D NULL; + GStrv keyval; + g_autofree char *script =3D NULL; + g_autofree char *model =3D NULL; + g_autofree char *type =3D NULL; + g_autofree char *ip =3D NULL; + g_autofree char *mac =3D NULL; + g_autofree char *bridge =3D NULL; + g_autofree char *vifname =3D NULL; + g_autofree char *rate =3D NULL; + + keyvals =3D g_strsplit(entry, ",", 0); + + for (keyval =3D keyvals; keyval && *keyval; keyval++) { + const char *key =3D *keyval; + char *val =3D strchr(key, '=3D'); + + virSkipSpaces(&key); + + if (!val) return NULL; - data++; + + val++; if (STRPREFIX(key, "mac=3D")) { - int len =3D nextkey ? (nextkey - data) : strlen(data); - if (virStrncpy(mac, data, len, sizeof(mac)) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("MAC address %s too big for destination"), - data); - return NULL; - } + g_clear_pointer(&mac, g_free); + mac =3D g_strdup(val); } else if (STRPREFIX(key, "bridge=3D")) { - int len =3D nextkey ? (nextkey - data) : strlen(data); - if (virStrncpy(bridge, data, len, sizeof(bridge)) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Bridge %s too big for destination"), - data); - return NULL; - } + g_clear_pointer(&bridge, g_free); + bridge =3D g_strdup(val); } else if (STRPREFIX(key, "script=3D")) { - int len =3D nextkey ? (nextkey - data) : strlen(data); - VIR_FREE(script); - script =3D g_strndup(data, len); + g_clear_pointer(&script, g_free); + script =3D g_strdup(val); } else if (STRPREFIX(key, "model=3D")) { - int len =3D nextkey ? (nextkey - data) : strlen(data); - if (virStrncpy(model, data, len, sizeof(model)) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Model %s too big for destination"), - data); - return NULL; - } + g_clear_pointer(&model, g_free); + model =3D g_strdup(val); } else if (STRPREFIX(key, "type=3D")) { - int len =3D nextkey ? (nextkey - data) : strlen(data); - if (virStrncpy(type, data, len, sizeof(type)) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Type %s too big for destination"), - data); - return NULL; - } + g_clear_pointer(&type, g_free); + type =3D g_strdup(val); } else if (STRPREFIX(key, "vifname=3D")) { - int len =3D nextkey ? (nextkey - data) : strlen(data); - if (virStrncpy(vifname, data, len, sizeof(vifname)) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Vifname %s too big for destination"), - data); - return NULL; - } + g_clear_pointer(&vifname, g_free); + vifname =3D g_strdup(val); } else if (STRPREFIX(key, "ip=3D")) { - int len =3D nextkey ? (nextkey - data) : strlen(data); - if (virStrncpy(ip, data, len, sizeof(ip)) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("IP %s too big for destination"), data); - return NULL; - } + g_clear_pointer(&ip, g_free); + ip =3D g_strdup(val); } else if (STRPREFIX(key, "rate=3D")) { - int len =3D nextkey ? (nextkey - data) : strlen(data); - if (virStrncpy(rate, data, len, sizeof(rate)) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("rate %s too big for destination"), data); - return NULL; - } + g_clear_pointer(&rate, g_free); + rate =3D g_strdup(val); } - - while (nextkey && (nextkey[0] =3D=3D ',' || - nextkey[0] =3D=3D ' ' || - nextkey[0] =3D=3D '\t')) - nextkey++; - key =3D nextkey; } if (!(net =3D virDomainNetDefNew(NULL))) goto cleanup; - if (mac[0]) { + if (mac) { if (virMacAddrParse(mac, &net->mac) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, _("malformed mac address '%s'"), mac); @@ -1246,18 +1203,18 @@ xenParseVif(char *entry, const char *vif_typename) } } - if (bridge[0] || STREQ_NULLABLE(script, "vif-bridge") || + if (bridge || STREQ_NULLABLE(script, "vif-bridge") || STREQ_NULLABLE(script, "vif-vnic")) { net->type =3D VIR_DOMAIN_NET_TYPE_BRIDGE; } else { net->type =3D VIR_DOMAIN_NET_TYPE_ETHERNET; } - if (net->type =3D=3D VIR_DOMAIN_NET_TYPE_BRIDGE && bridge[0]) { + if (net->type =3D=3D VIR_DOMAIN_NET_TYPE_BRIDGE && bridge) { if (xenParseVifBridge(net, bridge) < 0) goto cleanup; } - if (ip[0]) { + if (ip) { char **ip_list =3D g_strsplit(ip, " ", 0); size_t i; @@ -1276,18 +1233,18 @@ xenParseVif(char *entry, const char *vif_typename) if (script && script[0]) net->script =3D g_strdup(script); - if (model[0]) { + if (model) { if (virDomainNetSetModelString(net, model) < 0) goto cleanup; } else { - if (type[0] && STREQ(type, vif_typename)) + if (type && STREQ(type, vif_typename)) net->model =3D VIR_DOMAIN_NET_MODEL_NETFRONT; } - if (vifname[0]) + if (vifname && vifname[0]) net->ifname =3D g_strdup(vifname); - if (rate[0]) { + if (rate) { virNetDevBandwidthPtr bandwidth; unsigned long long kbytes_per_sec; @@ -1304,7 +1261,6 @@ xenParseVif(char *entry, const char *vif_typename) cleanup: virDomainNetDefFree(net); - VIR_FREE(script); return ret; } --=20 2.29.2