[xml] What does it mean if I get an XML_NODESET with nodesetval == NULL?



I have some code (below) which segfaults because xmlXPathEvalExpression
returns:

  (gdb) print *labelObj
  $1 = {type = XPATH_NODESET, nodesetval = 0x0, boolval = 0, floatval = 0, 
    stringval = 0x0, user = 0x0, index = 0, user2 = 0x0, index2 = 0}

I have previously thought it was impossible for xmlXPathEvalExpression
to return something != NULL but with nodesetval == NULL, but obviously
it can do this.

But what does it mean?

It doesn't seem to mean "no nodes", because when I use the standalone
'xpath' program (which AIUI uses libxml2) it returns a single matching
node.

Rich.

libxml2-2.9.0-3.fc18.x86_64

XPath expr:  /domain/seclabel[ model='selinux']/label

----------------------------------------------------------------------

Code:

static int
libvirt_selinux_label (guestfs_h *g, xmlDocPtr doc,
                       char **label_rtn, char **imagelabel_rtn)
{
  CLEANUP_XMLXPATHFREECONTEXT xmlXPathContextPtr xpathCtx = NULL;
  CLEANUP_XMLXPATHFREEOBJECT xmlXPathObjectPtr labelObj = NULL;
  CLEANUP_XMLXPATHFREEOBJECT xmlXPathObjectPtr imagelabelObj = NULL;
  xmlNodeSetPtr nodes;
  size_t nr_nodes;
  xmlNodePtr node;

  xpathCtx = xmlXPathNewContext (doc);
  if (xpathCtx == NULL) {
    error (g, _("unable to create new XPath context"));
    return -1;
  }

  const char *label_xpath =
    "/domain/seclabel[ model='selinux']/label";
  const char *imagelabel_xpath =
    "/domain/seclabel[ model='selinux']/imagelabel";

  labelObj = xmlXPathEvalExpression (BAD_CAST label_xpath, xpathCtx);
  if (labelObj == NULL) {
    error (g, _("unable to evaluate XPath expression"));
    return -1;
  }

  /* Process <label> */
  nodes = labelObj->nodesetval;
  nr_nodes = nodes->nodeNr;           /**** segfault here ****/
  if (nr_nodes == 0)
    return 0;
  if (nr_nodes > 1) {
    debug (g, _("ignoring multiple nodes matching '%s'"), label_xpath);
    return 0;
  }
  node = nodes->nodeTab[0];
  if (node->type != XML_ELEMENT_NODE) {
    error (g, _("expected <label/> to be an XML element"));
    return -1;
  }

  *label_rtn = (char *) xmlNodeGetContent (node);

  /* Same as above for <imagelabel> */
  imagelabelObj = xmlXPathEvalExpression (BAD_CAST imagelabel_xpath, xpathCtx);
  if (imagelabelObj == NULL) {
    error (g, _("unable to evaluate XPath expression"));
    return -1;
  }

  nodes = imagelabelObj->nodesetval;
  nr_nodes = nodes->nodeNr;
  if (nr_nodes == 0)
    return 0;
  if (nr_nodes > 1) {
    debug (g, _("ignoring multiple nodes matching '%s'"), imagelabel_xpath);
    return 0;
  }
  node = nodes->nodeTab[0];
  if (node->type != XML_ELEMENT_NODE) {
    error (g, _("expected <imagelabel/> to be an XML element"));
    return -1;
  }

  *imagelabel_rtn = (char *) xmlNodeGetContent (node);

  return 0;
}

----------------------------------------------------------------------

XML:

<domain type='kvm' id='151'>
  <name>f19rawhidex64</name>
  <uuid>2697b7dd-4bcc-c828-2fc7-8a717f19ee80</uuid>
  <memory unit='KiB'>2097152</memory>
  <currentMemory unit='KiB'>2097152</currentMemory>
  <vcpu placement='static'>1</vcpu>
  <os>
    <type arch='x86_64' machine='pc-1.2'>hvm</type>
    <boot dev='hd'/>
  </os>
  <features>
    <acpi/>
    <apic/>
    <pae/>
  </features>
  <cpu mode='custom' match='exact'>
    <model fallback='allow'>SandyBridge</model>
    <vendor>Intel</vendor>
    <feature policy='require' name='tm2'/>
    <feature policy='require' name='est'/>
    <feature policy='require' name='monitor'/>
    <feature policy='require' name='osxsave'/>
    <feature policy='require' name='smx'/>
    <feature policy='require' name='ss'/>
    <feature policy='require' name='vme'/>
    <feature policy='require' name='dtes64'/>
    <feature policy='require' name='ht'/>
    <feature policy='require' name='ds'/>
    <feature policy='require' name='pbe'/>
    <feature policy='require' name='tm'/>
    <feature policy='require' name='pdcm'/>
    <feature policy='require' name='vmx'/>
    <feature policy='require' name='ds_cpl'/>
    <feature policy='require' name='xtpr'/>
    <feature policy='require' name='acpi'/>
  </cpu>
  <clock offset='utc'/>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>restart</on_crash>
  <devices>
    <emulator>/usr/bin/qemu-system-x86_64</emulator>
    <disk type='block' device='disk'>
      <driver name='qemu' type='raw'/>
      <source dev='/dev/fedora/f19rawhidex64'/>
      <target dev='vda' bus='virtio'/>
      <alias name='virtio-disk0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
    </disk>
    <disk type='block' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <target dev='hdc' bus='ide'/>
      <readonly/>
      <alias name='ide0-1-0'/>
      <address type='drive' controller='0' bus='1' target='0' unit='0'/>
    </disk>
    <controller type='usb' index='0'>
      <alias name='usb0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>
    </controller>
    <controller type='ide' index='0'>
      <alias name='ide0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
    </controller>
    <interface type='network'>
      <mac address='52:54:00:a8:ba:f0'/>
      <source network='default'/>
      <target dev='vnet0'/>
      <model type='virtio'/>
      <alias name='net0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </interface>
    <serial type='pty'>
      <source path='/dev/pts/16'/>
      <target port='0'/>
      <alias name='serial0'/>
    </serial>
    <console type='pty' tty='/dev/pts/16'>
      <source path='/dev/pts/16'/>
      <target type='serial' port='0'/>
      <alias name='serial0'/>
    </console>
    <input type='tablet' bus='usb'>
      <alias name='input0'/>
    </input>
    <input type='mouse' bus='ps2'/>
    <graphics type='vnc' port='5900' autoport='yes' listen='127.0.0.1'>
      <listen type='address' address='127.0.0.1'/>
    </graphics>
    <video>
      <model type='cirrus' vram='9216' heads='1'/>
      <alias name='video0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
    </video>
    <memballoon model='virtio'>
      <alias name='balloon0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
    </memballoon>
  </devices>
  <seclabel type='dynamic' model='selinux' relabel='yes'>
    <label>system_u:system_r:svirt_t:s0:c117,c281</label>
    <imagelabel>system_u:object_r:svirt_image_t:s0:c117,c281</imagelabel>
  </seclabel>
</domain>



-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
libguestfs lets you edit virtual machines.  Supports shell scripting,
bindings from many languages.  http://libguestfs.org


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]