From nobody Tue Dec 16 08:04:20 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 15028872572989.070412321896015; Wed, 16 Aug 2017 05:40:57 -0700 (PDT) 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 mx1.redhat.com (Postfix) with ESMTPS id 5BC1A4022C; Wed, 16 Aug 2017 12:40:55 +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 F16C36FE4D; Wed, 16 Aug 2017 12:40:54 +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 A6CE03FACE; Wed, 16 Aug 2017 12:40:52 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v7GCepxa021693 for ; Wed, 16 Aug 2017 08:40:51 -0400 Received: by smtp.corp.redhat.com (Postfix) id 9CFFA7DB2D; Wed, 16 Aug 2017 12:40:51 +0000 (UTC) Received: from antique-work.brq.redhat.com (unknown [10.43.2.152]) by smtp.corp.redhat.com (Postfix) with ESMTP id 22CA67DB20 for ; Wed, 16 Aug 2017 12:40:50 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 5BC1A4022C Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=libvir-list-bounces@redhat.com From: Pavel Hrdina To: libvir-list@redhat.com Date: Wed, 16 Aug 2017 14:40:38 +0200 Message-Id: In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 01/10] util: introduce virXMLPropStringLimit 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: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Wed, 16 Aug 2017 12:40:55 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" The virXMLPropStringLimit is an equivalent of virXPathStringLimit which should be preferred if you already have a XML dom node or if you need to parse more than one property. Signed-off-by: Pavel Hrdina --- src/libvirt_private.syms | 1 + src/util/virxml.c | 52 +++++++++++++++++++++++++++++++++++++++-----= ---- src/util/virxml.h | 3 +++ 3 files changed, 47 insertions(+), 9 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 6e4c3e83b9..7646998aef 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2939,6 +2939,7 @@ virXMLNodeToString; virXMLParseHelper; virXMLPickShellSafeComment; virXMLPropString; +virXMLPropStringLimit; virXMLSaveFile; virXMLValidateAgainstSchema; virXMLValidatorFree; diff --git a/src/util/virxml.c b/src/util/virxml.c index b42358a08c..2904fc16c9 100644 --- a/src/util/virxml.c +++ b/src/util/virxml.c @@ -92,6 +92,24 @@ virXPathString(const char *xpath, return ret; } =20 + +static char * +virXMLStringLimitInternal(char *value, + size_t maxlen, + const char *name) +{ + if (value !=3D NULL && strlen(value) >=3D maxlen) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("'%s' value longer than '%zu' bytes"), + name, maxlen); + VIR_FREE(value); + return NULL; + } + + return value; +} + + /** * virXPathStringLimit: * @xpath: the XPath string to evaluate @@ -111,15 +129,7 @@ virXPathStringLimit(const char *xpath, { char *tmp =3D virXPathString(xpath, ctxt); =20 - if (tmp !=3D NULL && strlen(tmp) >=3D maxlen) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("\'%s\' value longer than %zu bytes"), - xpath, maxlen); - VIR_FREE(tmp); - return NULL; - } - - return tmp; + return virXMLStringLimitInternal(tmp, maxlen, xpath); } =20 /** @@ -506,6 +516,30 @@ virXMLPropString(xmlNodePtr node, return (char *)xmlGetProp(node, BAD_CAST name); } =20 + +/** + * virXMLPropStringLimit: + * @node: XML dom node pointer + * @name: Name of the property (attribute) to get + * @maxlen: maximum length permitted string + * + * Wrapper for virXMLPropString, which validates the length of the returned + * string. + * + * Returns a new string which must be deallocated by the caller or NULL if + * the evaluation failed. + */ +char * +virXMLPropStringLimit(xmlNodePtr node, + const char *name, + size_t maxlen) +{ + char *tmp =3D (char *)xmlGetProp(node, BAD_CAST name); + + return virXMLStringLimitInternal(tmp, maxlen, name); +} + + /** * virXPathBoolean: * @xpath: the XPath string to evaluate diff --git a/src/util/virxml.h b/src/util/virxml.h index 2f953a6d44..1ecc6b0a61 100644 --- a/src/util/virxml.h +++ b/src/util/virxml.h @@ -73,6 +73,9 @@ int virXPathNodeSet(const char *xpath, xmlNodePtr **list); char * virXMLPropString(xmlNodePtr node, const char *name); +char * virXMLPropStringLimit(xmlNodePtr node, + const char *name, + size_t maxlen); long virXMLChildElementCount(xmlNodePtr node); =20 /* Internal function; prefer the macros below. */ --=20 2.13.5 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list