From nobody Mon Feb 9 09:33:02 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) client-ip=207.211.31.81; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.81 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=1595278153; cv=none; d=zohomail.com; s=zohoarc; b=V5BBtfSDYyroTTuI3/KcrKR7MLRmi8MixI3xhx4xYge53YBt+w3ptGnkOx9eONykLhIt/scWmpwHAWXFFmn2m6173RBTuxfnj2MB2ix/9NbSgJNlsiRGLhpTc4i9aAEgVHjaIHgD+fn4mlzGIGe3AQzKo37M7AHepIqdlVrPETQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1595278153; 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=pI0CFWcXpXTpGJbbDgCEJ0G8APgkTYTWxlyQEID1nMU=; b=SBGOtRre61eNCxBJz0yIxXakhFgQWsjAaCaJEqR8UTNFfSKL3DmbPl+Thj7AS+0Mbh/uoKtvMLqNUTadKy3hh4Dyc//KKId+5R3sjf3rR9pWL/IxBLqgo0GR9IzBpcJ0ft3yu7d5pbL74BY36H5azr9vhsbyQkDIMM7rQlvioMM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.81 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-1.mimecast.com (us-smtp-2.mimecast.com [207.211.31.81]) by mx.zohomail.com with SMTPS id 1595278153362708.4181554312963; Mon, 20 Jul 2020 13:49:13 -0700 (PDT) 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-401-sFIWB_0hMn204sbiaiIBQw-1; Mon, 20 Jul 2020 16:49:09 -0400 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 92BB28018A1; Mon, 20 Jul 2020 20:49:03 +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 752307EF95; Mon, 20 Jul 2020 20:49:03 +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 4330A1809563; Mon, 20 Jul 2020 20:49:03 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 06KKmr2S013500 for ; Mon, 20 Jul 2020 16:48:53 -0400 Received: by smtp.corp.redhat.com (Postfix) id 0517C1002397; Mon, 20 Jul 2020 20:48:53 +0000 (UTC) Received: from vhost2.laine.org (ovpn-112-16.phx2.redhat.com [10.3.112.16]) by smtp.corp.redhat.com (Postfix) with ESMTP id AFC591002391 for ; Mon, 20 Jul 2020 20:48:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1595278152; 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=pI0CFWcXpXTpGJbbDgCEJ0G8APgkTYTWxlyQEID1nMU=; b=CtmIhxG+pJ0+sRMQtK3dTHwTgEgOzosORQP6MRVoXSaOrc3pv3evmomqn8h6MuNzvi2Ma/ UsN3W693WsI1zbom+TxqRBwByzOvwJIt1j46phiQbUavx+Stje+bBlEDIlXKZ/MnmInZpc NExTPc2rLhjL3oNhjCV+V4Fh16BsCRQ= X-MC-Unique: sFIWB_0hMn204sbiaiIBQw-1 From: Laine Stump To: libvir-list@redhat.com Subject: [FYI PATCH 5/5] util: open code virXMLNodeContentString to access the node object directly Date: Mon, 20 Jul 2020 16:48:43 -0400 Message-Id: <20200720204843.1245289-6-laine@redhat.com> In-Reply-To: <20200720204843.1245289-1-laine@redhat.com> References: <20200720204843.1245289-1-laine@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 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 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" (I am *NOT* advocating that we apply this patch. Just providing it for informational purposes, since we had previously discussed this possibility on the list) Since it's impossible to determine whether xmlNodeContent has returned a NULL due to OOM, or due to badly formed / evil XML, this patch open codes virXMLNodeContentString to get the content string directly from the node. This turns out to not be so easy as it seemed at first glance when it was suggested - the "content" member of the element node itself does not contain the content string for the node. The content string that we want can be found (at least for our uses of libxml) by looking for a child node of the element node - if that child node is of type XML_TEXT_NODE, then the content member of *that* node is the string we're looking for. If there is no child node, then the element has no content, so we return "". Likewise, if the child node is type XML_TEXT_NODE but has no content, we also return "". In all other cases, we log an error and return because this is some case that hasn't been encountered in our test cases, so either someone is sending bad XML, or our assumptions about the layout of the XML node object list are incorrect. Note that while calling virXMLNodeContentString() would return NULL from an OOM situation, this new code will exit the process on OOM (since it is calling glib for memory allocation). Signed-off-by: Laine Stump --- src/util/virxml.c | 43 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/src/util/virxml.c b/src/util/virxml.c index 5315d4ff6f..b2298d74c8 100644 --- a/src/util/virxml.c +++ b/src/util/virxml.c @@ -538,7 +538,17 @@ virXMLPropStringLimit(xmlNodePtr node, char * virXMLNodeContentString(xmlNodePtr node) { - char *ret =3D (char *)xmlNodeGetContent(node); + /* We specifically avoid using virXMLNodeContentString() here, because + * when NULL is returned, it is difficult/impossible to + * distinguish between 1) OOM, 2) NULL content, 3) some other error. + */ + + /* for elements used the way libvirt uses them, the xmlNode object + * for an element will have a type of XML_ELEMENT_NODE, and if the + * node has any content, it will be in the content field of a + * child node of that object which is itself of type + * XML_TEXT_NODE. + */ =20 if (node->type !=3D XML_ELEMENT_NODE) { virReportError(VIR_ERR_INTERNAL_ERROR, @@ -547,15 +557,38 @@ virXMLNodeContentString(xmlNodePtr node) return NULL; } =20 - if (!ret) { + /* no children --> empty element node */ + if (!node->children) + return g_strdup(""); + + /* if the child isn't text, or there is more than a single node + * hanging off "children", our assumptions have been wrong + */ + if (node->children->type !=3D XML_TEXT_NODE) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("child of element node '%s' has unexpected name '= %s', type %d"), + node->name, node->children->name, node->children->t= ype); + return NULL; + } + if (node->children->next) { virReportError(VIR_ERR_INTERNAL_ERROR, - _("node '%s' has unexpected NULL content. This coul= d be caused by malformed input, or a memory allocation failure"), + _("child of element node '%s' is type XML_TEXT_NODE= , but is a list"), + node->name); + return NULL; + } + if (node->children->children) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("child of element node '%s' is type XML_TEXT_NODE= , but has children"), node->name); return NULL; } =20 - return ret; -} + /* if content is NULL, return "" instead */ + if (!node->children->content) + return g_strdup(""); + + return g_strdup((char *)node->children->content); + } =20 =20 /** --=20 2.25.4