Kaynağa Gözat

esp_flash: fix the cleanup when add device fails

Armando 5 yıl önce
ebeveyn
işleme
59c1bf4d8f
1 değiştirilmiş dosya ile 15 ekleme ve 3 silme
  1. 15 3
      components/spi_flash/esp_flash_spi_init.c

+ 15 - 3
components/spi_flash/esp_flash_spi_init.c

@@ -112,17 +112,28 @@ esp_err_t spi_bus_add_flash_device(esp_flash_t **out_chip, const esp_flash_spi_d
     if (config->host_id == SPI_HOST) caps = MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT;
 
     chip = (esp_flash_t*)heap_caps_malloc(sizeof(esp_flash_t), caps);
-    host = (spi_flash_host_driver_t*)heap_caps_malloc(sizeof(spi_flash_host_driver_t), caps);
-    host_data = (memspi_host_data_t*)heap_caps_malloc(sizeof(memspi_host_data_t), caps);
-    if (!chip || !host || !host_data) {
+    if (!chip) {
         ret = ESP_ERR_NO_MEM;
         goto fail;
     }
 
+    host = (spi_flash_host_driver_t*)heap_caps_malloc(sizeof(spi_flash_host_driver_t), caps);
     *chip = (esp_flash_t) {
         .read_mode = config->io_mode,
         .host = host,
     };
+    if (!host) {
+        ret = ESP_ERR_NO_MEM;
+        goto fail;
+    }
+
+    host_data = (memspi_host_data_t*)heap_caps_malloc(sizeof(memspi_host_data_t), caps);
+    host->driver_data = host_data;
+    if (!host_data) {
+        ret = ESP_ERR_NO_MEM;
+        goto fail;
+    }
+
     esp_err_t err = esp_flash_init_os_functions(chip, config->host_id);
     if (err != ESP_OK) {
         ret = err;
@@ -147,6 +158,7 @@ esp_err_t spi_bus_add_flash_device(esp_flash_t **out_chip, const esp_flash_spi_d
     *out_chip = chip;
     return ret;
 fail:
+    // The memory allocated are free'd in the `spi_bus_remove_flash_device`.
     spi_bus_remove_flash_device(chip);
     return ret;
 }