virshFindDisk: Sanitize use of 'tmp' variable

The return value of virXMLPropString was assigned into 'tmp' multiple
times and to prevent static analyzers moaning about a potential leak a
short-circuited if logic or was used.

Replace the code by having a helper variable for each possibility and
also replace the for-loop to iterate elements.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2022-10-19 13:59:17 +02:00
parent 1a136152e6
commit 22766a1a53

View File

@ -12642,7 +12642,6 @@ virshFindDisk(const char *doc,
g_autoptr(xmlXPathContext) ctxt = NULL; g_autoptr(xmlXPathContext) ctxt = NULL;
g_autofree xmlNodePtr *nodes = NULL; g_autofree xmlNodePtr *nodes = NULL;
ssize_t nnodes; ssize_t nnodes;
xmlNodePtr cur = NULL;
size_t i; size_t i;
xml = virXMLParseStringCtxt(doc, _("(domain_definition)"), &ctxt); xml = virXMLParseStringCtxt(doc, _("(domain_definition)"), &ctxt);
@ -12658,6 +12657,13 @@ virshFindDisk(const char *doc,
/* search disk using @path */ /* search disk using @path */
for (i = 0; i < nnodes; i++) { for (i = 0; i < nnodes; i++) {
xmlNodePtr sourceNode;
g_autofree char *sourceFile = NULL;
g_autofree char *sourceDev = NULL;
g_autofree char *sourceDir = NULL;
g_autofree char *sourceName = NULL;
g_autofree char *targetDev = NULL;
if (type == VIRSH_FIND_DISK_CHANGEABLE) { if (type == VIRSH_FIND_DISK_CHANGEABLE) {
g_autofree char *device = virXMLPropString(nodes[i], "device"); g_autofree char *device = virXMLPropString(nodes[i], "device");
@ -12668,29 +12674,25 @@ virshFindDisk(const char *doc,
continue; continue;
} }
cur = nodes[i]->children; if ((sourceNode = virXMLNodeGetSubelement(nodes[i], "source"))) {
while (cur != NULL) { sourceFile = virXMLPropString(sourceNode, "file");
if (cur->type == XML_ELEMENT_NODE) { sourceDev = virXMLPropString(sourceNode, "dev");
g_autofree char *tmp = NULL; sourceDir = virXMLPropString(sourceNode, "dir");
sourceName = virXMLPropString(sourceNode, "name");
}
if (virXMLNodeNameEqual(cur, "source")) { ctxt->node = nodes[i];
if ((tmp = virXMLPropString(cur, "file")) || targetDev = virXPathString("string(./target/@dev)", ctxt);
(tmp = virXMLPropString(cur, "dev")) ||
(tmp = virXMLPropString(cur, "dir")) ||
(tmp = virXMLPropString(cur, "name"))) {
}
} else if (virXMLNodeNameEqual(cur, "target")) {
tmp = virXMLPropString(cur, "dev");
}
if (STREQ_NULLABLE(tmp, path)) { if (STREQ_NULLABLE(targetDev, path) ||
xmlNodePtr ret = xmlCopyNode(nodes[i], 1); STREQ_NULLABLE(sourceFile, path) ||
/* drop backing store since they are not needed here */ STREQ_NULLABLE(sourceDev, path) ||
virshDiskDropBackingStore(ret); STREQ_NULLABLE(sourceDir, path) ||
return ret; STREQ_NULLABLE(sourceName, path)) {
} xmlNodePtr ret = xmlCopyNode(nodes[i], 1);
} /* drop backing store since they are not needed here */
cur = cur->next; virshDiskDropBackingStore(ret);
return ret;
} }
} }