[PATCHv2 1/3] virsh: avoid strtol

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

 



We were forgetting to check errno for overflow.

* tools/virsh.c (get_integer_keycode, vshCommandOptInt)
(vshCommandOptUInt, vshCommandOptUL, vshCommandOptLongLong)
(vshCommandOptULongLong): Rewrite to be safer.
---

v2: get_integer_keycode still must reject 0

 tools/virsh.c |   67 +++++++++++++++++----------------------------------------
 1 files changed, 20 insertions(+), 47 deletions(-)

diff --git a/tools/virsh.c b/tools/virsh.c
index 95ed7bc..0bfb529 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -5756,15 +5756,13 @@ static const vshCmdOptDef opts_send_key[] = {
     {NULL, 0, 0, NULL}
 };

-static int get_integer_keycode(const char *key_name)
+static int
+get_integer_keycode(const char *key_name)
 {
-    long val;
-    char *endptr;
-
-    val = strtol(key_name, &endptr, 0);
-    if (*endptr != '\0' || val > 0xffff || val <= 0)
-         return -1;
+    unsigned int val;

+    if (virStrToLong_ui(key_name, NULL, 0, &val) < 0 || val > 0xffff || !val)
+        return -1;
     return val;
 }

@@ -17973,8 +17971,6 @@ vshCommandOptInt(const vshCmd *cmd, const char *name, int *value)
 {
     vshCmdOpt *arg;
     int ret;
-    int num;
-    char *end_p = NULL;

     ret = vshCommandOpt(cmd, name, &arg);
     if (ret <= 0)
@@ -17985,12 +17981,9 @@ vshCommandOptInt(const vshCmd *cmd, const char *name, int *value)
         return -2;
     }

-    num = strtol(arg->data, &end_p, 10);
-    if (arg->data != end_p && *end_p == 0) {
-        *value = num;
-        return 1;
-    }
-    return -1;
+    if (virStrToLong_i(arg->data, NULL, 10, value) < 0)
+        return -1;
+    return 1;
 }


@@ -18008,8 +18001,6 @@ vshCommandOptUInt(const vshCmd *cmd, const char *name, unsigned int *value)
 {
     vshCmdOpt *arg;
     int ret;
-    unsigned int num;
-    char *end_p = NULL;

     ret = vshCommandOpt(cmd, name, &arg);
     if (ret <= 0)
@@ -18020,12 +18011,9 @@ vshCommandOptUInt(const vshCmd *cmd, const char *name, unsigned int *value)
         return -2;
     }

-    num = strtoul(arg->data, &end_p, 10);
-    if (arg->data != end_p && *end_p == 0) {
-        *value = num;
-        return 1;
-    }
-    return -1;
+    if (virStrToLong_ui(arg->data, NULL, 10, value) < 0)
+        return -1;
+    return 1;
 }


@@ -18043,8 +18031,6 @@ vshCommandOptUL(const vshCmd *cmd, const char *name, unsigned long *value)
 {
     vshCmdOpt *arg;
     int ret;
-    unsigned long num;
-    char *end_p = NULL;

     ret = vshCommandOpt(cmd, name, &arg);
     if (ret <= 0)
@@ -18055,12 +18041,9 @@ vshCommandOptUL(const vshCmd *cmd, const char *name, unsigned long *value)
         return -2;
     }

-    num = strtoul(arg->data, &end_p, 10);
-    if (arg->data != end_p && *end_p == 0) {
-        *value = num;
-        return 1;
-    }
-    return -1;
+    if (virStrToLong_ul(arg->data, NULL, 10, value) < 0)
+        return -1;
+    return 1;
 }

 /**
@@ -18112,8 +18095,6 @@ vshCommandOptLongLong(const vshCmd *cmd, const char *name,
 {
     vshCmdOpt *arg;
     int ret;
-    long long num;
-    char *end_p = NULL;

     ret = vshCommandOpt(cmd, name, &arg);
     if (ret <= 0)
@@ -18124,12 +18105,9 @@ vshCommandOptLongLong(const vshCmd *cmd, const char *name,
         return -2;
     }

-    num = strtoll(arg->data, &end_p, 10);
-    if (arg->data != end_p && *end_p == 0) {
-        *value = num;
-        return 1;
-    }
-    return -1;
+    if (virStrToLong_ll(arg->data, NULL, 10, value) < 0)
+        return -1;
+    return 1;
 }

 /**
@@ -18147,8 +18125,6 @@ vshCommandOptULongLong(const vshCmd *cmd, const char *name,
 {
     vshCmdOpt *arg;
     int ret;
-    unsigned long long num;
-    char *end_p = NULL;

     ret = vshCommandOpt(cmd, name, &arg);
     if (ret <= 0)
@@ -18159,12 +18135,9 @@ vshCommandOptULongLong(const vshCmd *cmd, const char *name,
         return -2;
     }

-    num = strtoull(arg->data, &end_p, 10);
-    if (arg->data != end_p && *end_p == 0) {
-        *value = num;
-        return 1;
-    }
-    return -1;
+    if (virStrToLong_ull(arg->data, NULL, 10, value) < 0)
+        return -1;
+    return 1;
 }


-- 
1.7.7.6

--
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]