From nobody Sun Feb 8 07:15:06 2026 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=1685705327; cv=none; d=zohomail.com; s=zohoarc; b=WAfz1uFRAWuTetrbzCtsPQ4wz2qqGsAtbiUxhPT46ZGgdnS2LYJUIFDAYxCYEyXn7rUmGzJSR6lA94oFg38LCrFXQNl3PTSDszHyz6E6E56QuGLfbGgvpYFiDVcyn2aep7dz5LxtXawdf8mIOuTJfejA3LR7m6bNMRb1qmn0poM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1685705327; 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=8XEZ02P6U7QtUpTgUn6DYmK6KvFs/0uz7vlamaUVjKE=; b=TSle9GxEhbcOh6911WmQfeicu+ma/1n5ja1w8Fa8VFDFgQjHrQ01AZVs+mnT6RsZV/FTxqT9ZDEfW8GrM9X9WNjWvfYPRP/91QchI7VAtg6GykYz88+NQrGAwr/emkYYad77haEkp03wcwJQCodsIGEEWLezjTPBkmFvU39DcfY= 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 1685705327422820.7777959346354; Fri, 2 Jun 2023 04:28:47 -0700 (PDT) Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-456-Ic6auviiOhOGin_DYuUJmg-1; Fri, 02 Jun 2023 07:28:42 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id CDAAB811E7C; Fri, 2 Jun 2023 11:28:37 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (unknown [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id B733B40C6EC4; Fri, 2 Jun 2023 11:28:37 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 9C25019465A3; Fri, 2 Jun 2023 11:28:37 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 021DF194658C for ; Fri, 2 Jun 2023 11:28:37 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id E58688162; Fri, 2 Jun 2023 11:28:36 +0000 (UTC) Received: from speedmetal.lan (unknown [10.45.242.14]) by smtp.corp.redhat.com (Postfix) with ESMTP id 61C539E71 for ; Fri, 2 Jun 2023 11:28:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1685705326; 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=8XEZ02P6U7QtUpTgUn6DYmK6KvFs/0uz7vlamaUVjKE=; b=SkkZB6KunooHVj3G/MP5eLpGMjal8brViB8fc0ZekN4GCtNgJgOqHsKpTdlUl6W/wgG8KL /jTNbsGo/Rt7EMRz+ZNmZOHo+WOONYD7jJqNxf0eCWDrhQhgBqrmUcvN+5kgWYWkAsf/u+ wQQ/UjZ9YEGWZ5UVzxWUUM504z9Jr0g= X-MC-Unique: Ic6auviiOhOGin_DYuUJmg-1 X-Original-To: libvir-list@listman.corp.redhat.com From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 12/15] virNWFilterRuleParse: Refactor attribute parser Date: Fri, 2 Jun 2023 13:28:22 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1685705328478100005 Content-Type: text/plain; charset="utf-8" Use virXMLNodeGetSubelementList to get the elements to process. The new approach documents the complexity of the parser, which is designed to ignore unknown attributes and parse only a single kind of them after finding the first valid one. Note that the XML schema doesn't actually allow having multiple sub-elements, but I'm not sure how that translates to actual configs present. Signed-off-by: Peter Krempa --- src/conf/nwfilter_conf.c | 65 +++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 34 deletions(-) diff --git a/src/conf/nwfilter_conf.c b/src/conf/nwfilter_conf.c index b7400b553a..e6f7c0f8b7 100644 --- a/src/conf/nwfilter_conf.c +++ b/src/conf/nwfilter_conf.c @@ -2374,10 +2374,8 @@ static virNWFilterRuleDef * virNWFilterRuleParse(xmlNodePtr node) { g_autofree char *statematch =3D NULL; - bool found; - int found_i =3D 0; - - xmlNodePtr cur; + g_autofree xmlNodePtr *attrNodes =3D NULL; + size_t nattrNodes =3D 0; g_autoptr(virNWFilterRuleDef) ret =3D NULL; ret =3D g_new0(virNWFilterRuleDef, 1); @@ -2403,43 +2401,42 @@ virNWFilterRuleParse(xmlNodePtr node) (STREQ(statematch, "0") || STRCASEEQ(statematch, "false"))) ret->flags |=3D RULE_FLAG_NO_STATEMATCH; - cur =3D node->children; - - found =3D false; - - while (cur !=3D NULL) { - if (cur->type =3D=3D XML_ELEMENT_NODE) { - size_t i =3D 0; - while (1) { - if (found) - i =3D found_i; - - if (virXMLNodeNameEqual(cur, virAttr[i].id)) { + nattrNodes =3D virXMLNodeGetSubelementList(node, NULL, &attrNodes); - found_i =3D i; - found =3D true; - ret->prtclType =3D virAttr[i].prtclType; + if (nattrNodes > 0) { + size_t i; + size_t attr; - if (virNWFilterRuleDetailsParse(cur, - ret, - virAttr[i].att) < 0) { - return NULL; - } - if (virNWFilterRuleValidate(ret) < 0) - return NULL; + /* First we look up the type of the first valid element. The rest = of + * the parsing then only considers elements with same name. */ + for (i =3D 0; i < nattrNodes; i++) { + for (attr =3D 0; virAttr[attr].id; attr++) { + if (virXMLNodeNameEqual(attrNodes[i], virAttr[attr].id)) { + ret->prtclType =3D virAttr[attr].prtclType; break; } - if (!found) { - i++; - if (!virAttr[i].id) - break; - } else { - break; - } } + + /* if we've found the first correct element end the search */ + if (virAttr[attr].id) + break; + } + + /* parse the correct subelements now */ + for (i =3D 0; i < nattrNodes; i++) { + /* no valid elements */ + if (!virAttr[attr].id) + break; + + if (!virXMLNodeNameEqual(attrNodes[i], virAttr[attr].id)) + continue; + + if (virNWFilterRuleDetailsParse(attrNodes[i], ret, virAttr[att= r].att) < 0) + return NULL; } - cur =3D cur->next; + if (virNWFilterRuleValidate(ret) < 0) + return NULL; } virNWFilterRuleDefFixup(ret); --=20 2.40.1