From nobody Thu May 2 09:31:24 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=1615479841; cv=none; d=zohomail.com; s=zohoarc; b=LBeZxHd2tht0p7cTKkmSOg4PdpK5r9t0uQhwycjsSpXSX1OYP8iixskbRcp46P+EsY9LsHW32TA/6wdUgjsR2Xo2zLN0rjoCfeO/G6iw8Z736RFOiY8Re8pWeLibtLOLXEe23Vbv4LlflvTv5r9BTd360iEgYZlfBRWFwXjhysQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1615479841; h=Content-Type:Content-Transfer-Encoding:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=uunSJ52M2d4eOR47C9LMAk36uIw1+ky/X/v+MyDuJik=; b=mmq4pIgtx/UePoabzXORo9hfZel4r+LFCW5N54bUxcTUFw7fsadZqlyJhMVSJDeSvNguoETFnWNGKiKIWbUT3xsTjYjLNKjq2hYpfb8MSraP1HiNdkm4x22lmRFx81B5uKK5H7pq7gD41nktiFGmPVPIVageSoKO6qYFHxdg8hY= 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) header.from= 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 16154798419201018.064411826272; Thu, 11 Mar 2021 08:24:01 -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-196-fohh6-0KPhuRaosHRAgLtg-1; Thu, 11 Mar 2021 11:23:58 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 7A821800D55; Thu, 11 Mar 2021 16:23:53 +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 E58B45DDAD; Thu, 11 Mar 2021 16:23: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 710651800232; Thu, 11 Mar 2021 16:23:50 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 12BGNmDd017780 for ; Thu, 11 Mar 2021 11:23:48 -0500 Received: by smtp.corp.redhat.com (Postfix) id D6095138EA; Thu, 11 Mar 2021 16:23:48 +0000 (UTC) Received: from speedmetal.lan (unknown [10.40.208.53]) by smtp.corp.redhat.com (Postfix) with ESMTP id 90FF3610F1 for ; Thu, 11 Mar 2021 16:23:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1615479840; 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:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=uunSJ52M2d4eOR47C9LMAk36uIw1+ky/X/v+MyDuJik=; b=QckCm2dMauTIsTb6RYW6NV1riZ/KWn5AeHx2nOUtta5SJLD5QlYOgdj2U0NTEu4g/e+C+x NlKEK0sJgTCWVGceYmiefw+saFn1sb4khvzgeA7oTixsKmMbryOuEzSMa4E/X0+rWEjN/z k3I0YbtzKP1GOMrYah3PP2ZP2udOq8w= X-MC-Unique: fohh6-0KPhuRaosHRAgLtg-1 From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH v2] xenParseVif: Refactor parser Date: Thu, 11 Mar 2021 17:23:26 +0100 Message-Id: <9832f390e51c6c6b07d5fdbfb24bdf70bde60fc0.1615479689.git.pkrempa@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 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.14 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 Reviewed-by: Michal Privoznik --- src/libxl/xen_common.c | 130 +++++++++++++---------------------------- 1 file changed, 39 insertions(+), 91 deletions(-) v2: - helper variables are made const and strings are borrowed from the array returned by g_strsplit instead of copying/freeing - second argument of xenParseVifBridge turned const to silence compiler diff --git a/src/libxl/xen_common.c b/src/libxl/xen_common.c index c56815d7fc..0d3a3f994a 100644 --- a/src/libxl/xen_common.c +++ b/src/libxl/xen_common.c @@ -1027,7 +1027,7 @@ xenParseCharDev(virConfPtr conf, virDomainDefPtr def,= const char *nativeFormat) static int -xenParseVifBridge(virDomainNetDefPtr net, char *bridge) +xenParseVifBridge(virDomainNetDefPtr net, const char *bridge) { char *vlanstr; unsigned int tag; @@ -1141,104 +1141,53 @@ 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; + const char *script =3D NULL; + const char *model =3D NULL; + const char *type =3D NULL; + const char *ip =3D NULL; + const char *mac =3D NULL; + const char *bridge =3D NULL; + const char *vifname =3D NULL; + const 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; - } + mac =3D 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; - } + bridge =3D val; } else if (STRPREFIX(key, "script=3D")) { - int len =3D nextkey ? (nextkey - data) : strlen(data); - VIR_FREE(script); - script =3D g_strndup(data, len); + script =3D 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; - } + model =3D 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; - } + type =3D 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; - } + vifname =3D 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; - } + ip =3D 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; - } + rate =3D 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 +1195,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 +1225,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 +1253,6 @@ xenParseVif(char *entry, const char *vif_typename) cleanup: virDomainNetDefFree(net); - VIR_FREE(script); return ret; } --=20 2.29.2