[PATCHv4 1/4] virsh: Reject negative numbers in vshCommandOptUInt

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Use virStrToLong_uip instead of virStrToLong_ui to reject negative
numbers in the helper. None of the callers expects the wraparound
"feature" for negative numbers.

Also add a function that allows wrapping of negative numbers as it might
be used in the future and be explicit about the new semantics in the
function docs.
---
 tools/virsh.c | 48 ++++++++++++++++++++++++++++++++++++++----------
 tools/virsh.h |  3 +++
 2 files changed, 41 insertions(+), 10 deletions(-)

diff --git a/tools/virsh.c b/tools/virsh.c
index 15f3025..cbab6b0 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -1494,6 +1494,28 @@ vshCommandOptInt(const vshCmd *cmd, const char *name, int *value)
     return 1;
 }

+static int
+vshCommandOptUIntInternal(const vshCmd *cmd,
+                          const char *name,
+                          unsigned int *value,
+                          bool wrap)
+{
+    vshCmdOpt *arg;
+    int ret;
+
+    if ((ret = vshCommandOpt(cmd, name, &arg, true)) <= 0)
+        return ret;
+
+    if (wrap) {
+        if (virStrToLong_ui(arg->data, NULL, 10, value) < 0)
+            return -1;
+    } else {
+        if (virStrToLong_uip(arg->data, NULL, 10, value) < 0)
+            return -1;
+    }
+
+    return 1;
+}

 /**
  * vshCommandOptUInt:
@@ -1501,22 +1523,28 @@ vshCommandOptInt(const vshCmd *cmd, const char *name, int *value)
  * @name option name
  * @value result
  *
- * Convert option to unsigned int
+ * Convert option to unsigned int, reject negative numbers
  * See vshCommandOptInt()
  */
 int
 vshCommandOptUInt(const vshCmd *cmd, const char *name, unsigned int *value)
 {
-    vshCmdOpt *arg;
-    int ret;
-
-    ret = vshCommandOpt(cmd, name, &arg, true);
-    if (ret <= 0)
-        return ret;
+    return vshCommandOptUIntInternal(cmd, name, value, false);
+}

-    if (virStrToLong_ui(arg->data, NULL, 10, value) < 0)
-        return -1;
-    return 1;
+/**
+ * vshCommandOptUIntWrap:
+ * @cmd command reference
+ * @name option name
+ * @value result
+ *
+ * Convert option to unsigned int, wraps negative numbers to positive
+ * See vshCommandOptInt()
+ */
+int
+vshCommandOptUIntWrap(const vshCmd *cmd, const char *name, unsigned int *value)
+{
+    return vshCommandOptUIntInternal(cmd, name, value, true);
 }


diff --git a/tools/virsh.h b/tools/virsh.h
index 1ffc003..9afbbb5 100644
--- a/tools/virsh.h
+++ b/tools/virsh.h
@@ -285,6 +285,9 @@ int vshCommandOptInt(const vshCmd *cmd, const char *name, int *value)
 int vshCommandOptUInt(const vshCmd *cmd, const char *name,
                       unsigned int *value)
     ATTRIBUTE_NONNULL(3) ATTRIBUTE_RETURN_CHECK;
+int vshCommandOptUIntWrap(const vshCmd *cmd, const char *name,
+                          unsigned int *value)
+    ATTRIBUTE_NONNULL(3) ATTRIBUTE_RETURN_CHECK;
 int vshCommandOptUL(const vshCmd *cmd, const char *name,
                     unsigned long *value)
     ATTRIBUTE_NONNULL(3) ATTRIBUTE_RETURN_CHECK;
-- 
1.9.3

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]