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-2023 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(const xmlNode *xml_top, const char *path)
     /* [previous][next][first][last][top][bottom][index][help] */
 140 {
 141     xmlXPathObjectPtr xpathObj = NULL;
 142     xmlXPathContextPtr xpathCtx = NULL;
 143     const xmlChar *xpathExpr = (pcmkXmlStr) path;
 144 
 145     CRM_CHECK(path != NULL, return NULL);
 146     CRM_CHECK(xml_top != NULL, return NULL);
 147     CRM_CHECK(strlen(path) > 0, return NULL);
 148 
 149     xpathCtx = xmlXPathNewContext(xml_top->doc);
 150     CRM_ASSERT(xpathCtx != NULL);
 151 
 152     xpathObj = xmlXPathEvalExpression(xpathExpr, xpathCtx);
 153     xmlXPathFreeContext(xpathCtx);
 154     return xpathObj;
 155 }
 156 
 157 /*!
 158  * \brief Run a supplied function for each result of an xpath search
 159  *
 160  * \param[in,out] xml        XML to search
 161  * \param[in]     xpath      XPath search string
 162  * \param[in]     helper     Function to call for each result
 163  * \param[in,out] user_data  Data to pass to supplied function
 164  *
 165  * \note The helper function will be passed the XML node of the result,
 166  *       and the supplied user_data. This function does not otherwise
 167  *       use user_data.
 168  */
 169 void
 170 crm_foreach_xpath_result(xmlNode *xml, const char *xpath,
     /* [previous][next][first][last][top][bottom][index][help] */
 171                          void (*helper)(xmlNode*, void*), void *user_data)
 172 {
 173     xmlXPathObjectPtr xpathObj = xpath_search(xml, xpath);
 174     int nresults = numXpathResults(xpathObj);
 175     int i;
 176 
 177     for (i = 0; i < nresults; i++) {
 178         xmlNode *result = getXpathResult(xpathObj, i);
 179 
 180         CRM_LOG_ASSERT(result != NULL);
 181         if (result) {
 182             (*helper)(result, user_data);
 183         }
 184     }
 185     freeXpathObject(xpathObj);
 186 }
 187 
 188 xmlNode *
 189 get_xpath_object_relative(const char *xpath, xmlNode * xml_obj, int error_level)
     /* [previous][next][first][last][top][bottom][index][help] */
 190 {
 191     xmlNode *result = NULL;
 192     char *xpath_full = NULL;
 193     char *xpath_prefix = NULL;
 194 
 195     if (xml_obj == NULL || xpath == NULL) {
 196         return NULL;
 197     }
 198 
 199     xpath_prefix = (char *)xmlGetNodePath(xml_obj);
 200 
 201     xpath_full = crm_strdup_printf("%s%s", xpath_prefix, xpath);
 202 
 203     result = get_xpath_object(xpath_full, xml_obj, error_level);
 204 
 205     free(xpath_prefix);
 206     free(xpath_full);
 207     return result;
 208 }
 209 
 210 xmlNode *
 211 get_xpath_object(const char *xpath, xmlNode * xml_obj, int error_level)
     /* [previous][next][first][last][top][bottom][index][help] */
 212 {
 213     int max;
 214     xmlNode *result = NULL;
 215     xmlXPathObjectPtr xpathObj = NULL;
 216     char *nodePath = NULL;
 217     char *matchNodePath = NULL;
 218 
 219     if (xpath == NULL) {
 220         return xml_obj;         /* or return NULL? */
 221     }
 222 
 223     xpathObj = xpath_search(xml_obj, xpath);
 224     nodePath = (char *)xmlGetNodePath(xml_obj);
 225     max = numXpathResults(xpathObj);
 226 
 227     if (max < 1) {
 228         if (error_level < LOG_NEVER) {
 229             do_crm_log(error_level, "No match for %s in %s",
 230                        xpath, pcmk__s(nodePath, "unknown path"));
 231             crm_log_xml_explicit(xml_obj, "Unexpected Input");
 232         }
 233 
 234     } else if (max > 1) {
 235         if (error_level < LOG_NEVER) {
 236             int lpc = 0;
 237 
 238             do_crm_log(error_level, "Too many matches for %s in %s",
 239                        xpath, pcmk__s(nodePath, "unknown path"));
 240 
 241             for (lpc = 0; lpc < max; lpc++) {
 242                 xmlNode *match = getXpathResult(xpathObj, lpc);
 243 
 244                 CRM_LOG_ASSERT(match != NULL);
 245                 if (match != NULL) {
 246                     matchNodePath = (char *) xmlGetNodePath(match);
 247                     do_crm_log(error_level, "%s[%d] = %s",
 248                                xpath, lpc,
 249                                pcmk__s(matchNodePath, "unrecognizable match"));
 250                     free(matchNodePath);
 251                 }
 252             }
 253             crm_log_xml_explicit(xml_obj, "Bad Input");
 254         }
 255 
 256     } else {
 257         result = getXpathResult(xpathObj, 0);
 258     }
 259 
 260     freeXpathObject(xpathObj);
 261     free(nodePath);
 262 
 263     return result;
 264 }
 265 
 266 /*!
 267  * \internal
 268  * \brief Get an XPath string that matches an XML element as closely as possible
 269  *
 270  * \param[in] xml  The XML element for which to build an XPath string
 271  *
 272  * \return A \p GString that matches \p xml, or \p NULL if \p xml is \p NULL.
 273  *
 274  * \note The caller is responsible for freeing the string using
 275  *       \p g_string_free().
 276  */
 277 GString *
 278 pcmk__element_xpath(const xmlNode *xml)
     /* [previous][next][first][last][top][bottom][index][help] */
 279 {
 280     const xmlNode *parent = NULL;
 281     GString *xpath = NULL;
 282     const char *id = NULL;
 283 
 284     if (xml == NULL) {
 285         return NULL;
 286     }
 287 
 288     parent = xml->parent;
 289     xpath = pcmk__element_xpath(parent);
 290     if (xpath == NULL) {
 291         xpath = g_string_sized_new(256);
 292     }
 293 
 294     // Build xpath like "/" -> "/cib" -> "/cib/configuration"
 295     if (parent == NULL) {
 296         g_string_append_c(xpath, '/');
 297     } else if (parent->parent == NULL) {
 298         g_string_append(xpath, (const gchar *) xml->name);
 299     } else {
 300         pcmk__g_strcat(xpath, "/", (const char *) xml->name, NULL);
 301     }
 302 
 303     id = ID(xml);
 304     if (id != NULL) {
 305         pcmk__g_strcat(xpath, "[@" XML_ATTR_ID "='", id, "']", NULL);
 306     }
 307 
 308     return xpath;
 309 }
 310 
 311 char *
 312 pcmk__xpath_node_id(const char *xpath, const char *node)
     /* [previous][next][first][last][top][bottom][index][help] */
 313 {
 314     char *retval = NULL;
 315     char *patt = NULL;
 316     char *start = NULL;
 317     char *end = NULL;
 318 
 319     if (node == NULL || xpath == NULL) {
 320         return retval;
 321     }
 322 
 323     patt = crm_strdup_printf("/%s[@" XML_ATTR_ID "=", node);
 324     start = strstr(xpath, patt);
 325 
 326     if (!start) {
 327         free(patt);
 328         return retval;
 329     }
 330 
 331     start += strlen(patt);
 332     start++;
 333 
 334     end = strstr(start, "\'");
 335     CRM_ASSERT(end);
 336     retval = strndup(start, end-start);
 337 
 338     free(patt);
 339     return retval;
 340 }
 341 
 342 // Deprecated functions kept only for backward API compatibility
 343 // LCOV_EXCL_START
 344 
 345 #include <crm/common/xml_compat.h>
 346 
 347 /*!
 348  * \deprecated This function will be removed in a future release
 349  * \brief Get an XPath string that matches an XML element as closely as possible
 350  *
 351  * \param[in] xml  The XML element for which to build an XPath string
 352  *
 353  * \return A string that matches \p xml, or \p NULL if \p xml is \p NULL.
 354  *
 355  * \note The caller is responsible for freeing the string using free().
 356  */
 357 char *
 358 xml_get_path(const xmlNode *xml)
     /* [previous][next][first][last][top][bottom][index][help] */
 359 {
 360     char *path = NULL;
 361     GString *g_path = pcmk__element_xpath(xml);
 362 
 363     if (g_path == NULL) {
 364         return NULL;
 365     }
 366 
 367     path = strdup((const char *) g_path->str);
 368     CRM_ASSERT(path != NULL);
 369 
 370     g_string_free(g_path, TRUE);
 371     return path;
 372 }
 373 
 374 // LCOV_EXCL_STOP
 375 // End deprecated API

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