From: Michal Privoznik <[email protected]>

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 <[email protected]>
---
 src/conf/domain_conf.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 9672168df9..1b2a439ca4 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -8719,7 +8719,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,
@@ -8730,21 +8732,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;
 }
 
 
-- 
2.52.0

Reply via email to