dlogctl: add a parameter correctness check 49/193449/2
authorMichal Bloch <m.bloch@samsung.com>
Tue, 20 Nov 2018 10:58:24 +0000 (11:58 +0100)
committerMichal Bloch <m.bloch@samsung.com>
Tue, 20 Nov 2018 18:50:50 +0000 (19:50 +0100)
Now `-g -s 123 --disable` is disallowed.

Change-Id: I9b8163e21cbc9c4fe1d20d2d3c58d289cfb0f640
Signed-off-by: Michal Bloch <m.bloch@samsung.com>
src/logctl/logctl.c

index b3278fa..81d0efa 100644 (file)
@@ -46,6 +46,12 @@ bool parse_options(int argc, const char **argv, struct parsed_params *params, in
        params->plog_buffers_bitset = 0;
        bool dump = true;
 
+#define SET_ACTION(x) do { \
+       if (params->action != x && params->action != ACTION_NONE) \
+               goto failure; \
+       params->action = x; \
+} while (false)
+
        for (;;) {
 
                static const struct option long_options[] = {
@@ -67,7 +73,7 @@ bool parse_options(int argc, const char **argv, struct parsed_params *params, in
 
                switch (opt) {
                case 0:
-                       params->action = ACTION_PLOG;
+                       SET_ACTION(ACTION_PLOG);
                        params->plog_enable = (long_index == 6);
                        break;
                case 'b': {
@@ -81,14 +87,14 @@ bool parse_options(int argc, const char **argv, struct parsed_params *params, in
                        *exit_value = EXIT_SUCCESS;
                        goto print_help;
                case 'g':
-                       params->action = ACTION_GET;
+                       SET_ACTION(ACTION_GET);
                        break;
                case 's': {
                        const int value_num = atoi(optarg);
                        if (strcmp(optarg, "allow") && strcmp(optarg, "deny") && (value_num <= 0 || value_num >= __LOG_LIMITER_LIMIT_MAX))
                                goto failure;
                        params->value = optarg;
-                       params->action = ACTION_SET;
+                       SET_ACTION(ACTION_SET);
                        break;
                } case 't': {
                        params->tag = optarg;
@@ -101,8 +107,9 @@ bool parse_options(int argc, const char **argv, struct parsed_params *params, in
                        dump = false;
                        break;
                case ':':
-                       if (optopt == 's')
-                               params->action = ACTION_SET;
+                       if (optopt != 's')
+                               goto failure;
+                       SET_ACTION(ACTION_SET);
                        break;
 
                default:
@@ -110,6 +117,8 @@ bool parse_options(int argc, const char **argv, struct parsed_params *params, in
                }
        }
 
+#undef SET_ACTION
+
        if (params->plog_buffers_bitset != 0 && params->action != ACTION_PLOG)
                goto failure;