From 2a596acfd054afda5234083b675d5fd4bc171a21 Mon Sep 17 00:00:00 2001 From: Atin Mukherjee Date: Thu, 9 Feb 2017 16:09:08 +0530 Subject: cli: add integer check for timeout option >Reviewed-on: https://review.gluster.org/16578 >NetBSD-regression: NetBSD Build System >CentOS-regression: Gluster Build System >Smoke: Gluster Build System >Reviewed-by: Prashanth Pai >Reviewed-by: Samikshan Bairagya >(cherry picked from commit 421a098d2acfd4b837d4c03ea6f69987c670d3f7) Change-Id: Ia9f2d343e0a9ad13af1a62abe8946d646d36b3bb BUG: 1421017 Signed-off-by: Atin Mukherjee Reviewed-on: https://review.gluster.org/16595 Smoke: Gluster Build System Reviewed-by: Prashanth Pai NetBSD-regression: NetBSD Build System CentOS-regression: Gluster Build System Reviewed-by: Shyamsundar Ranganathan --- cli/src/cli.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) (limited to 'cli') diff --git a/cli/src/cli.c b/cli/src/cli.c index 422dad2a694..1710724abd6 100644 --- a/cli/src/cli.c +++ b/cli/src/cli.c @@ -289,11 +289,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) @@ -359,6 +379,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; } @@ -424,6 +449,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]; -- cgit