From c8dbdc6aa643b50fec13351ebfe398a1c7eb5fc8 Mon Sep 17 00:00:00 2001 From: Mahavir Jain Date: Thu, 2 May 2024 13:24:45 +0530 Subject: [PATCH] fix(tests): correct the flash write length for NVS encrypted test Write only till the embedded file size in the NVS partition. Earlier the length was kept as the whole partition size and it could result in accessing embedded rodata beyond the MMU mapped range. --- components/nvs_flash/test/test_nvs.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/components/nvs_flash/test/test_nvs.c b/components/nvs_flash/test/test_nvs.c index c2318ebe50..42340c3b13 100644 --- a/components/nvs_flash/test/test_nvs.c +++ b/components/nvs_flash/test/test_nvs.c @@ -1,4 +1,6 @@ #include +#include + #include #include #include @@ -483,6 +485,7 @@ TEST_CASE("test nvs apis for nvs partition generator utility with encryption ena extern const char nvs_key_end[] asm("_binary_encryption_keys_bin_end"); extern const char nvs_data_start[] asm("_binary_partition_encrypted_bin_start"); + extern const char nvs_data_end[] asm("_binary_partition_encrypted_bin_end"); extern const char sample_bin_start[] asm("_binary_sample_bin_start"); @@ -505,7 +508,11 @@ TEST_CASE("test nvs apis for nvs partition generator utility with encryption ena ESP_ERROR_CHECK( esp_partition_write(key_part, i, nvs_key_start + i, SPI_FLASH_SEC_SIZE) ); } - for (int i = 0; i < nvs_part->size; i+= SPI_FLASH_SEC_SIZE) { + const int content_size = nvs_data_end - nvs_data_start - 1; + TEST_ASSERT_TRUE((content_size % SPI_FLASH_SEC_SIZE) == 0); + + const int size_to_write = MIN(content_size, nvs_part->size); + for (int i = 0; i < size_to_write; i+= SPI_FLASH_SEC_SIZE) { ESP_ERROR_CHECK( esp_partition_write(nvs_part, i, nvs_data_start + i, SPI_FLASH_SEC_SIZE) ); }