Skip to content

Commit 2ef08ce

Browse files
ndrs-pstcfriedt
authored andcommitted
net: wifi: shell: use getopt_state for safer optarg access
Using `getopt_state` to access `optarg` and also `optopt` offers a better alternative to direct global access. See e145eb9 for the previous change related to this. Signed-off-by: Pisit Sawangvonganan <[email protected]>
1 parent 5af4e9d commit 2ef08ce

File tree

1 file changed

+36
-35
lines changed

1 file changed

+36
-35
lines changed

subsys/net/l2/wifi/wifi_shell.c

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ static int __wifi_args_to_params(const struct shell *sh, size_t argc, char *argv
492492
state = getopt_state_get();
493493
switch (opt) {
494494
case 's':
495-
params->ssid = optarg;
495+
params->ssid = state->optarg;
496496
params->ssid_length = strlen(params->ssid);
497497
if (params->ssid_length > WIFI_SSID_MAX_LEN) {
498498
PR_WARNING("SSID too long (max %d characters)\n",
@@ -501,17 +501,17 @@ static int __wifi_args_to_params(const struct shell *sh, size_t argc, char *argv
501501
}
502502
break;
503503
case 'k':
504-
params->security = atoi(optarg);
504+
params->security = atoi(state->optarg);
505505
if (params->security) {
506506
secure_connection = true;
507507
}
508508
break;
509509
case 'p':
510-
params->psk = optarg;
510+
params->psk = state->optarg;
511511
params->psk_length = strlen(params->psk);
512512
break;
513513
case 'c':
514-
channel = strtol(optarg, &endptr, 10);
514+
channel = strtol(state->optarg, &endptr, 10);
515515
for (band = 0; band < ARRAY_SIZE(all_bands); band++) {
516516
offset += snprintf(bands_str + offset,
517517
sizeof(bands_str) - offset,
@@ -543,7 +543,7 @@ static int __wifi_args_to_params(const struct shell *sh, size_t argc, char *argv
543543
break;
544544
case 'b':
545545
if (iface_mode == WIFI_MODE_INFRA) {
546-
switch (atoi(optarg)) {
546+
switch (atoi(state->optarg)) {
547547
case 2:
548548
params->band = WIFI_FREQ_BAND_2_4_GHZ;
549549
break;
@@ -554,7 +554,7 @@ static int __wifi_args_to_params(const struct shell *sh, size_t argc, char *argv
554554
params->band = WIFI_FREQ_BAND_6_GHZ;
555555
break;
556556
default:
557-
PR_ERROR("Invalid band: %d\n", atoi(optarg));
557+
PR_ERROR("Invalid band: %d\n", atoi(state->optarg));
558558
return -EINVAL;
559559
}
560560
}
@@ -566,28 +566,28 @@ static int __wifi_args_to_params(const struct shell *sh, size_t argc, char *argv
566566
wifi_security_txt(params->security));
567567
return -EINVAL;
568568
}
569-
params->mfp = atoi(optarg);
569+
params->mfp = atoi(state->optarg);
570570
break;
571571
case 'm':
572572
if (net_bytes_from_str(params->bssid, sizeof(params->bssid),
573-
optarg) < 0) {
573+
state->optarg) < 0) {
574574
PR_WARNING("Invalid MAC address\n");
575575
return -EINVAL;
576576
}
577577
break;
578578
case 't':
579579
if (iface_mode == WIFI_MODE_INFRA) {
580-
params->timeout = strtol(optarg, &endptr, 10);
580+
params->timeout = strtol(state->optarg, &endptr, 10);
581581
if (*endptr != '\0') {
582-
PR_ERROR("Invalid timeout: %s\n", optarg);
582+
PR_ERROR("Invalid timeout: %s\n", state->optarg);
583583
return -EINVAL;
584584
}
585585
}
586586
break;
587587
case 'h':
588588
return -ENOEXEC;
589589
default:
590-
PR_ERROR("Invalid option %c\n", optopt);
590+
PR_ERROR("Invalid option %c\n", state->optopt);
591591
return -EINVAL;
592592
}
593593
}
@@ -1412,15 +1412,15 @@ static int wifi_ap_config_args_to_params(const struct shell *sh, size_t argc, ch
14121412
state = getopt_state_get();
14131413
switch (opt) {
14141414
case 'i':
1415-
if (!parse_number(sh, &val, optarg, "max_inactivity",
1415+
if (!parse_number(sh, &val, state->optarg, "max_inactivity",
14161416
0, WIFI_AP_STA_MAX_INACTIVITY)) {
14171417
return -EINVAL;
14181418
}
14191419
params->max_inactivity = (uint32_t)val;
14201420
params->type |= WIFI_AP_CONFIG_PARAM_MAX_INACTIVITY;
14211421
break;
14221422
case 's':
1423-
if (!parse_number(sh, &val, optarg, "max_num_sta",
1423+
if (!parse_number(sh, &val, state->optarg, "max_num_sta",
14241424
0, CONFIG_WIFI_MGMT_AP_MAX_NUM_STA)) {
14251425
return -EINVAL;
14261426
}
@@ -1431,7 +1431,7 @@ static int wifi_ap_config_args_to_params(const struct shell *sh, size_t argc, ch
14311431
shell_help(sh);
14321432
return SHELL_CMD_HELP_PRINTED;
14331433
default:
1434-
PR_ERROR("Invalid option %c\n", optopt);
1434+
PR_ERROR("Invalid option %c\n", state->optopt);
14351435
shell_help(sh);
14361436
return SHELL_CMD_HELP_PRINTED;
14371437
}
@@ -1984,22 +1984,22 @@ static int parse_dpp_args_auth_init(const struct shell *sh, size_t argc, char *a
19841984
state = getopt_state_get();
19851985
switch (opt) {
19861986
case 'p':
1987-
params->auth_init.peer = shell_strtol(optarg, 10, &ret);
1987+
params->auth_init.peer = shell_strtol(state->optarg, 10, &ret);
19881988
break;
19891989
case 'r':
1990-
params->auth_init.role = shell_strtol(optarg, 10, &ret);
1990+
params->auth_init.role = shell_strtol(state->optarg, 10, &ret);
19911991
break;
19921992
case 'c':
1993-
params->auth_init.configurator = shell_strtol(optarg, 10, &ret);
1993+
params->auth_init.configurator = shell_strtol(state->optarg, 10, &ret);
19941994
break;
19951995
case 'm':
1996-
params->auth_init.conf = shell_strtol(optarg, 10, &ret);
1996+
params->auth_init.conf = shell_strtol(state->optarg, 10, &ret);
19971997
break;
19981998
case 's':
1999-
strncpy(params->auth_init.ssid, optarg, WIFI_SSID_MAX_LEN);
1999+
strncpy(params->auth_init.ssid, state->optarg, WIFI_SSID_MAX_LEN);
20002000
break;
20012001
default:
2002-
PR_ERROR("Invalid option %c\n", optopt);
2002+
PR_ERROR("Invalid option %c\n", state->optopt);
20032003
return -EINVAL;
20042004
}
20052005

@@ -2029,13 +2029,13 @@ static int parse_dpp_args_chirp(const struct shell *sh, size_t argc, char *argv[
20292029
state = getopt_state_get();
20302030
switch (opt) {
20312031
case 'i':
2032-
params->chirp.id = shell_strtol(optarg, 10, &ret);
2032+
params->chirp.id = shell_strtol(state->optarg, 10, &ret);
20332033
break;
20342034
case 'f':
2035-
params->chirp.freq = shell_strtol(optarg, 10, &ret);
2035+
params->chirp.freq = shell_strtol(state->optarg, 10, &ret);
20362036
break;
20372037
default:
2038-
PR_ERROR("Invalid option %c\n", optopt);
2038+
PR_ERROR("Invalid option %c\n", state->optopt);
20392039
return -EINVAL;
20402040
}
20412041

@@ -2065,13 +2065,13 @@ static int parse_dpp_args_listen(const struct shell *sh, size_t argc, char *argv
20652065
state = getopt_state_get();
20662066
switch (opt) {
20672067
case 'r':
2068-
params->listen.role = shell_strtol(optarg, 10, &ret);
2068+
params->listen.role = shell_strtol(state->optarg, 10, &ret);
20692069
break;
20702070
case 'f':
2071-
params->listen.freq = shell_strtol(optarg, 10, &ret);
2071+
params->listen.freq = shell_strtol(state->optarg, 10, &ret);
20722072
break;
20732073
default:
2074-
PR_ERROR("Invalid option %c\n", optopt);
2074+
PR_ERROR("Invalid option %c\n", state->optopt);
20752075
return -EINVAL;
20762076
}
20772077

@@ -2103,20 +2103,20 @@ static int parse_dpp_args_btstrap_gen(const struct shell *sh, size_t argc, char
21032103
state = getopt_state_get();
21042104
switch (opt) {
21052105
case 't':
2106-
params->bootstrap_gen.type = shell_strtol(optarg, 10, &ret);
2106+
params->bootstrap_gen.type = shell_strtol(state->optarg, 10, &ret);
21072107
break;
21082108
case 'o':
2109-
params->bootstrap_gen.op_class = shell_strtol(optarg, 10, &ret);
2109+
params->bootstrap_gen.op_class = shell_strtol(state->optarg, 10, &ret);
21102110
break;
21112111
case 'h':
2112-
params->bootstrap_gen.chan = shell_strtol(optarg, 10, &ret);
2112+
params->bootstrap_gen.chan = shell_strtol(state->optarg, 10, &ret);
21132113
break;
21142114
case 'a':
21152115
ret = net_bytes_from_str(params->bootstrap_gen.mac,
2116-
WIFI_MAC_ADDR_LEN, optarg);
2116+
WIFI_MAC_ADDR_LEN, state->optarg);
21172117
break;
21182118
default:
2119-
PR_ERROR("Invalid option %c\n", optopt);
2119+
PR_ERROR("Invalid option %c\n", state->optopt);
21202120
return -EINVAL;
21212121
}
21222122

@@ -2164,16 +2164,17 @@ static int parse_dpp_args_set_config_param(const struct shell *sh, size_t argc,
21642164
state = getopt_state_get();
21652165
switch (opt) {
21662166
case 'c':
2167-
params->configurator_set.configurator = shell_strtol(optarg, 10, &ret);
2167+
params->configurator_set.configurator =
2168+
shell_strtol(state->optarg, 10, &ret);
21682169
break;
21692170
case 'm':
2170-
params->configurator_set.conf = shell_strtol(optarg, 10, &ret);
2171+
params->configurator_set.conf = shell_strtol(state->optarg, 10, &ret);
21712172
break;
21722173
case 's':
2173-
strncpy(params->configurator_set.ssid, optarg, WIFI_SSID_MAX_LEN);
2174+
strncpy(params->configurator_set.ssid, state->optarg, WIFI_SSID_MAX_LEN);
21742175
break;
21752176
default:
2176-
PR_ERROR("Invalid option %c\n", optopt);
2177+
PR_ERROR("Invalid option %c\n", state->optopt);
21772178
return -EINVAL;
21782179
}
21792180

0 commit comments

Comments
 (0)