]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: xml: Reimplement virXPath(U)Int via virXPathEvalString
authorPeter Krempa <pkrempa@redhat.com>
Wed, 5 Oct 2022 11:04:17 +0000 (13:04 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Wed, 2 Nov 2022 08:20:57 +0000 (09:20 +0100)
Similarly to the refactor of virXPath(U)LongLong drop the ability to
convert from the internal double value forcing the use of the 'string()'
conversion.

In case of 32 bit integers there's no problem with overflows, but we can
implement the code identically to what we have in the other helpers.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
src/util/virxml.c

index 067fef8856ccf9dde579eeed85b45f0497749e24..d885fc16807883adf867559a88ea02e079b6ee78 100644 (file)
@@ -149,7 +149,9 @@ virXPathLongBase(const char *xpath,
  * @ctxt: an XPath context
  * @value: the returned int value
  *
- * Convenience function to evaluate an XPath number
+ * Convenience function to evaluate an XPath number. The @xpath expression
+ * must ensure that the evaluated value is returned as a string (use the
+ * 'string()' conversion in the expression).
  *
  * Returns 0 in case of success in which case @value is set,
  *         or -1 if the XPath evaluation failed or -2 if the
@@ -160,15 +162,14 @@ virXPathInt(const char *xpath,
             xmlXPathContextPtr ctxt,
             int *value)
 {
-    long tmp;
-    int ret;
+    g_autoptr(xmlXPathObject) obj = NULL;
 
-    ret = virXPathLongBase(xpath, ctxt, 10, &tmp);
-    if (ret < 0)
-        return ret;
-    if ((int) tmp != tmp)
+    if (!(obj = virXPathEvalString(xpath, ctxt)))
+        return -1;
+
+    if (virStrToLong_i((char *) obj->stringval, NULL, 10, value) < 0)
         return -2;
-    *value = tmp;
+
     return 0;
 }
 
@@ -230,28 +231,29 @@ virXPathULongBase(const char *xpath,
  * virXPathUInt:
  * @xpath: the XPath string to evaluate
  * @ctxt: an XPath context
- * @value: the returned int value
+ * @value: the returned unsigned int value
  *
- * Convenience function to evaluate an XPath number
+ * Convenience function to evaluate an XPath number. The @xpath expression
+ * must ensure that the evaluated value is returned as a string (use the
+ * 'string()' conversion in the expression).
  *
  * Returns 0 in case of success in which case @value is set,
  *         or -1 if the XPath evaluation failed or -2 if the
- *         value doesn't have an int format.
+ *         value doesn't have an unsigned int format.
  */
 int
 virXPathUInt(const char *xpath,
              xmlXPathContextPtr ctxt,
              unsigned int *value)
 {
-    unsigned long tmp;
-    int ret;
+    g_autoptr(xmlXPathObject) obj = NULL;
 
-    ret = virXPathULongBase(xpath, ctxt, 10, &tmp);
-    if (ret < 0)
-        return ret;
-    if ((unsigned int) tmp != tmp)
+    if (!(obj = virXPathEvalString(xpath, ctxt)))
+        return -1;
+
+    if (virStrToLong_ui((char *) obj->stringval, NULL, 10, value) < 0)
         return -2;
-    *value = tmp;
+
     return 0;
 }