Fix: check errors in lttng command argument values
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Thu, 2 May 2013 15:53:08 +0000 (11:53 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Thu, 2 May 2013 15:53:08 +0000 (11:53 -0400)
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
src/bin/lttng/commands/enable_channels.c

index 039750bcb99dc8c7e95d902e5d26656eb3b8c3ea..52917cdad56e6289e0b99608dacd549b3441386b 100644 (file)
@@ -340,33 +340,89 @@ int cmd_enable_channels(int argc, const char **argv)
                        DBG("Channel subbuf size set to %" PRIu64, chan.attr.subbuf_size);
                        break;
                case OPT_NUM_SUBBUF:
                        DBG("Channel subbuf size set to %" PRIu64, chan.attr.subbuf_size);
                        break;
                case OPT_NUM_SUBBUF:
-                       /* TODO Replace atoi with strtol and check for errors */
-                       chan.attr.num_subbuf = atoi(poptGetOptArg(pc));
+                       errno = 0;
+                       chan.attr.num_subbuf = strtoull(poptGetOptArg(pc), NULL, 0);
+                       if (errno != 0) {
+                               ERR("Wrong value the --num-subbuf parameter: %s", opt_arg);
+                               ret = CMD_ERROR;
+                               goto end;
+                       }
+
                        DBG("Channel subbuf num set to %" PRIu64, chan.attr.num_subbuf);
                        break;
                case OPT_SWITCH_TIMER:
                        DBG("Channel subbuf num set to %" PRIu64, chan.attr.num_subbuf);
                        break;
                case OPT_SWITCH_TIMER:
-                       /* TODO Replace atoi with strtol and check for errors */
-                       chan.attr.switch_timer_interval = atoi(poptGetOptArg(pc));
+               {
+                       unsigned long v;
+
+                       errno = 0;
+                       v = strtoul(poptGetOptArg(pc), NULL, 0);
+                       if (errno != 0) {
+                               ERR("Wrong value the --switch-timer parameter: %s", opt_arg);
+                               ret = CMD_ERROR;
+                               goto end;
+                       }
+                       if (v != (uint32_t) v) {
+                               ERR("32-bit overflow in --switch-timer parameter: %s", opt_arg);
+                               ret = CMD_ERROR;
+                               goto end;
+                       }
+                       chan.attr.switch_timer_interval = (uint32_t) v;
                        DBG("Channel switch timer interval set to %d", chan.attr.switch_timer_interval);
                        break;
                        DBG("Channel switch timer interval set to %d", chan.attr.switch_timer_interval);
                        break;
+               }
                case OPT_READ_TIMER:
                case OPT_READ_TIMER:
-                       /* TODO Replace atoi with strtol and check for errors */
-                       chan.attr.read_timer_interval = atoi(poptGetOptArg(pc));
+               {
+                       unsigned long v;
+
+                       errno = 0;
+                       v = strtoul(poptGetOptArg(pc), NULL, 0);
+                       if (errno != 0) {
+                               ERR("Wrong value the --read-timer parameter: %s", opt_arg);
+                               ret = CMD_ERROR;
+                               goto end;
+                       }
+                       if (v != (uint32_t) v) {
+                               ERR("32-bit overflow in --read-timer parameter: %s", opt_arg);
+                               ret = CMD_ERROR;
+                               goto end;
+                       }
+                       chan.attr.read_timer_interval = (uint32_t) v;
                        DBG("Channel read timer interval set to %d", chan.attr.read_timer_interval);
                        break;
                        DBG("Channel read timer interval set to %d", chan.attr.read_timer_interval);
                        break;
+               }
                case OPT_USERSPACE:
                        opt_userspace = 1;
                        break;
                case OPT_TRACEFILE_SIZE:
                case OPT_USERSPACE:
                        opt_userspace = 1;
                        break;
                case OPT_TRACEFILE_SIZE:
-                       chan.attr.tracefile_size = atoll(poptGetOptArg(pc));
+                       if (utils_parse_size_suffix(opt_arg, &chan.attr.tracefile_size) < 0) {
+                               ERR("Wrong value the --tracefile-size parameter: %s", opt_arg);
+                               ret = CMD_ERROR;
+                               goto end;
+                       }
                        DBG("Maximum tracefile size set to %" PRIu64,
                                        chan.attr.tracefile_size);
                        break;
                case OPT_TRACEFILE_COUNT:
                        DBG("Maximum tracefile size set to %" PRIu64,
                                        chan.attr.tracefile_size);
                        break;
                case OPT_TRACEFILE_COUNT:
-                       chan.attr.tracefile_count = atoll(poptGetOptArg(pc));
+               {
+                       unsigned long v;
+
+                       errno = 0;
+                       v = strtoul(poptGetOptArg(pc), NULL, 0);
+                       if (errno != 0) {
+                               ERR("Wrong value the --tracefile-count parameter: %s", opt_arg);
+                               ret = CMD_ERROR;
+                               goto end;
+                       }
+                       if (v != (uint32_t) v) {
+                               ERR("32-bit overflow in --tracefile-count parameter: %s", opt_arg);
+                               ret = CMD_ERROR;
+                               goto end;
+                       }
+                       chan.attr.tracefile_count = (uint32_t) v;
                        DBG("Maximum tracefile count set to %" PRIu64,
                                        chan.attr.tracefile_count);
                        break;
                        DBG("Maximum tracefile count set to %" PRIu64,
                                        chan.attr.tracefile_count);
                        break;
+               }
                case OPT_LIST_OPTIONS:
                        list_cmd_options(stdout, long_options);
                        goto end;
                case OPT_LIST_OPTIONS:
                        list_cmd_options(stdout, long_options);
                        goto end;
This page took 0.040992 seconds and 4 git commands to generate.