wifi: shell: Fix the channel extraction

The channel extraction from string directly uses the end variable with
limited data type, this causes issue if an invalid channel that exceeds
the data is given as an input e.g., 300, which would end up as a valid
channel 44.

Use an intermediate variable with type that can hold all possible
combinations (valid and invalid) and only after validation assign that
to the end type.

Signed-off-by: Chaitanya Tata <Chaitanya.Tata@nordicsemi.no>
This commit is contained in:
Chaitanya Tata 2024-01-16 23:54:36 +05:30 committed by Carles Cufí
parent 2f88df9cef
commit c9363a9c71

View file

@ -466,12 +466,13 @@ static int __wifi_args_to_params(size_t argc, char *argv[],
/* Channel (optional: STA, mandatory: AP) */
if ((idx < argc) && (strlen(argv[idx]) <= 3)) {
params->channel = strtol(argv[idx], &endptr, 10);
long channel = strtol(argv[idx], &endptr, 10);
if (*endptr != '\0') {
return -EINVAL;
}
if (iface_mode == WIFI_MODE_INFRA && params->channel == 0) {
if (iface_mode == WIFI_MODE_INFRA && channel == 0) {
params->channel = WIFI_CHANNEL_ANY;
} else {
const uint8_t bands[] = {WIFI_FREQ_BAND_2_4_GHZ,
@ -482,7 +483,7 @@ static int __wifi_args_to_params(size_t argc, char *argv[],
for (band = 0; band < ARRAY_SIZE(bands); band++) {
if (wifi_utils_validate_chan(bands[band],
params->channel)) {
channel)) {
found = true;
break;
}
@ -491,8 +492,9 @@ static int __wifi_args_to_params(size_t argc, char *argv[],
if (!found) {
return -EINVAL;
}
}
params->channel = channel;
}
idx++;
}