root/lib/common/xpath.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. freeXpathObject
  2. getXpathResult
  3. dedupXpathResults
  4. xpath_search
  5. crm_foreach_xpath_result
  6. get_xpath_object_relative
  7. get_xpath_object
  8. pcmk__element_xpath
  9. pcmk__xpath_node_id
  10. xml_get_path

   1 /*
   2  * Copyright 2004-2022 the Pacemaker project contributors
   3  *
   4  * The version control history for this file may have further details.
   5  *
   6  * This source code is licensed under the GNU Lesser General Public License
   7  * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
   8  */
   9 
  10 #include <crm_internal.h>
  11 #include <stdio.h>
  12 #include <string.h>
  13 #include <crm/msg_xml.h>
  14 #include <crm/common/xml_internal.h>
  15 #include "crmcommon_private.h"
  16 
  17 /*
  18  * From xpath2.c
  19  *
  20  * All the elements returned by an XPath query are pointers to
  21  * elements from the tree *except* namespace nodes where the XPath
  22  * semantic is different from the implementation in libxml2 tree.
  23  * As a result when a returned node set is freed when
  24  * xmlXPathFreeObject() is called, that routine must check the
  25  * element type. But node from the returned set may have been removed
  26  * by xmlNodeSetContent() resulting in access to freed data.
  27  *
  28  * This can be exercised by running
  29  *       valgrind xpath2 test3.xml '//discarded' discarded
  30  *
  31  * There is 2 ways around it:
  32  *   - make a copy of the pointers to the nodes from the result set
  33  *     then call xmlXPathFreeObject() and then modify the nodes
  34  * or
  35  * - remove the references from the node set, if they are not
  36        namespace nodes, before calling xmlXPathFreeObject().
  37  */
  38 void
  39 freeXpathObject(xmlXPathObjectPtr xpathObj)
     /* [previous][next][first][last][top][bottom][index][help] */
  40 {
  41     int lpc, max = numXpathResults(xpathObj);
  42 
  43     if (xpathObj == NULL) {
  44         return;
  45     }
  46 
  47     for (lpc = 0; lpc < max; lpc++) {
  48         if (xpathObj->nodesetval->nodeTab[lpc] && xpathObj->nodesetval->nodeTab[lpc]->type != XML_NAMESPACE_DECL) {
  49             xpathObj->nodesetval->nodeTab[lpc] = NULL;
  50         }
  51     }
  52 
  53     /* _Now_ it's safe to free it */
  54     xmlXPathFreeObject(xpathObj);
  55 }
  56 
  57 xmlNode *
  58 getXpathResult(xmlXPathObjectPtr xpathObj, int index)
     /* [previous][next][first][last][top][bottom][index][help] */
  59 {
  60     xmlNode *match = NULL;
  61     int max = numXpathResults(xpathObj);
  62 
  63     CRM_CHECK(index >= 0, return NULL);
  64     CRM_CHECK(xpathObj != NULL, return NULL);
  65 
  66     if (index >= max) {
  67         crm_err("Requested index %d of only %d items", index, max);
  68         return NULL;
  69 
  70     } else if(xpathObj->nodesetval->nodeTab[index] == NULL) {
  71         /* Previously requested */
  72         return NULL;
  73     }
  74 
  75     match = xpathObj->nodesetval->nodeTab[index];
  76     CRM_CHECK(match != NULL, return NULL);
  77 
  78     if (xpathObj->nodesetval->nodeTab[index]->type != XML_NAMESPACE_DECL) {
  79         /* See the comment for freeXpathObject() */
  80         xpathObj->nodesetval->nodeTab[index] = NULL;
  81     }
  82 
  83     if (match->type == XML_DOCUMENT_NODE) {
  84         /* Will happen if section = '/' */
  85         match = match->children;
  86 
  87     } else if (match->type != XML_ELEMENT_NODE
  88                && match->parent && match->parent->type == XML_ELEMENT_NODE) {
  89         /* Return the parent instead */
  90         match = match->parent;
  91 
  92     } else if (match->type != XML_ELEMENT_NODE) {
  93         /* We only support searching nodes */
  94         crm_err("We only support %d not %d", XML_ELEMENT_NODE, match->type);
  95         match = NULL;
  96     }
  97     return match;
  98 }
  99 
 100 void
 101 dedupXpathResults(xmlXPathObjectPtr xpathObj)
     /* [previous][next][first][last][top][bottom][index][help] */
 102 {
 103     int lpc, max = numXpathResults(xpathObj);
 104 
 105     if (xpathObj == NULL) {
 106         return;
 107     }
 108 
 109     for (lpc = 0; lpc < max; lpc++) {
 110         xmlNode *xml = NULL;
 111         gboolean dedup = FALSE;
 112 
 113         if (xpathObj->nodesetval->nodeTab[lpc] == NULL) {
 114             continue;
 115         }
 116 
 117         xml = xpathObj->nodesetval->nodeTab[lpc]->parent;
 118 
 119         for (; xml; xml = xml->parent) {
 120             int lpc2 = 0;
 121 
 122             for (lpc2 = 0; lpc2 < max; lpc2++) {
 123                 if (xpathObj->nodesetval->nodeTab[lpc2] == xml) {
 124                     xpathObj->nodesetval->nodeTab[lpc] = NULL;
 125                     dedup = TRUE;
 126                     break;
 127                 }
 128             }
 129 
 130             if (dedup) {
 131                 break;
 132             }
 133         }
 134     }
 135 }
 136 
 137 /* the caller needs to check if the result contains a xmlDocPtr or xmlNodePtr */
 138 xmlXPathObjectPtr
 139 xpath_search(xmlNode * xml_top, const char *path)
     /* [previous][next][first][last][top][bottom][index][help] */
 140 {
 141     xmlDocPtr doc = NULL;
 142     xmlXPathObjectPtr xpathObj = NULL;
 143     xmlXPathContextPtr xpathCtx = NULL;
 144     const xmlChar *xpathExpr = (pcmkXmlStr) path;
 145 
 146     CRM_CHECK(path != NULL, return NULL);
 147     CRM_CHECK(xml_top != NULL, return NULL);
 148     CRM_CHECK(strlen(path) > 0, return NULL);
 149 
 150     doc = getDocPtr(xml_top);
 151 
 152     xpathCtx = xmlXPathNewContext(doc);
 153     CRM_ASSERT(xpathCtx != NULL);
 154 
 155     xpathObj = xmlXPathEvalExpression(xpathExpr, xpathCtx);
 156     xmlXPathFreeContext(xpathCtx);
 157     return xpathObj;
 158 }
 159 
 160 /*!
 161  * \brief Run a supplied function for each result of an xpath search
 162  *
 163  * \param[in,out] xml        XML to search
 164  * \param[in]     xpath      XPath search string
 165  * \param[in]     helper     Function to call for each result
 166  * \param[in,out] user_data  Data to pass to supplied function
 167  *
 168  * \note The helper function will be passed the XML node of the result,
 169  *       and the supplied user_data. This function does not otherwise
 170  *       use user_data.
 171  */
 172 void
 173 crm_foreach_xpath_result(xmlNode *xml, const char *xpath,
     /* [previous][next][first][last][top][bottom][index][help] */
 174                          void (*helper)(xmlNode*, void*), void *user_data)
 175 {
 176     xmlXPathObjectPtr xpathObj = xpath_search(xml, xpath);
 177     int nresults = numXpathResults(xpathObj);
 178     int i;
 179 
 180     for (i = 0; i < nresults; i++) {
 181         xmlNode *result = getXpathResult(xpathObj, i);
 182 
 183         CRM_LOG_ASSERT(result != NULL);
 184         if (result) {
 185             (*helper)(result, user_data);
 186         }
 187     }
 188     freeXpathObject(xpathObj);
 189 }
 190 
 191 xmlNode *
 192 get_xpath_object_relative(const char *xpath, xmlNode * xml_obj, int error_level)
     /* [previous][next][first][last][top][bottom][index][help] */
 193 {
 194     xmlNode *result = NULL;
 195     char *xpath_full = NULL;
 196     char *xpath_prefix = NULL;
 197 
 198     if (xml_obj == NULL || xpath == NULL) {
 199         return NULL;
 200     }
 201 
 202     xpath_prefix = (char *)xmlGetNodePath(xml_obj);
 203 
 204     xpath_full = crm_strdup_printf("%s%s", xpath_prefix, xpath);
 205 
 206     result = get_xpath_object(xpath_full, xml_obj, error_level);
 207 
 208     free(xpath_prefix);
 209     free(xpath_full);
 210     return result;
 211 }
 212 
 213 xmlNode *
 214 get_xpath_object(const char *xpath, xmlNode * xml_obj, int error_level)
     /* [previous][next][first][last][top][bottom][index][help] */
 215 {
 216     int max;
 217     xmlNode *result = NULL;
 218     xmlXPathObjectPtr xpathObj = NULL;
 219     char *nodePath = NULL;
 220     char *matchNodePath = NULL;
 221 
 222     if (xpath == NULL) {
 223         return xml_obj;         /* or return NULL? */
 224     }
 225 
 226     xpathObj = xpath_search(xml_obj, xpath);
 227     nodePath = (char *)xmlGetNodePath(xml_obj);
 228     max = numXpathResults(xpathObj);
 229 
 230     if (max < 1) {
 231         if (error_level < LOG_NEVER) {
 232             do_crm_log(error_level, "No match for %s in %s",
 233                        xpath, pcmk__s(nodePath, "unknown path"));
 234             crm_log_xml_explicit(xml_obj, "Unexpected Input");
 235         }
 236 
 237     } else if (max > 1) {
 238         if (error_level < LOG_NEVER) {
 239             int lpc = 0;
 240 
 241             do_crm_log(error_level, "Too many matches for %s in %s",
 242                        xpath, pcmk__s(nodePath, "unknown path"));
 243 
 244             for (lpc = 0; lpc < max; lpc++) {
 245                 xmlNode *match = getXpathResult(xpathObj, lpc);
 246 
 247                 CRM_LOG_ASSERT(match != NULL);
 248                 if (match != NULL) {
 249                     matchNodePath = (char *) xmlGetNodePath(match);
 250                     do_crm_log(error_level, "%s[%d] = %s",
 251                                xpath, lpc,
 252                                pcmk__s(matchNodePath, "unrecognizable match"));
 253                     free(matchNodePath);
 254                 }
 255             }
 256             crm_log_xml_explicit(xml_obj, "Bad Input");
 257         }
 258 
 259     } else {
 260         result = getXpathResult(xpathObj, 0);
 261     }
 262 
 263     freeXpathObject(xpathObj);
 264     free(nodePath);
 265 
 266     return result;
 267 }
 268 
 269 /*!
 270  * \internal
 271  * \brief Get an XPath string that matches an XML element as closely as possible
 272  *
 273  * \param[in] xml  The XML element for which to build an XPath string
 274  *
 275  * \return A \p GString that matches \p xml, or \p NULL if \p xml is \p NULL.
 276  *
 277  * \note The caller is responsible for freeing the string using
 278  *       \p g_string_free().
 279  */
 280 GString *
 281 pcmk__element_xpath(const xmlNode *xml)
     /* [previous][next][first][last][top][bottom][index][help] */
 282 {
 283     const xmlNode *parent = NULL;
 284     GString *xpath = NULL;
 285     const char *id = NULL;
 286 
 287     if (xml == NULL) {
 288         return NULL;
 289     }
 290 
 291     parent = xml->parent;
 292     xpath = pcmk__element_xpath(parent);
 293     if (xpath == NULL) {
 294         xpath = g_string_sized_new(256);
 295     }
 296 
 297     // Build xpath like "/" -> "/cib" -> "/cib/configuration"
 298     if (parent == NULL) {
 299         g_string_append_c(xpath, '/');
 300     } else if (parent->parent == NULL) {
 301         g_string_append(xpath, TYPE(xml));
 302     } else {
 303         pcmk__g_strcat(xpath, "/", TYPE(xml), NULL);
 304     }
 305 
 306     id = ID(xml);
 307     if (id != NULL) {
 308         pcmk__g_strcat(xpath, "[@" XML_ATTR_ID "='", id, "']", NULL);
 309     }
 310 
 311     return xpath;
 312 }
 313 
 314 char *
 315 pcmk__xpath_node_id(const char *xpath, const char *node)
     /* [previous][next][first][last][top][bottom][index][help] */
 316 {
 317     char *retval = NULL;
 318     char *patt = NULL;
 319     char *start = NULL;
 320     char *end = NULL;
 321 
 322     if (node == NULL || xpath == NULL) {
 323         return retval;
 324     }
 325 
 326     patt = crm_strdup_printf("/%s[@id=", node);
 327     start = strstr(xpath, patt);
 328 
 329     if (!start) {
 330         free(patt);
 331         return retval;
 332     }
 333 
 334     start += strlen(patt);
 335     start++;
 336 
 337     end = strstr(start, "\'");
 338     CRM_ASSERT(end);
 339     retval = strndup(start, end-start);
 340 
 341     free(patt);
 342     return retval;
 343 }
 344 
 345 // Deprecated functions kept only for backward API compatibility
 346 // LCOV_EXCL_START
 347 
 348 #include <crm/common/xml_compat.h>
 349 
 350 /*!
 351  * \deprecated This function will be removed in a future release
 352  * \brief Get an XPath string that matches an XML element as closely as possible
 353  *
 354  * \param[in] xml  The XML element for which to build an XPath string
 355  *
 356  * \return A string that matches \p xml, or \p NULL if \p xml is \p NULL.
 357  *
 358  * \note The caller is responsible for freeing the string using free().
 359  */
 360 char *
 361 xml_get_path(const xmlNode *xml)
     /* [previous][next][first][last][top][bottom][index][help] */
 362 {
 363     char *path = NULL;
 364     GString *g_path = pcmk__element_xpath(xml);
 365 
 366     if (g_path == NULL) {
 367         return NULL;
 368     }
 369 
 370     path = strdup((const char *) g_path->str);
 371     CRM_ASSERT(path != NULL);
 372 
 373     g_string_free(g_path, TRUE);
 374     return path;
 375 }
 376 
 377 // LCOV_EXCL_STOP
 378 // End deprecated API

/* [previous][next][first][last][top][bottom][index][help] */