From 36d989b16c87fe9f83021f43cf1563f07d1c87cf Mon Sep 17 00:00:00 2001 From: "Michael (XIAO Xufeng)" Date: Thu, 24 Mar 2022 16:28:37 +0800 Subject: [PATCH] spi: fixed crash when calling spi_bus_free when not initialized introduced in 49a48644e42458366b2dd7b7d153acc943d50e0f Closes: https://github.com/espressif/esp-idf/issues/8642 --- components/driver/include/driver/spi_common.h | 2 +- components/driver/spi_common.c | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/components/driver/include/driver/spi_common.h b/components/driver/include/driver/spi_common.h index 830b8efd8c..424e9c0588 100644 --- a/components/driver/include/driver/spi_common.h +++ b/components/driver/include/driver/spi_common.h @@ -163,7 +163,7 @@ esp_err_t spi_bus_initialize(spi_host_device_t host_id, const spi_bus_config_t * * @param host_id SPI peripheral to free * @return * - ESP_ERR_INVALID_ARG if parameter is invalid - * - ESP_ERR_INVALID_STATE if not all devices on the bus are freed + * - ESP_ERR_INVALID_STATE if bus hasn't been initialized before, or not all devices on the bus are freed * - ESP_OK on success */ esp_err_t spi_bus_free(spi_host_device_t host_id); diff --git a/components/driver/spi_common.c b/components/driver/spi_common.c index 34f2ebe39a..e4aa3f7577 100644 --- a/components/driver/spi_common.c +++ b/components/driver/spi_common.c @@ -862,6 +862,10 @@ const spi_bus_attr_t* spi_bus_get_attr(spi_host_device_t host_id) esp_err_t spi_bus_free(spi_host_device_t host_id) { + if (bus_ctx[host_id] == NULL) { + return ESP_ERR_INVALID_STATE; + } + esp_err_t err = ESP_OK; spicommon_bus_context_t* ctx = bus_ctx[host_id]; spi_bus_attr_t* bus_attr = &ctx->bus_attr;