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

I have some code (below) which segfaults because xmlXPathEvalExpression

  (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



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



static int
libvirt_selinux_label (guestfs_h *g, xmlDocPtr doc,
                       char **label_rtn, char **imagelabel_rtn)
  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;



<domain type='kvm' id='151'>
  <memory unit='KiB'>2097152</memory>
  <currentMemory unit='KiB'>2097152</currentMemory>
  <vcpu placement='static'>1</vcpu>
    <type arch='x86_64' machine='pc-1.2'>hvm</type>
    <boot dev='hd'/>
  <cpu mode='custom' match='exact'>
    <model fallback='allow'>SandyBridge</model>
    <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'/>
  <clock offset='utc'/>
    <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 type='block' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <target dev='hdc' bus='ide'/>
      <alias name='ide0-1-0'/>
      <address type='drive' controller='0' bus='1' target='0' unit='0'/>
    <controller type='usb' index='0'>
      <alias name='usb0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>
    <controller type='ide' index='0'>
      <alias name='ide0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
    <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'/>
    <serial type='pty'>
      <source path='/dev/pts/16'/>
      <target port='0'/>
      <alias name='serial0'/>
    <console type='pty' tty='/dev/pts/16'>
      <source path='/dev/pts/16'/>
      <target type='serial' port='0'/>
      <alias name='serial0'/>
    <input type='tablet' bus='usb'>
      <alias name='input0'/>
    <input type='mouse' bus='ps2'/>
    <graphics type='vnc' port='5900' autoport='yes' listen=''>
      <listen type='address' address=''/>
      <model type='cirrus' vram='9216' heads='1'/>
      <alias name='video0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
    <memballoon model='virtio'>
      <alias name='balloon0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
  <seclabel type='dynamic' model='selinux' relabel='yes'>

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]