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

[libvirt] [PATCH 1/3] virsh: avoid strtol



We were forgetting to check errno for overflow.

* tools/virsh.c (get_integer_keycode, vshCommandOptInt)
(vshCommandOptUInt, vshCommandOptUL, vshCommandOptLongLong)
(vshCommandOptULongLong): Rewrite to be safer.
---
 tools/virsh.c |   66 ++++++++++++++++----------------------------------------
 1 files changed, 19 insertions(+), 47 deletions(-)

diff --git a/tools/virsh.c b/tools/virsh.c
index 95ed7bc..3ec853b 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -5758,14 +5758,11 @@ static const vshCmdOptDef opts_send_key[] = {

 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;
+    int ret;

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

 static bool
@@ -17973,8 +17970,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 +17980,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 +18000,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 +18010,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 +18030,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 +18040,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 +18094,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 +18104,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 +18124,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 +18134,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


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