@@ -68,7 +68,7 @@ static enum nbctl_wait_type wait_type = NBCTL_WAIT_NONE;
static bool force_wait = false;
/* --timeout: Time to wait for a connection to 'db'. */
-static int timeout;
+static unsigned int timeout;
/* Format for table output. */
static struct table_style table_style = TABLE_STYLE_DEFAULT;
@@ -345,8 +345,7 @@ handle_main_loop_option(int opt, const char *arg, bool *handled)
break;
case 't':
- timeout = strtoul(arg, NULL, 10);
- if (timeout < 0) {
+ if (!str_to_uint(arg, 10, &timeout) || !timeout) {
return xasprintf("value %s on -t or --timeout is invalid", arg);
}
break;
@@ -5358,8 +5357,7 @@ nbctl_client(const char *socket_name,
exit(EXIT_SUCCESS);
case 't':
- timeout = strtoul(po->arg, NULL, 10);
- if (timeout < 0) {
+ if (!str_to_uint(po->arg, 10, &timeout) || !timeout) {
ctl_fatal("value %s on -t or --timeout is invalid", po->arg);
}
break;
@@ -69,7 +69,7 @@ static bool oneline;
static bool dry_run;
/* --timeout: Time to wait for a connection to 'db'. */
-static int timeout;
+static unsigned int timeout;
/* Format for table output. */
static struct table_style table_style = TABLE_STYLE_DEFAULT;
@@ -268,8 +268,7 @@ parse_options(int argc, char *argv[], struct shash *local_options)
exit(EXIT_SUCCESS);
case 't':
- timeout = strtoul(optarg, NULL, 10);
- if (timeout < 0) {
+ if (!str_to_uint(optarg, 10, &timeout) || !timeout) {
ctl_fatal("value %s on -t or --timeout is invalid", optarg);
}
break;
@@ -332,7 +332,7 @@ parse_options(int argc, char *argv[])
table_style.format = TF_TABLE;
for (;;) {
- unsigned long int timeout;
+ unsigned int timeout = 0;
int c;
c = getopt_long(argc, argv, short_options, long_options, NULL);
@@ -366,10 +366,8 @@ parse_options(int argc, char *argv[])
break;
case 't':
- timeout = strtoul(optarg, NULL, 10);
- if (timeout <= 0) {
- ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
- optarg);
+ if (!str_to_uint(optarg, 10, &timeout) || !timeout) {
+ ovs_fatal(0, "value %s on -t or --timeout is invalid", optarg);
} else {
time_alarm(timeout);
}
@@ -96,7 +96,7 @@ parse_options(int argc, char *argv[], struct test_ovsdb_pvt_context *pvt)
char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
for (;;) {
- unsigned long int timeout;
+ unsigned int timeout = 0;
int c;
c = getopt_long(argc, argv, short_options, long_options, NULL);
@@ -106,10 +106,8 @@ parse_options(int argc, char *argv[], struct test_ovsdb_pvt_context *pvt)
switch (c) {
case 't':
- timeout = strtoul(optarg, NULL, 10);
- if (timeout <= 0) {
- ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
- optarg);
+ if (!str_to_uint(optarg, 10, &timeout) || !timeout) {
+ ovs_fatal(0, "value %s on -t or --timeout is invalid", optarg);
} else {
time_alarm(timeout);
}
@@ -133,6 +133,7 @@ parse_command_line(int argc, char *argv[])
e_options = 0;
for (;;) {
int option;
+ unsigned int timeout = 0;
option = getopt_long(argc, argv, short_options, long_options, NULL);
if (option == -1) {
@@ -165,7 +166,11 @@ parse_command_line(int argc, char *argv[])
exit(EXIT_SUCCESS);
case 'T':
- time_alarm(atoi(optarg));
+ if (!str_to_uint(optarg, 10, &timeout) || !timeout) {
+ ovs_fatal(0, "value %s on -T or --timeout is invalid", optarg);
+ } else {
+ time_alarm(timeout);
+ }
break;
case 'V':
@@ -101,7 +101,7 @@ parse_options(int argc, char *argv[])
bool set_names = false;
for (;;) {
- unsigned long int timeout;
+ unsigned int timeout = 0;
int c;
c = getopt_long(argc, argv, short_options, long_options, NULL);
@@ -141,10 +141,8 @@ parse_options(int argc, char *argv[])
break;
case 't':
- timeout = strtoul(optarg, NULL, 10);
- if (timeout <= 0) {
- ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
- optarg);
+ if (!str_to_uint(optarg, 10, &timeout) || !timeout) {
+ ovs_fatal(0, "value %s on -t or --timeout is invalid", optarg);
} else {
time_alarm(timeout);
}
@@ -272,7 +272,7 @@ parse_options(int argc, char *argv[])
set_allowed_ofp_versions("OpenFlow10");
for (;;) {
- unsigned long int timeout;
+ unsigned int timeout = 0;
int c;
c = getopt_long(argc, argv, short_options, long_options, NULL);
@@ -282,10 +282,8 @@ parse_options(int argc, char *argv[])
switch (c) {
case 't':
- timeout = strtoul(optarg, NULL, 10);
- if (timeout <= 0) {
- ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
- optarg);
+ if (!str_to_uint(optarg, 10, &timeout) || !timeout) {
+ ovs_fatal(0, "value %s on -t or --timeout is invalid", optarg);
} else {
time_alarm(timeout);
}
@@ -69,7 +69,7 @@ static bool dry_run;
static bool wait_for_reload = true;
/* --timeout: Time to wait for a connection to 'db'. */
-static int timeout;
+static unsigned int timeout;
/* --retry: If true, ovs-vsctl will retry connecting to the database forever.
* If false and --db says to use an active connection method (e.g. "unix:",
@@ -311,10 +311,8 @@ parse_options(int argc, char *argv[], struct shash *local_options)
exit(EXIT_SUCCESS);
case 't':
- timeout = strtoul(optarg, NULL, 10);
- if (timeout < 0) {
- ctl_fatal("value %s on -t or --timeout is invalid",
- optarg);
+ if (!str_to_uint(optarg, 10, &timeout) || !timeout) {
+ ctl_fatal("value %s on -t or --timeout is invalid", optarg);
}
break;
@@ -65,7 +65,7 @@ static bool oneline;
static bool dry_run;
/* --timeout: Time to wait for a connection to 'db'. */
-static int timeout;
+static unsigned int timeout;
/* Format for table output. */
static struct table_style table_style = TABLE_STYLE_DEFAULT;
@@ -252,10 +252,8 @@ parse_options(int argc, char *argv[], struct shash *local_options)
exit(EXIT_SUCCESS);
case 't':
- timeout = strtoul(optarg, NULL, 10);
- if (timeout < 0) {
- ctl_fatal("value %s on -t or --timeout is invalid",
- optarg);
+ if (!str_to_uint(optarg, 10, &timeout) || !timeout) {
+ ctl_fatal("value %s on -t or --timeout is invalid", optarg);
}
break;
Parsing of the '--timeout' option implemented differently for every single control utility and, which is more important, highly inaccurate. In most cases unsigned result of 'strtoul' stored in signed variable. Parsing failures are not tracked. 'ovs-appctl' even uses just 'atoi' without any checking of the argument or result. This patch unifies the parsing by using 'str_to_uint'. Signed-off-by: Ilya Maximets <i.maximets@samsung.com> --- ovn/utilities/ovn-nbctl.c | 8 +++----- ovn/utilities/ovn-sbctl.c | 5 ++--- ovsdb/ovsdb-client.c | 8 +++----- tests/test-ovsdb.c | 8 +++----- utilities/ovs-appctl.c | 7 ++++++- utilities/ovs-dpctl.c | 8 +++----- utilities/ovs-ofctl.c | 8 +++----- utilities/ovs-vsctl.c | 8 +++----- vtep/vtep-ctl.c | 8 +++----- 9 files changed, 29 insertions(+), 39 deletions(-)