diff --git a/src/common/librlist/rhwloc.c b/src/common/librlist/rhwloc.c index 45badacc7183..20f006e6ffc9 100644 --- a/src/common/librlist/rhwloc.c +++ b/src/common/librlist/rhwloc.c @@ -60,13 +60,24 @@ static int init_topo_from_xml (hwloc_topology_t *tp, const char *xml, unsigned long flags) { - if ((topo_init_common (tp, flags) < 0) - || (hwloc_topology_set_xmlbuffer (*tp, xml, strlen (xml)) < 0) - || (hwloc_topology_load (*tp) < 0)) { - hwloc_topology_destroy (*tp); - return (-1); + int len = strlen (xml) + 1; + + if (topo_init_common (tp, flags) < 0) + return -1; + if (hwloc_topology_set_xmlbuffer (*tp, xml, len) < 0) { + /* In some versions of hwloc/libxml, the NUL character on the XML + * buffer cannot be included in len. Therefore, if set_xmlbuffer fails + * above, retry with len-1. + */ + if (hwloc_topology_set_xmlbuffer (*tp, xml, len - 1) < 0) + goto error; } - return (0); + if (hwloc_topology_load (*tp) < 0) + goto error; + return 0; +error: + hwloc_topology_destroy (*tp); + return -1; } static int topo_restrict (hwloc_topology_t topo)