]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
conf: Teach virDomainParseMemory() new retval
authorMichal Privoznik <mprivozn@redhat.com>
Fri, 23 Jan 2026 09:09:15 +0000 (10:09 +0100)
committerMichal Privoznik <mprivozn@redhat.com>
Fri, 6 Feb 2026 15:13:27 +0000 (16:13 +0100)
So far, virDomainParseMemory() returns either 0 or -1. While this
allows callers to distinguish a success case from an error it
doesn't allow them to differentiate the case when no value was
provided in the XML, thus nothing was parsed and nothing was
required. Therefore, make virDomainParseMemory() return 1 on
success, 0 in case nothing was parsed and nothing was required,
and -1 on failure.

Arguably, no caller needs this distinction currently, but that is
about to change.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
src/conf/domain_conf.c

index 02e23f78667a775637c710b651ba5fc7a127226f..7e4de6d1f9b9f666a18cf0bd80d66552cdb94a94 100644 (file)
@@ -8729,7 +8729,9 @@ virDomainDiskDefParseXML(virDomainXMLOption *xmlopt,
  * if @capped is true, the value must fit within an unsigned long
  * (only matters on 32-bit platforms).
  *
- * Return 0 on success, -1 on failure after issuing error.
+ * Returns: 1 if value was parsed successfully,
+ *          0 if value wasn't present and @required is false,
+ *         -1 on failure after issuing error.
  */
 int
 virDomainParseMemory(const char *xpath,
@@ -8740,21 +8742,27 @@ virDomainParseMemory(const char *xpath,
                      bool capped)
 {
     unsigned long long bytes, max;
+    int rc;
 
     max = virMemoryMaxValue(capped);
 
-    if (virParseScaledValue(xpath, units_xpath, ctxt,
-                            &bytes, 1024, max, required) < 0)
+    rc = virParseScaledValue(xpath, units_xpath, ctxt,
+                             &bytes, 1024, max, required);
+    if (rc < 0) {
         return -1;
+    } else if (rc == 0) {
+        *mem = 0;
+        return 0;
+    }
 
-    /* Yes, we really do use kibibytes for our internal sizing.  */
+    /* Yes, we really do use kibibytes for our internal sizing. */
     *mem = VIR_DIV_UP(bytes, 1024);
 
     if (*mem >= VIR_DIV_UP(max, 1024)) {
         virReportError(VIR_ERR_OVERFLOW, "%s", _("size value too large"));
         return -1;
     }
-    return 0;
+    return 1;
 }