Skip to content

Commit 84381fa

Browse files
committed
esp8266/modnetwork: Protect scan() callback against memory errors.
scan() allocates memory so may cause an exception to be raised.
1 parent 55df14f commit 84381fa

1 file changed

Lines changed: 19 additions & 10 deletions

File tree

esp8266/modnetwork.c

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -136,16 +136,25 @@ STATIC void esp_scan_cb(scaninfo *si, STATUS status) {
136136
return;
137137
}
138138
if (si->pbss && status == 0) {
139-
struct bss_info *bs;
140-
STAILQ_FOREACH(bs, si->pbss, next) {
141-
mp_obj_tuple_t *t = mp_obj_new_tuple(6, NULL);
142-
t->items[0] = mp_obj_new_bytes(bs->ssid, strlen((char*)bs->ssid));
143-
t->items[1] = mp_obj_new_bytes(bs->bssid, sizeof(bs->bssid));
144-
t->items[2] = MP_OBJ_NEW_SMALL_INT(bs->channel);
145-
t->items[3] = MP_OBJ_NEW_SMALL_INT(bs->rssi);
146-
t->items[4] = MP_OBJ_NEW_SMALL_INT(bs->authmode);
147-
t->items[5] = MP_OBJ_NEW_SMALL_INT(bs->is_hidden);
148-
mp_obj_list_append(*esp_scan_list, MP_OBJ_FROM_PTR(t));
139+
// we need to catch any memory errors
140+
nlr_buf_t nlr;
141+
if (nlr_push(&nlr) == 0) {
142+
struct bss_info *bs;
143+
STAILQ_FOREACH(bs, si->pbss, next) {
144+
mp_obj_tuple_t *t = mp_obj_new_tuple(6, NULL);
145+
t->items[0] = mp_obj_new_bytes(bs->ssid, strlen((char*)bs->ssid));
146+
t->items[1] = mp_obj_new_bytes(bs->bssid, sizeof(bs->bssid));
147+
t->items[2] = MP_OBJ_NEW_SMALL_INT(bs->channel);
148+
t->items[3] = MP_OBJ_NEW_SMALL_INT(bs->rssi);
149+
t->items[4] = MP_OBJ_NEW_SMALL_INT(bs->authmode);
150+
t->items[5] = MP_OBJ_NEW_SMALL_INT(bs->is_hidden);
151+
mp_obj_list_append(*esp_scan_list, MP_OBJ_FROM_PTR(t));
152+
}
153+
nlr_pop();
154+
} else {
155+
mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(nlr.ret_val));
156+
// indicate error
157+
*esp_scan_list = MP_OBJ_NULL;
149158
}
150159
} else {
151160
// indicate error

0 commit comments

Comments
 (0)