Skip to content

Commit 8d9436a

Browse files
committed
Merge pull request #1057 from UncleGrumpy/esp32_gpio_stop
Fix bug in ESP32 gpio:stop/0 and gpio:close/1 This fixes the bug that causes the VM to crash when the GPIO port driver is stopped on the ESP32 platform (issue #816). Also adds missing `NULL` check when the `GPIOData` struct is malloc'd. These changes are made under both the "Apache 2.0" and the "GNU Lesser General Public License 2.1 or later" license terms (dual license). SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
2 parents 24edc6b + b61cc87 commit 8d9436a

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ easily support integration with Esp32 callbacks
1919
- ESP32-S3: crash in network driver caused by a smaller stack size for scheduler threads, when
2020
calling `esp_wifi_init()`. See also issue [#1059](https://github.com/atomvm/AtomVM/issues/1059).
2121
- Fixed Esp32 network driver on non-SMP builds
22+
- ESP32: fixed bug in `gpio:stop/0` and `gpio:close/1` that would cause the VM to crash.
2223

2324
### Changed
2425

src/platforms/esp32/components/avm_builtins/gpio_driver.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,11 @@ Context *gpio_driver_create_port(GlobalContext *global, term opts)
297297
Context *ctx = context_new(global);
298298

299299
struct GPIOData *gpio_data = malloc(sizeof(struct GPIOData));
300+
if (IS_NULL_PTR(gpio_data)) {
301+
ESP_LOGE(TAG, "Not enough free memory to initialize GPIO port driver!");
302+
scheduler_terminate(ctx);
303+
return NULL;
304+
}
300305
list_init(&gpio_data->gpio_listeners);
301306

302307
ctx->native_handler = consume_gpio_mailbox;
@@ -332,7 +337,7 @@ static term gpiodriver_close(Context *ctx)
332337
struct GPIOListenerData *gpio_listener = GET_LIST_ENTRY(item, struct GPIOListenerData, gpio_listener_list_head);
333338
gpio_num = gpio_listener->gpio;
334339
list_remove(&gpio_listener->gpio_listener_list_head);
335-
list_remove(&gpio_listener->listener.listeners_list_head);
340+
sys_unregister_listener(ctx->global, &gpio_listener->listener);
336341
free(gpio_listener);
337342

338343
gpio_set_intr_type(gpio_num, GPIO_INTR_DISABLE);
@@ -344,6 +349,7 @@ static term gpiodriver_close(Context *ctx)
344349
}
345350
}
346351

352+
ctx->platform_data = NULL;
347353
globalcontext_unregister_process(glb, gpio_atom_index);
348354
free(gpio_data);
349355

0 commit comments

Comments
 (0)