diff --git a/components/nvs_flash/src/intrusive_list.h b/components/nvs_flash/src/intrusive_list.h index 798b718a66..9a8c6f228e 100644 --- a/components/nvs_flash/src/intrusive_list.h +++ b/components/nvs_flash/src/intrusive_list.h @@ -30,9 +30,14 @@ class intrusive_list public: - class iterator : public std::iterator + class iterator { public: + using iterator_category = std::forward_iterator_tag; + using value_type = T; + using difference_type = ptrdiff_t; + using pointer = T*; + using reference = T&; iterator() : mPos(nullptr) {} diff --git a/components/nvs_flash/src/nvs_api.cpp b/components/nvs_flash/src/nvs_api.cpp index 8d63c25469..6d7f546050 100644 --- a/components/nvs_flash/src/nvs_api.cpp +++ b/components/nvs_flash/src/nvs_api.cpp @@ -116,7 +116,7 @@ extern "C" esp_err_t nvs_flash_init_partition_ptr(const esp_partition_t *partiti return init_res; } -#ifndef LINUX_TARGET +#ifndef LINUX_HOST_LEGACY_TEST extern "C" esp_err_t nvs_flash_init_partition(const char *part_name) { esp_err_t lock_result = Lock::init(); @@ -239,7 +239,7 @@ extern "C" esp_err_t nvs_flash_erase(void) { return nvs_flash_erase_partition(NVS_DEFAULT_PART_NAME); } -#endif // ! LINUX_TARGET +#endif // LINUX_HOST_LEGACY_TEST extern "C" esp_err_t nvs_flash_deinit_partition(const char* partition_name) { diff --git a/components/nvs_flash/src/nvs_page.cpp b/components/nvs_flash/src/nvs_page.cpp index dbae73060e..36cde3db25 100644 --- a/components/nvs_flash/src/nvs_page.cpp +++ b/components/nvs_flash/src/nvs_page.cpp @@ -128,37 +128,11 @@ esp_err_t Page::writeEntryData(const uint8_t* data, size_t size) NVS_ASSERT_OR_RETURN(mFirstUsedEntry != INVALID_ENTRY, ESP_FAIL); const uint16_t count = size / ENTRY_SIZE; - const uint8_t* buf = data; - -#if !defined LINUX_TARGET - // TODO: check whether still necessary with esp_partition* API - /* On the ESP32, data can come from DROM, which is not accessible by spi_flash_write - * function. To work around this, we copy the data to heap if it came from DROM. - * Hopefully this won't happen very often in practice. For data from DRAM, we should - * still be able to write it to flash directly. - * TODO: figure out how to make this platform-specific check nicer (probably by introducing - * a platform-specific flash layer). - */ - if ((uint32_t) data < 0x3ff00000) { - buf = (uint8_t*) malloc(size); - if (!buf) { - return ESP_ERR_NO_MEM; - } - memcpy((void*)buf, data, size); - } -#endif // ! LINUX_TARGET - uint32_t phyAddr; esp_err_t rc = getEntryAddress(mNextFreeEntry, &phyAddr); if (rc == ESP_OK) { - rc = mPartition->write(phyAddr, buf, size); + rc = mPartition->write(phyAddr, data, size); } - -#if !defined LINUX_TARGET - if (buf != data) { - free((void*)buf); - } -#endif // ! LINUX_TARGET if (rc != ESP_OK) { mState = PageState::INVALID; return rc; diff --git a/components/nvs_flash/src/nvs_pagemanager.cpp b/components/nvs_flash/src/nvs_pagemanager.cpp index cdc6b221dd..062f627743 100644 --- a/components/nvs_flash/src/nvs_pagemanager.cpp +++ b/components/nvs_flash/src/nvs_pagemanager.cpp @@ -47,7 +47,10 @@ esp_err_t PageManager::load(Partition *partition, uint32_t baseSector, uint32_t return activatePage(); } else { uint32_t lastSeqNo; - ESP_ERROR_CHECK( mPageList.back().getSeqNumber(lastSeqNo) ); + auto err = mPageList.back().getSeqNumber(lastSeqNo); + if (err != ESP_OK) { + return err; + } mSeqNumber = lastSeqNo + 1; } diff --git a/components/nvs_flash/src/nvs_storage.cpp b/components/nvs_flash/src/nvs_storage.cpp index 1cf3b94859..03172f1a96 100644 --- a/components/nvs_flash/src/nvs_storage.cpp +++ b/components/nvs_flash/src/nvs_storage.cpp @@ -4,6 +4,10 @@ * SPDX-License-Identifier: Apache-2.0 */ #include "nvs_storage.hpp" +#if __has_include() +// for strlcpy +#include +#endif #ifndef ESP_PLATFORM // We need NO_DEBUG_STORAGE here since the integration tests on the host add some debug code. @@ -305,7 +309,10 @@ esp_err_t Storage::writeItem(uint8_t nsIndex, ItemType datatype, const char* key if (findPage->state() == Page::PageState::UNINITIALIZED || findPage->state() == Page::PageState::INVALID) { - ESP_ERROR_CHECK(findItem(nsIndex, datatype, key, findPage, item)); + err = findItem(nsIndex, datatype, key, findPage, item); + if (err != ESP_OK) { + return err; + } } /* Get the version of the previous index with same */ prevStart = item.blobIndex.chunkStart; @@ -383,7 +390,10 @@ esp_err_t Storage::writeItem(uint8_t nsIndex, ItemType datatype, const char* key if (findPage) { if (findPage->state() == Page::PageState::UNINITIALIZED || findPage->state() == Page::PageState::INVALID) { - ESP_ERROR_CHECK(findItem(nsIndex, datatype, key, findPage, item)); + err = findItem(nsIndex, datatype, key, findPage, item); + if (err != ESP_OK) { + return err; + } } err = findPage->eraseItem(nsIndex, datatype, key); if (err == ESP_ERR_FLASH_OP_FAIL) { @@ -749,11 +759,7 @@ void Storage::fillEntryInfo(Item &item, nvs_entry_info_t &info) for (auto &name : mNamespaces) { if(item.nsIndex == name.mIndex) { -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wstringop-truncation" - strncpy(info.namespace_name, name.mName, sizeof(info.namespace_name) - 1); -#pragma GCC diagnostic pop - info.namespace_name[sizeof(info.namespace_name) -1] = '\0'; + strlcpy(info.namespace_name, name.mName, sizeof(info.namespace_name)); break; } } diff --git a/components/nvs_flash/test_nvs_host/Makefile b/components/nvs_flash/test_nvs_host/Makefile index a7843759f5..ffa5a920c3 100644 --- a/components/nvs_flash/test_nvs_host/Makefile +++ b/components/nvs_flash/test_nvs_host/Makefile @@ -38,10 +38,14 @@ COMPILER := gcc endif CPPFLAGS += -I../private_include -I../include -I../src -I../../esp_rom/include -I../../esp_rom/include/linux -I../../log/include -I./ -I../../esp_common/include -I../../esp32/include -I ../../mbedtls/mbedtls/include -I ../../spi_flash/include -I ../../hal/include -I ../../xtensa/include -I ../../../tools/catch -fprofile-arcs -ftest-coverage -g2 -ggdb -CFLAGS += -fprofile-arcs -ftest-coverage -DLINUX_TARGET -CXXFLAGS += -std=c++11 -Wall -Werror -DLINUX_TARGET +CFLAGS += -fprofile-arcs -ftest-coverage -DLINUX_TARGET -DLINUX_HOST_LEGACY_TEST +CXXFLAGS += -std=c++11 -Wall -Werror -DLINUX_TARGET -DLINUX_HOST_LEGACY_TEST LDFLAGS += -lstdc++ -Wall -fprofile-arcs -ftest-coverage +ifeq ($(shell uname -s),Linux) +LDFLAGS += -lbsd +endif + ifeq ($(COMPILER),clang) CFLAGS += -fsanitize=address CXXFLAGS += -fsanitize=address @@ -52,13 +56,16 @@ OBJ_FILES = $(SOURCE_FILES:.cpp=.o) OBJ_FILES_C = $(SOURCE_FILES_C:.c=.o) COVERAGE_FILES = $(OBJ_FILES:.o=.gc*) +MBEDTLS_LIB := ../../mbedtls/mbedtls/library/libmbedcrypto.a $(OBJ_FILES): %.o: %.cpp $(OBJ_FILES_C): %.c: %.c -$(TEST_PROGRAM): clean-coverage $(OBJ_FILES) $(OBJ_FILES_C) +$(MBEDTLS_LIB): $(MAKE) -C ../../mbedtls/mbedtls/ lib - g++ $(LDFLAGS) -o $(TEST_PROGRAM) $(OBJ_FILES) $(OBJ_FILES_C) ../../mbedtls/mbedtls/library/libmbedcrypto.a + +$(TEST_PROGRAM): $(OBJ_FILES) $(OBJ_FILES_C) $(MBEDTLS_LIB) | clean-coverage + g++ -o $@ $^ $(LDFLAGS) $(OUTPUT_DIR): mkdir -p $(OUTPUT_DIR)