Skip to content

Commit 1ab7235

Browse files
committed
fix(sdmmc): Legacy driver slot deinit set NULL after free
1 parent 68f9f03 commit 1ab7235

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

components/esp_driver_sdmmc/legacy/src/sdmmc_host.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ sd_host_slot_handle_t sdmmc_get_slot_handle(int slot_id)
9191
return slot_id == 0 ? s_slot0 : s_slot1;
9292
}
9393

94+
static sd_host_slot_handle_t* sdmmc_get_slot_handle_ptr(int slot_id)
95+
{
96+
return slot_id == 0 ? &s_slot0 : &s_slot1;
97+
}
98+
9499
esp_err_t sdmmc_host_is_slot_set_to_uhs1(int slot, bool *is_uhs1)
95100
{
96101
SLOT_CHECK(slot);
@@ -145,8 +150,9 @@ esp_err_t sdmmc_host_init_slot(int slot, const sdmmc_slot_config_t *slot_config)
145150
esp_err_t sdmmc_host_deinit_slot(int slot)
146151
{
147152
esp_err_t ret = ESP_FAIL;
148-
sd_host_slot_handle_t hdl = sdmmc_get_slot_handle(slot);
149-
ESP_RETURN_ON_ERROR(sd_host_remove_slot(hdl), TAG, "failed to remove slot");
153+
sd_host_slot_handle_t* hdl = sdmmc_get_slot_handle_ptr(slot);
154+
ESP_RETURN_ON_ERROR(sd_host_remove_slot(*hdl), TAG, "failed to remove slot");
155+
*hdl = NULL;
150156

151157
ret = sd_host_del_controller(s_ctlr);
152158
//for backward compatibility, return ESP_OK when only slot is removed and host is still there
@@ -160,11 +166,13 @@ esp_err_t sdmmc_host_deinit_slot(int slot)
160166
esp_err_t sdmmc_host_deinit(void)
161167
{
162168
esp_err_t ret = ESP_FAIL;
163-
sd_host_slot_handle_t hdl[2] = {s_slot0, s_slot1};
169+
sd_host_slot_handle_t* hdl_ptrs[2] = {&s_slot0, &s_slot1};
164170
for (int i = 0; i < 2; i++) {
165-
if (hdl[i]) {
166-
ret = sd_host_remove_slot(hdl[i]);
171+
sd_host_slot_handle_t hdl = *hdl_ptrs[i];
172+
if (hdl) {
173+
ret = sd_host_remove_slot(hdl);
167174
ESP_RETURN_ON_ERROR(ret, TAG, "failed to remove slot%d", i);
175+
*hdl_ptrs[i] = NULL;
168176
}
169177
}
170178
ESP_RETURN_ON_ERROR(sd_host_del_controller(s_ctlr), TAG, "failed to delete controller");

0 commit comments

Comments
 (0)