Skip to content

Commit b191075

Browse files
committed
Better length checks
1 parent 6446010 commit b191075

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

ports/espressif/common-hal/wifi/Radio.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,9 @@ wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t
249249
bool connected = ((bits & WIFI_CONNECTED_BIT) != 0) &&
250250
!((bits & WIFI_DISCONNECTED_BIT) != 0);
251251
if (connected) {
252-
if (memcmp(ssid, config->sta.ssid, ssid_len) == 0) {
252+
// SSIDs are up to 32 bytes. Assume it is null terminated if it is less.
253+
if (memcmp(ssid, config->sta.ssid, ssid_len) == 0 &&
254+
(ssid_len == 32 || strlen((const char *)config->sta.ssid) == ssid_len)) {
253255
// Already connected to the desired network.
254256
return WIFI_RADIO_ERROR_NONE;
255257
} else {
@@ -272,7 +274,9 @@ wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t
272274
set_mode_station(self, true);
273275

274276
memcpy(&config->sta.ssid, ssid, ssid_len);
275-
config->sta.ssid[ssid_len] = 0;
277+
if (ssid_len < 32) {
278+
config->sta.ssid[ssid_len] = 0;
279+
}
276280
memcpy(&config->sta.password, password, password_len);
277281
config->sta.password[password_len] = 0;
278282
config->sta.channel = channel;

shared-bindings/wifi/Radio.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ STATIC mp_obj_t wifi_radio_start_ap(size_t n_args, const mp_obj_t *pos_args, mp_
257257

258258
mp_buffer_info_t ssid;
259259
mp_get_buffer_raise(args[ARG_ssid].u_obj, &ssid, MP_BUFFER_READ);
260+
mp_arg_validate_length_range(ssid.len, 1, 32, MP_QSTR_ssid);
260261

261262
mp_buffer_info_t password;
262263
password.len = 0;
@@ -330,9 +331,7 @@ STATIC mp_obj_t wifi_radio_connect(size_t n_args, const mp_obj_t *pos_args, mp_m
330331
mp_buffer_info_t ssid;
331332
ssid.len = 0;
332333
mp_get_buffer_raise(args[ARG_ssid].u_obj, &ssid, MP_BUFFER_READ);
333-
if (ssid.len > 32) {
334-
mp_raise_ValueError(translate("ssid can't be more than 32 bytes"));
335-
}
334+
mp_arg_validate_length_range(ssid.len, 1, 32, MP_QSTR_ssid);
336335

337336
mp_buffer_info_t password;
338337
password.len = 0;

0 commit comments

Comments
 (0)