From nobody Wed May 1 14:59:13 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) client-ip=205.139.110.120; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.120 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=1595278141; cv=none; d=zohomail.com; s=zohoarc; b=XkhuSe9bwhBZ1udRIKDDiPMuQNdCnWjmV1GLBGGmzNEM/4MyTjImr7kG62NbKlZWN4ihUobMPJFcyJgdSFJ2R/jAnAfHR7lfok5t2fjCJt+HOfhRdkJjba3D2fkC2Bzq5ynhCdXRKw7z1jnqXWOXWMvsQ4tCJJbtHGpDIOZnG0w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1595278141; 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=WjdMXSWSB/m9dkqfoZzId8Bl/NveE2QQdicCVmGMJGA=; b=QVGCnFM1TbYP0U42BKMO4veDYt7yIExPKtOLKDbki4ziRxHB5Y5GCCH6/R+yRmLn/OtsulQNlMHNUNbwZv/7GHxKoe/W8PZYIuVIjuOJKhn+Zb2uytIGOEPXflNWD+zaLxOuqwgFqcpPN98WGJaQoKBXq4g+h1wLfL5mbdnD5DA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.120 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-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.120]) by mx.zohomail.com with SMTPS id 1595278141911831.1050769881341; Mon, 20 Jul 2020 13:49:01 -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-400-q3b67DPnMJSf5mTBiwJxQg-1; Mon, 20 Jul 2020 16:48:58 -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 6F8EF1DF0; Mon, 20 Jul 2020 20:48:52 +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 551655BAD5; Mon, 20 Jul 2020 20:48:52 +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 5280A9A133; Mon, 20 Jul 2020 20:48:51 +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 06KKmpXv013473 for ; Mon, 20 Jul 2020 16:48:51 -0400 Received: by smtp.corp.redhat.com (Postfix) id 0DC5A1002397; Mon, 20 Jul 2020 20:48:51 +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 BADFD1002391 for ; Mon, 20 Jul 2020 20:48:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1595278140; 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=WjdMXSWSB/m9dkqfoZzId8Bl/NveE2QQdicCVmGMJGA=; b=XxXvWVGxSI+1hjaJ3+yKhcUR8Dkt7YE13KthlZ9E6m1Nnk0U8YVQcaPusuOXYhixGlrCmx G95IsnIisprmuwSXe8zLDKxEsob183v2Ur/hVss9IuzDfzaSdSKxOnEMR0GF6g/1D1xQml fxPCzmJHPzrHG2BUlM94l1r8V1/N1B4= X-MC-Unique: q3b67DPnMJSf5mTBiwJxQg-1 From: Laine Stump To: libvir-list@redhat.com Subject: [PATCH 1/5] conf: refactor virDomainBlkioDeviceParseXML to reduce calls to xmlNodeGetContent Date: Mon, 20 Jul 2020 16:48:39 -0400 Message-Id: <20200720204843.1245289-2-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" virDomainBlkioDeviceParseXML() calls xmlNodeGetContent() multiple times in a loop, but can easily be refactored to call it once for all element nodes, and then use the result of that one call in each of the (mutually exclusive) blocks that previously each had their own call to xmlNodeGetContent. This is being done in order to reduce the number of changes needed in an upcoming patch that will eliminate the lack of checking for NULL on return from xmlNodeGetContent(). As part of the simplification, the while() loop has been changed into a for() so that we can use "continue" without bypassing the "node =3D node->next". Signed-off-by: Laine Stump Change from V1: turned into for() loop and log error rather than ignoring NULL. Jano had suggested we might be able to set dev->path directly instead of using a temporary var, but doing that would require keeping the error: label and its cleanup of dev->path, rather than just relying on g_autofree. Signed-off-by: Laine Stump Reviewed-by: Michal Privoznik --- src/conf/domain_conf.c | 128 ++++++++++++++++++++++------------------- 1 file changed, 70 insertions(+), 58 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 7ecd2818b9..ade8c13914 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -1635,73 +1635,85 @@ virDomainBlkioDeviceParseXML(xmlNodePtr root, virBlkioDevicePtr dev) { xmlNodePtr node; - g_autofree char *c =3D NULL; - - node =3D root->children; - while (node) { - if (node->type =3D=3D XML_ELEMENT_NODE) { - if (virXMLNodeNameEqual(node, "path") && !dev->path) { - dev->path =3D (char *)xmlNodeGetContent(node); - } else if (virXMLNodeNameEqual(node, "weight")) { - c =3D (char *)xmlNodeGetContent(node); - if (virStrToLong_ui(c, NULL, 10, &dev->weight) < 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("could not parse weight %s"), - c); - goto error; - } - VIR_FREE(c); - } else if (virXMLNodeNameEqual(node, "read_bytes_sec")) { - c =3D (char *)xmlNodeGetContent(node); - if (virStrToLong_ull(c, NULL, 10, &dev->rbps) < 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("could not parse read bytes sec %s"), - c); - goto error; - } - VIR_FREE(c); - } else if (virXMLNodeNameEqual(node, "write_bytes_sec")) { - c =3D (char *)xmlNodeGetContent(node); - if (virStrToLong_ull(c, NULL, 10, &dev->wbps) < 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("could not parse write bytes sec %s"), - c); - goto error; - } - VIR_FREE(c); - } else if (virXMLNodeNameEqual(node, "read_iops_sec")) { - c =3D (char *)xmlNodeGetContent(node); - if (virStrToLong_ui(c, NULL, 10, &dev->riops) < 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("could not parse read iops sec %s"), - c); - goto error; - } - VIR_FREE(c); - } else if (virXMLNodeNameEqual(node, "write_iops_sec")) { - c =3D (char *)xmlNodeGetContent(node); - if (virStrToLong_ui(c, NULL, 10, &dev->wiops) < 0) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, - _("could not parse write iops sec %s"), - c); - goto error; - } - VIR_FREE(c); + g_autofree char *path =3D NULL; + + for (node =3D root->children; node !=3D NULL; node =3D node->next) { + g_autofree char *c =3D NULL; + + if (node->type !=3D XML_ELEMENT_NODE) + continue; + + c =3D (char *)xmlNodeGetContent(node); + + if (virXMLNodeNameEqual(node, "path")) { + /* To avoid the need for explicit cleanup on failure, + * don't set dev->path until we're assured of + * success. Until then, store it in an autofree pointer. + */ + if (!path) + path =3D g_steal_pointer(&c); + continue; + } + + if (virXMLNodeNameEqual(node, "weight")) { + if (virStrToLong_ui(c, NULL, 10, &dev->weight) < 0) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("could not parse weight %s"), + c); + return -1; } + continue; + } + + if (virXMLNodeNameEqual(node, "read_bytes_sec")) { + if (virStrToLong_ull(c, NULL, 10, &dev->rbps) < 0) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("could not parse read bytes sec %s"), + c); + return -1; + } + continue; + } + + if (virXMLNodeNameEqual(node, "write_bytes_sec")) { + if (virStrToLong_ull(c, NULL, 10, &dev->wbps) < 0) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("could not parse write bytes sec %s"), + c); + return -1; + } + continue; + } + + if (virXMLNodeNameEqual(node, "read_iops_sec")) { + if (virStrToLong_ui(c, NULL, 10, &dev->riops) < 0) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("could not parse read iops sec %s"), + c); + return -1; + } + continue; + } + + if (virXMLNodeNameEqual(node, "write_iops_sec")) { + if (virStrToLong_ui(c, NULL, 10, &dev->wiops) < 0) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("could not parse write iops sec %s"), + c); + return -1; + } + continue; } - node =3D node->next; } - if (!dev->path) { + + if (!path) { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", _("missing per-device path")); return -1; } =20 + dev->path =3D g_steal_pointer(&path); return 0; - - error: - VIR_FREE(dev->path); - return -1; } =20 =20 --=20 2.25.4 From nobody Wed May 1 14:59:13 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 207.211.31.120 as permitted sender) client-ip=207.211.31.120; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.120 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=1595278150; cv=none; d=zohomail.com; s=zohoarc; b=lj0ZkbAa9aGQQ2bkJgYy+n76zGwvD0kqdkokdycf66SH60TNIUvoMVWVyBAFz/Hqya0SlgIkmitVLOqEw9pzsBajXLFnvk55M6qzNd6smx4+D8NfSGaNNh7HRuhl0RqBnwXFTGbAa/qgM7EkImlYKjldG4/7T47RsTHTOcDvlMk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1595278150; 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=dHqWqGOl2Nl0o5cgim+R1zrV1kKyMieYGl8C1tm4NGQ=; b=HHiYgb7IWIs9NTYEGf+G9jKTaVHc+A1NmUHFHALoDY+iKEBnkLXHik8BOggS/V2qz7R0Hsz8qEoVHUYSJExmG5uu15ludk/lrt7qreDMJWUNgGhM7OKnDi1bMp7SqGv83SPLllcsONvgXYybhkUC//kblKHg8Yq/Dl2lXNWwd2k= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.120 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-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) by mx.zohomail.com with SMTPS id 1595278150396195.66222165632962; Mon, 20 Jul 2020 13:49:10 -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-395-Sf4Llzm5PQOrOS0g_9wvlQ-1; Mon, 20 Jul 2020 16:49:05 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 0386E100AA25; Mon, 20 Jul 2020 20:48:59 +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 D83717848C; Mon, 20 Jul 2020 20:48:58 +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 8D5039A153; Mon, 20 Jul 2020 20:48:58 +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 06KKmp6r013478 for ; Mon, 20 Jul 2020 16:48:51 -0400 Received: by smtp.corp.redhat.com (Postfix) id 85FA810021B3; Mon, 20 Jul 2020 20:48:51 +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 3EB551059127 for ; Mon, 20 Jul 2020 20:48:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1595278149; 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=dHqWqGOl2Nl0o5cgim+R1zrV1kKyMieYGl8C1tm4NGQ=; b=Ccm5digQMVsflolonjTRg4g0Ai1gEOg3XY+C8hxKZe7X+fGf5lExZ9LtD91ZuR87vv2gu/ CZ3iPHeXA4sExCUKNp1t/i83yirhMrxhMbMiDO3aUhrMSvjdxE/J6kXWQA9NYPfnST4fgh Arx2q/F9jPmF/J7ex1S8zA3sgNMzSbM= X-MC-Unique: Sf4Llzm5PQOrOS0g_9wvlQ-1 From: Laine Stump To: libvir-list@redhat.com Subject: [PATCH 2/5] util: replace all calls to xmlNodeGetContent with virXMLNodeContentString Date: Mon, 20 Jul 2020 16:48:40 -0400 Message-Id: <20200720204843.1245289-3-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.11 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" No functional change Signed-off-by: Laine Stump Reviewed-by: Michal Privoznik --- src/conf/domain_conf.c | 20 ++++++++++---------- src/conf/network_conf.c | 2 +- src/conf/node_device_conf.c | 4 ++-- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index ade8c13914..cb69c97a8e 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -1643,7 +1643,7 @@ virDomainBlkioDeviceParseXML(xmlNodePtr root, if (node->type !=3D XML_ELEMENT_NODE) continue; =20 - c =3D (char *)xmlNodeGetContent(node); + c =3D virXMLNodeContentString(node); =20 if (virXMLNodeNameEqual(node, "path")) { /* To avoid the need for explicit cleanup on failure, @@ -9373,10 +9373,10 @@ virDomainLeaseDefParseXML(xmlNodePtr node) while (cur !=3D NULL) { if (cur->type =3D=3D XML_ELEMENT_NODE) { if (!key && virXMLNodeNameEqual(cur, "key")) { - key =3D (char *)xmlNodeGetContent(cur); + key =3D virXMLNodeContentString(cur); } else if (!lockspace && virXMLNodeNameEqual(cur, "lockspace")) { - lockspace =3D (char *)xmlNodeGetContent(cur); + lockspace =3D virXMLNodeContentString(cur); } else if (!path && virXMLNodeNameEqual(cur, "target")) { path =3D virXMLPropString(cur, "path"); @@ -10595,16 +10595,16 @@ virDomainDiskDefParseXML(virDomainXMLOptionPtr xm= lopt, =20 } else if (!serial && virXMLNodeNameEqual(cur, "serial")) { - serial =3D (char *)xmlNodeGetContent(cur); + serial =3D virXMLNodeContentString(cur); } else if (!wwn && virXMLNodeNameEqual(cur, "wwn")) { - wwn =3D (char *)xmlNodeGetContent(cur); + wwn =3D virXMLNodeContentString(cur); =20 if (!virValidateWWN(wwn)) goto error; } else if (!vendor && virXMLNodeNameEqual(cur, "vendor")) { - vendor =3D (char *)xmlNodeGetContent(cur); + vendor =3D virXMLNodeContentString(cur); =20 if (strlen(vendor) > VENDOR_LEN) { virReportError(VIR_ERR_XML_ERROR, "%s", @@ -10619,7 +10619,7 @@ virDomainDiskDefParseXML(virDomainXMLOptionPtr xmlo= pt, } } else if (!product && virXMLNodeNameEqual(cur, "product")) { - product =3D (char *)xmlNodeGetContent(cur); + product =3D virXMLNodeContentString(cur); =20 if (strlen(product) > PRODUCT_LEN) { virReportError(VIR_ERR_XML_ERROR, "%s", @@ -13513,7 +13513,7 @@ virDomainSmartcardDefParseXML(virDomainXMLOptionPtr= xmlopt, "exactly three certificates")); goto error; } - def->data.cert.file[i] =3D (char *)xmlNodeGetContent(cur); + def->data.cert.file[i] =3D virXMLNodeContentString(cur); if (!def->data.cert.file[i]) { virReportOOMError(); goto error; @@ -13522,7 +13522,7 @@ virDomainSmartcardDefParseXML(virDomainXMLOptionPtr= xmlopt, } else if (cur->type =3D=3D XML_ELEMENT_NODE && virXMLNodeNameEqual(cur, "database") && !def->data.cert.database) { - def->data.cert.database =3D (char *)xmlNodeGetContent(cur); + def->data.cert.database =3D virXMLNodeContentString(cur); if (!def->data.cert.database) { virReportOOMError(); goto error; @@ -19875,7 +19875,7 @@ virDomainLoaderDefParseXML(xmlNodePtr node, if (!fwAutoSelect) { readonly_str =3D virXMLPropString(node, "readonly"); type_str =3D virXMLPropString(node, "type"); - loader->path =3D (char *) xmlNodeGetContent(node); + loader->path =3D virXMLNodeContentString(node); if (STREQ_NULLABLE(loader->path, "")) VIR_FREE(loader->path); } diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c index 0fd68a7d66..0a32f57188 100644 --- a/src/conf/network_conf.c +++ b/src/conf/network_conf.c @@ -720,7 +720,7 @@ virNetworkDNSHostDefParseXML(const char *networkName, if (cur->type =3D=3D XML_ELEMENT_NODE && virXMLNodeNameEqual(cur, "hostname")) { if (cur->children !=3D NULL) { - g_autofree char *name =3D (char *) xmlNodeGetContent(cur= ); + g_autofree char *name =3D virXMLNodeContentString(cur); =20 if (!name) { virReportError(VIR_ERR_XML_DETAIL, diff --git a/src/conf/node_device_conf.c b/src/conf/node_device_conf.c index c54015336a..7bbe709c5d 100644 --- a/src/conf/node_device_conf.c +++ b/src/conf/node_device_conf.c @@ -1988,10 +1988,10 @@ virNodeDeviceDefParseXML(xmlXPathContextPtr ctxt, =20 switch ((virNodeDevDevnodeType)val) { case VIR_NODE_DEV_DEVNODE_DEV: - def->devnode =3D (char*)xmlNodeGetContent(node); + def->devnode =3D virXMLNodeContentString(node); break; case VIR_NODE_DEV_DEVNODE_LINK: - def->devlinks[m++] =3D (char*)xmlNodeGetContent(node); + def->devlinks[m++] =3D virXMLNodeContentString(node); break; case VIR_NODE_DEV_DEVNODE_LAST: break; --=20 2.25.4 From nobody Wed May 1 14:59:13 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) client-ip=205.139.110.120; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.120 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=1595278149; cv=none; d=zohomail.com; s=zohoarc; b=Xn+PpFSO5wIkFNGdlMh2feW1ruDNxN8zOFym33WWtaPEojUkhV9F9WHUSCHYwp8oSK68mWZgmFUZqyKfdrhEgp5aqT6XV6vWwzJlOP0YE3eVkAN9OiSvsg0mXi+2WPXB4xWq+wbGaMLo7WhlAXXybxI2WbJzRWV3m258b/rNLKw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1595278149; 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=RSxNOrsE/fvIZ9aZFTbJQQNMPziT1qRcvG1QAgx+Z1U=; b=ZFEc9O6ycTrnDENxnobV6JN8STyIsAcPkTdW7QwqhNix7UbPShTtgBV9vT1Q8rHeaHHtLgiZtY9DFeBXKS+WuADmuIP3e22S5zFFXeuptUv5XTD0YcOBb0gJCfULYdW/80Hou19B8bDUym/1i4w4qEzIR/sDPIeURcXCopCfm34= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.120 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-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.120]) by mx.zohomail.com with SMTPS id 1595278149494574.1905466766228; Mon, 20 Jul 2020 13:49:09 -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-216-3xjkrtjzMQWrhUycSL7Jng-1; Mon, 20 Jul 2020 16:49:05 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 5608780046E; Mon, 20 Jul 2020 20:48:59 +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 CF4EB2E042; Mon, 20 Jul 2020 20:48:58 +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 8F0101809563; Mon, 20 Jul 2020 20:48:58 +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 06KKmqxt013485 for ; Mon, 20 Jul 2020 16:48:52 -0400 Received: by smtp.corp.redhat.com (Postfix) id 07E871002391; Mon, 20 Jul 2020 20:48:52 +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 B430A10021B3 for ; Mon, 20 Jul 2020 20:48:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1595278148; 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=RSxNOrsE/fvIZ9aZFTbJQQNMPziT1qRcvG1QAgx+Z1U=; b=Oz/alVZaMu+Vf9ahvn2/WN/GknaNcEzoQQnH/xvlYwSxc9KCdhCAeBXLJfVxIUGYI2vOoG X0FzZrTqL0V+mJqZRkE98sJz1r+1cvKHDWi6aO6nSewMZACU1rD+lhNBhPKYRQpLnbIFqn Pb8DDCIjkvSB1NtEi5AlgODJjEiPQxg= X-MC-Unique: 3xjkrtjzMQWrhUycSL7Jng-1 From: Laine Stump To: libvir-list@redhat.com Subject: [PATCH 3/5] util: log an error if virXMLNodeContentString will return NULL Date: Mon, 20 Jul 2020 16:48:41 -0400 Message-Id: <20200720204843.1245289-4-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.84 on 10.5.11.23 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" Many of our calls to xmlNodeGetContent() (which are now all via virXMLNodeContentString() are failing to check for a NULL return. We need to remedy that, but in order to make the remedy simpler, let's log an error in virXMLNodeContentString(), so that the callers don't all individually need to (since it would be the same error message for all of them anyway). Signed-off-by: Laine Stump Reviewed-by: Michal Privoznik --- src/util/virxml.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/util/virxml.c b/src/util/virxml.c index 27d22598ee..5315d4ff6f 100644 --- a/src/util/virxml.c +++ b/src/util/virxml.c @@ -538,7 +538,23 @@ virXMLPropStringLimit(xmlNodePtr node, char * virXMLNodeContentString(xmlNodePtr node) { - return (char *)xmlNodeGetContent(node); + char *ret =3D (char *)xmlNodeGetContent(node); + + if (node->type !=3D XML_ELEMENT_NODE) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("node '%s' has unexpected type %d"), + node->name, node->type); + return NULL; + } + + if (!ret) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("node '%s' has unexpected NULL content. This coul= d be caused by malformed input, or a memory allocation failure"), + node->name); + return NULL; + } + + return ret; } =20 =20 --=20 2.25.4 From nobody Wed May 1 14:59:13 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 205.139.110.61 as permitted sender) client-ip=205.139.110.61; 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 205.139.110.61 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=1595278152; cv=none; d=zohomail.com; s=zohoarc; b=lEiXkqUv2y3OyHKlDxqNVTUkUzfUBRtRasc/OTKiSbaNX1hl/OQRxqZCWADDPKXd4LG4Yzk1/dJfZ6zW0eP6JBiryjA+RhspzKaEJ8QEv3S1cuEqB7xRxsZjLbF1TBPr8ZLcDdug8JEgoDcKWpwnNKVrzKNOAkzYopznuQAbDKE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1595278152; 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=2hNOHVbyAWZp2tHdsxOWkWE5IENX/zp+w19ZHcsEurU=; b=ZL58L9KdvvoiZ0k0jasllxo7FgHh6vA4f+fyjzR6fz5er/5DVZLz3EqKqppRm+oeum20nXBHD3q717tcyen/DrHmsptYijMQcaWFGlk4HfGNzPhH0fOqcYc5zRknxinhirpuBn0n9Hwwz94QnxaPmkTsD/QPea0F1Vhy4O/Ebec= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.61 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-1.mimecast.com [205.139.110.61]) by mx.zohomail.com with SMTPS id 159527815287535.780900105912565; Mon, 20 Jul 2020 13:49:12 -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-114-rA4HKz3PNEW58qLxATXtGA-1; Mon, 20 Jul 2020 16:49:08 -0400 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 5382A800470; Mon, 20 Jul 2020 20:49:01 +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 338575D9D5; Mon, 20 Jul 2020 20:49: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 052609A15A; Mon, 20 Jul 2020 20:49:01 +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 06KKmq9e013492 for ; Mon, 20 Jul 2020 16:48:52 -0400 Received: by smtp.corp.redhat.com (Postfix) id 7EAAC10021B3; Mon, 20 Jul 2020 20:48:52 +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 36EC81002391 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=1595278151; 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=2hNOHVbyAWZp2tHdsxOWkWE5IENX/zp+w19ZHcsEurU=; b=fPArVtaqsE3nlhqkuxGrIChj8lMB6ScuMwMn/7NXiqEpmtjWpnIXZPA31kvBMrR9CjbN1s u9y0c68sU8wIMdsJSvVBD6XwVQX8ijfxXY/dQxw7Xla82TlQ8plrF8vVrjW1VkMUv4wc15 /wtlddYJyHE4spTI7xNdTnt6PgnfeeY= X-MC-Unique: rA4HKz3PNEW58qLxATXtGA-1 From: Laine Stump To: libvir-list@redhat.com Subject: [PATCH 4/5] treat all NULL returns from virXMLNodeContentString() as an error Date: Mon, 20 Jul 2020 16:48:42 -0400 Message-Id: <20200720204843.1245289-5-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.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" and stop erroneously equating NULL with "". The latter means that the element has empty content, while the former means there was an error during parsing (either internal with the parser, or the content of the XML was bad). Signed-off-by: Laine Stump Reviewed-by: Michal Privoznik --- src/conf/domain_conf.c | 68 ++++++++++++++++++++++--------------- src/conf/network_conf.c | 5 ++- src/conf/node_device_conf.c | 6 ++-- 3 files changed, 48 insertions(+), 31 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index cb69c97a8e..c377fd74aa 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -1643,7 +1643,8 @@ virDomainBlkioDeviceParseXML(xmlNodePtr root, if (node->type !=3D XML_ELEMENT_NODE) continue; =20 - c =3D virXMLNodeContentString(node); + if (!(c =3D virXMLNodeContentString(node))) + return -1; =20 if (virXMLNodeNameEqual(node, "path")) { /* To avoid the need for explicit cleanup on failure, @@ -9373,10 +9374,12 @@ virDomainLeaseDefParseXML(xmlNodePtr node) while (cur !=3D NULL) { if (cur->type =3D=3D XML_ELEMENT_NODE) { if (!key && virXMLNodeNameEqual(cur, "key")) { - key =3D virXMLNodeContentString(cur); + if (!(key =3D virXMLNodeContentString(cur))) + goto error; } else if (!lockspace && virXMLNodeNameEqual(cur, "lockspace")) { - lockspace =3D virXMLNodeContentString(cur); + if (!(lockspace =3D virXMLNodeContentString(cur))) + goto error; } else if (!path && virXMLNodeNameEqual(cur, "target")) { path =3D virXMLPropString(cur, "path"); @@ -10595,16 +10598,19 @@ virDomainDiskDefParseXML(virDomainXMLOptionPtr xm= lopt, =20 } else if (!serial && virXMLNodeNameEqual(cur, "serial")) { - serial =3D virXMLNodeContentString(cur); + if (!(serial =3D virXMLNodeContentString(cur))) + goto error; } else if (!wwn && virXMLNodeNameEqual(cur, "wwn")) { - wwn =3D virXMLNodeContentString(cur); + if (!(wwn =3D virXMLNodeContentString(cur))) + goto error; =20 if (!virValidateWWN(wwn)) goto error; } else if (!vendor && virXMLNodeNameEqual(cur, "vendor")) { - vendor =3D virXMLNodeContentString(cur); + if (!(vendor =3D virXMLNodeContentString(cur))) + goto error; =20 if (strlen(vendor) > VENDOR_LEN) { virReportError(VIR_ERR_XML_ERROR, "%s", @@ -10619,7 +10625,8 @@ virDomainDiskDefParseXML(virDomainXMLOptionPtr xmlo= pt, } } else if (!product && virXMLNodeNameEqual(cur, "product")) { - product =3D virXMLNodeContentString(cur); + if (!(product =3D virXMLNodeContentString(cur))) + goto error; =20 if (strlen(product) > PRODUCT_LEN) { virReportError(VIR_ERR_XML_ERROR, "%s", @@ -13513,20 +13520,16 @@ virDomainSmartcardDefParseXML(virDomainXMLOptionP= tr xmlopt, "exactly three certificates")); goto error; } - def->data.cert.file[i] =3D virXMLNodeContentString(cur); - if (!def->data.cert.file[i]) { - virReportOOMError(); + if (!(def->data.cert.file[i] =3D virXMLNodeContentString(c= ur))) goto error; - } + i++; } else if (cur->type =3D=3D XML_ELEMENT_NODE && virXMLNodeNameEqual(cur, "database") && !def->data.cert.database) { - def->data.cert.database =3D virXMLNodeContentString(cur); - if (!def->data.cert.database) { - virReportOOMError(); + if (!(def->data.cert.database =3D virXMLNodeContentString(= cur))) goto error; - } + if (*def->data.cert.database !=3D '/') { virReportError(VIR_ERR_XML_ERROR, _("expecting absolute path: %s"), @@ -15638,8 +15641,10 @@ virSysinfoOEMStringsParseXML(xmlNodePtr node, goto cleanup; =20 def->nvalues =3D nstrings; - for (i =3D 0; i < nstrings; i++) - def->values[i] =3D virXMLNodeContentString(strings[i]); + for (i =3D 0; i < nstrings; i++) { + if (!(def->values[i] =3D virXMLNodeContentString(strings[i]))) + goto cleanup; + } =20 *oem =3D g_steal_pointer(&def); ret =3D 0; @@ -15767,7 +15772,9 @@ virSysinfoParseFWCfgDef(virSysinfoDefPtr def, return -1; } =20 - value =3D virXMLNodeContentString(nodes[i]); + if (!(value =3D virXMLNodeContentString(nodes[i]))) + return -1; + file =3D virXMLPropString(nodes[i], "file"); =20 if (virStringIsEmpty(value)) @@ -19875,8 +19882,10 @@ virDomainLoaderDefParseXML(xmlNodePtr node, if (!fwAutoSelect) { readonly_str =3D virXMLPropString(node, "readonly"); type_str =3D virXMLPropString(node, "type"); - loader->path =3D virXMLNodeContentString(node); - if (STREQ_NULLABLE(loader->path, "")) + if (!(loader->path =3D virXMLNodeContentString(node))) + return -1; + + if (STREQ(loader->path, "")) VIR_FREE(loader->path); } =20 @@ -20093,14 +20102,15 @@ virDomainVcpuParse(virDomainDefPtr def, vcpus =3D maxvcpus =3D 1; =20 if ((vcpuNode =3D virXPathNode("./vcpu[1]", ctxt))) { - if ((tmp =3D virXMLNodeContentString(vcpuNode))) { - if (virStrToLong_ui(tmp, NULL, 10, &maxvcpus) < 0) { - virReportError(VIR_ERR_XML_ERROR, "%s", - _("maximum vcpus count must be an integer")= ); - return -1; - } - VIR_FREE(tmp); + if (!(tmp =3D virXMLNodeContentString(vcpuNode))) + return -1; + + if (virStrToLong_ui(tmp, NULL, 10, &maxvcpus) < 0) { + virReportError(VIR_ERR_XML_ERROR, "%s", + _("maximum vcpus count must be an integer")); + return -1; } + VIR_FREE(tmp); =20 if ((tmp =3D virXMLPropString(vcpuNode, "current"))) { if (virStrToLong_ui(tmp, NULL, 10, &vcpus) < 0) { @@ -20361,7 +20371,9 @@ virDomainDefParseBootOptions(virDomainDefPtr def, =20 if (STREQ_NULLABLE(tmp, "slic")) { VIR_FREE(tmp); - tmp =3D virXMLNodeContentString(nodes[0]); + if (!(tmp =3D virXMLNodeContentString(nodes[0]))) + return -1; + def->os.slic_table =3D virFileSanitizePath(tmp); } else { virReportError(VIR_ERR_XML_ERROR, diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c index 0a32f57188..54a723be64 100644 --- a/src/conf/network_conf.c +++ b/src/conf/network_conf.c @@ -722,7 +722,10 @@ virNetworkDNSHostDefParseXML(const char *networkName, if (cur->children !=3D NULL) { g_autofree char *name =3D virXMLNodeContentString(cur); =20 - if (!name) { + if (!name) + goto error; + + if (!name[0]) { virReportError(VIR_ERR_XML_DETAIL, _("Missing hostname in network '%s' D= NS HOST record"), networkName); diff --git a/src/conf/node_device_conf.c b/src/conf/node_device_conf.c index 7bbe709c5d..31c81a3b85 100644 --- a/src/conf/node_device_conf.c +++ b/src/conf/node_device_conf.c @@ -1988,10 +1988,12 @@ virNodeDeviceDefParseXML(xmlXPathContextPtr ctxt, =20 switch ((virNodeDevDevnodeType)val) { case VIR_NODE_DEV_DEVNODE_DEV: - def->devnode =3D virXMLNodeContentString(node); + if (!(def->devnode =3D virXMLNodeContentString(node))) + goto error; break; case VIR_NODE_DEV_DEVNODE_LINK: - def->devlinks[m++] =3D virXMLNodeContentString(node); + if (!(def->devlinks[m++] =3D virXMLNodeContentString(node))) + goto error; break; case VIR_NODE_DEV_DEVNODE_LAST: break; --=20 2.25.4 From nobody Wed May 1 14:59:13 2024 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 Reviewed-by: Michal Privoznik --- 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