Blob Blame History Raw
From 9e531fda058369ff70416fa1a8382fcc66e411c3 Mon Sep 17 00:00:00 2001
From: Atin Mukherjee <amukherj@redhat.com>
Date: Thu, 9 Feb 2017 16:09:08 +0530
Subject: [PATCH 331/361] cli: add integer check for timeout option

mainline:
> BUG: 1420697
> Reviewed-on: https://review.gluster.org/16578
> Smoke: Gluster Build System <jenkins@build.gluster.org>
> NetBSD-regression: NetBSD Build System <jenkins@build.gluster.org>
> CentOS-regression: Gluster Build System <jenkins@build.gluster.org>
> Reviewed-by: Prashanth Pai <ppai@redhat.com>
> Reviewed-by: Samikshan Bairagya <samikshan@gmail.com>
(cherry picked from commit ec1effd27de632ca9800e8cfd81716e511a9a2ba)

BUG: 1381158
Change-Id: Ia9f2d343e0a9ad13af1a62abe8946d646d36b3bb
Signed-off-by: Atin Mukherjee <amukherj@redhat.com>
Reviewed-on: https://code.engineering.redhat.com/gerrit/101312
Tested-by: Milind Changire <mchangir@redhat.com>
---
 cli/src/cli.c | 29 ++++++++++++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)

diff --git a/cli/src/cli.c b/cli/src/cli.c
index 518ae26..add10d2 100644
--- a/cli/src/cli.c
+++ b/cli/src/cli.c
@@ -292,11 +292,31 @@ cli_rpc_notify (struct rpc_clnt *rpc, void *mydata, rpc_clnt_event_t event,
         return ret;
 }
 
+static gf_boolean_t
+is_valid_int (char *str)
+{
+        if (*str == '-')
+                ++str;
+
+        /* Handle empty string or just "-".*/
+        if (!*str)
+                return _gf_false;
+
+        /* Check for non-digit chars in the rest of the string */
+        while (*str) {
+                if (!isdigit(*str))
+                        return _gf_false;
+        else
+                ++str;
+        }
+        return _gf_true;
+}
 
 /*
  * ret: 0: option successfully processed
  *      1: signalling end of option list
- *     -1: unknown option or other issue
+ *     -1: unknown option
+ *     -2: parsing issue (avoid unknown option error)
  */
 int
 cli_opt_parse (char *opt, struct cli_state *state)
@@ -362,6 +382,11 @@ cli_opt_parse (char *opt, struct cli_state *state)
         }
         oarg = strtail (opt, "timeout=");
         if (oarg) {
+                if (!is_valid_int (oarg) || atoi(oarg) <= 0) {
+                        cli_err ("timeout value should be a postive integer");
+                        return -2; /* -2 instead of -1 to avoid unknown option
+                                      error */
+                }
                 cli_default_conn_timeout = atoi(oarg);
                 return 0;
         }
@@ -427,6 +452,8 @@ parse_cmdline (int argc, char *argv[], struct cli_state *state)
                         if (ret == -1) {
                                 cli_out ("unrecognized option --%s", opt);
                                 return ret;
+                        } else if (ret == -2) {
+                                return ret;
                         }
                         for (j = i; j < state->argc - 1; j++)
                                 state->argv[j] = state->argv[j + 1];
-- 
1.8.3.1