From 1d44e40fb16f3f87fb3ad1d17c996299b785a060 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Fri, 9 Jul 2021 11:24:25 +0200 Subject: [PATCH] hal: spi_flash: avoid calling memcpy with NULL buffer In practice, calling memcpy with NULL buffer and 0 size works on the ESP32, but the C standard considers this an undefined behavior. When building with UBSAN checks enabled, compiler will insert a check that memcpy argument is non-NULL, regardless of the size argument. This caused a failure in tools/test_apps/system/panic, which is built with USBAN enabled for several components. --- components/hal/spi_flash_hal_common.inc | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/components/hal/spi_flash_hal_common.inc b/components/hal/spi_flash_hal_common.inc index 780f84c065..d728393d65 100644 --- a/components/hal/spi_flash_hal_common.inc +++ b/components/hal/spi_flash_hal_common.inc @@ -174,7 +174,9 @@ esp_err_t spi_flash_hal_common_command(spi_flash_host_inst_t *host, spi_flash_tr spi_flash_ll_set_miso_bitlen(dev, trans->miso_len * 8); spi_flash_ll_user_start(dev); host->driver->poll_cmd_done(host); - spi_flash_ll_get_buffer_data(dev, trans->miso_data, trans->miso_len); + if (trans->miso_len > 0) { + spi_flash_ll_get_buffer_data(dev, trans->miso_data, trans->miso_len); + } return ESP_OK; } @@ -188,6 +190,8 @@ esp_err_t spi_flash_hal_read(spi_flash_host_inst_t *host, void *buffer, uint32_t spi_flash_ll_set_miso_bitlen(dev, read_len * 8); spi_flash_ll_user_start(dev); host->driver->poll_cmd_done(host); - spi_flash_ll_get_buffer_data(dev, buffer, read_len); + if (read_len > 0) { + spi_flash_ll_get_buffer_data(dev, buffer, read_len); + } return ESP_OK; }