21ab4e
From 9e531fda058369ff70416fa1a8382fcc66e411c3 Mon Sep 17 00:00:00 2001
21ab4e
From: Atin Mukherjee <amukherj@redhat.com>
21ab4e
Date: Thu, 9 Feb 2017 16:09:08 +0530
21ab4e
Subject: [PATCH 331/361] cli: add integer check for timeout option
21ab4e
21ab4e
mainline:
21ab4e
> BUG: 1420697
21ab4e
> Reviewed-on: https://review.gluster.org/16578
21ab4e
> Smoke: Gluster Build System <jenkins@build.gluster.org>
21ab4e
> NetBSD-regression: NetBSD Build System <jenkins@build.gluster.org>
21ab4e
> CentOS-regression: Gluster Build System <jenkins@build.gluster.org>
21ab4e
> Reviewed-by: Prashanth Pai <ppai@redhat.com>
21ab4e
> Reviewed-by: Samikshan Bairagya <samikshan@gmail.com>
21ab4e
(cherry picked from commit ec1effd27de632ca9800e8cfd81716e511a9a2ba)
21ab4e
21ab4e
BUG: 1381158
21ab4e
Change-Id: Ia9f2d343e0a9ad13af1a62abe8946d646d36b3bb
21ab4e
Signed-off-by: Atin Mukherjee <amukherj@redhat.com>
21ab4e
Reviewed-on: https://code.engineering.redhat.com/gerrit/101312
21ab4e
Tested-by: Milind Changire <mchangir@redhat.com>
21ab4e
---
21ab4e
 cli/src/cli.c | 29 ++++++++++++++++++++++++++++-
21ab4e
 1 file changed, 28 insertions(+), 1 deletion(-)
21ab4e
21ab4e
diff --git a/cli/src/cli.c b/cli/src/cli.c
21ab4e
index 518ae26..add10d2 100644
21ab4e
--- a/cli/src/cli.c
21ab4e
+++ b/cli/src/cli.c
21ab4e
@@ -292,11 +292,31 @@ cli_rpc_notify (struct rpc_clnt *rpc, void *mydata, rpc_clnt_event_t event,
21ab4e
         return ret;
21ab4e
 }
21ab4e
 
21ab4e
+static gf_boolean_t
21ab4e
+is_valid_int (char *str)
21ab4e
+{
21ab4e
+        if (*str == '-')
21ab4e
+                ++str;
21ab4e
+
21ab4e
+        /* Handle empty string or just "-".*/
21ab4e
+        if (!*str)
21ab4e
+                return _gf_false;
21ab4e
+
21ab4e
+        /* Check for non-digit chars in the rest of the string */
21ab4e
+        while (*str) {
21ab4e
+                if (!isdigit(*str))
21ab4e
+                        return _gf_false;
21ab4e
+        else
21ab4e
+                ++str;
21ab4e
+        }
21ab4e
+        return _gf_true;
21ab4e
+}
21ab4e
 
21ab4e
 /*
21ab4e
  * ret: 0: option successfully processed
21ab4e
  *      1: signalling end of option list
21ab4e
- *     -1: unknown option or other issue
21ab4e
+ *     -1: unknown option
21ab4e
+ *     -2: parsing issue (avoid unknown option error)
21ab4e
  */
21ab4e
 int
21ab4e
 cli_opt_parse (char *opt, struct cli_state *state)
21ab4e
@@ -362,6 +382,11 @@ cli_opt_parse (char *opt, struct cli_state *state)
21ab4e
         }
21ab4e
         oarg = strtail (opt, "timeout=");
21ab4e
         if (oarg) {
21ab4e
+                if (!is_valid_int (oarg) || atoi(oarg) <= 0) {
21ab4e
+                        cli_err ("timeout value should be a postive integer");
21ab4e
+                        return -2; /* -2 instead of -1 to avoid unknown option
21ab4e
+                                      error */
21ab4e
+                }
21ab4e
                 cli_default_conn_timeout = atoi(oarg);
21ab4e
                 return 0;
21ab4e
         }
21ab4e
@@ -427,6 +452,8 @@ parse_cmdline (int argc, char *argv[], struct cli_state *state)
21ab4e
                         if (ret == -1) {
21ab4e
                                 cli_out ("unrecognized option --%s", opt);
21ab4e
                                 return ret;
21ab4e
+                        } else if (ret == -2) {
21ab4e
+                                return ret;
21ab4e
                         }
21ab4e
                         for (j = i; j < state->argc - 1; j++)
21ab4e
                                 state->argv[j] = state->argv[j + 1];
21ab4e
-- 
21ab4e
1.8.3.1
21ab4e