From f3be0395d22d0dea07a7f56cc250a25e99f5d00e Mon Sep 17 00:00:00 2001 From: baohongde Date: Wed, 30 Aug 2017 18:01:24 +0800 Subject: [PATCH] component/bt: fix nvs_get_str_or_blob length output error --- components/nvs_flash/src/nvs_api.cpp | 3 +++ components/nvs_flash/test_nvs_host/test_nvs.cpp | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/components/nvs_flash/src/nvs_api.cpp b/components/nvs_flash/src/nvs_api.cpp index 57759ecd8..920965793 100644 --- a/components/nvs_flash/src/nvs_api.cpp +++ b/components/nvs_flash/src/nvs_api.cpp @@ -400,7 +400,10 @@ static esp_err_t nvs_get_str_or_blob(nvs_handle handle, nvs::ItemType type, cons } else if (*length < dataSize) { *length = dataSize; return ESP_ERR_NVS_INVALID_LENGTH; + } else if (*length > dataSize) { + *length = dataSize; } + return entry.mStoragePtr->readItem(entry.mNsIndex, type, key, out_value, dataSize); } diff --git a/components/nvs_flash/test_nvs_host/test_nvs.cpp b/components/nvs_flash/test_nvs_host/test_nvs.cpp index 5a35c11f4..f419256cf 100644 --- a/components/nvs_flash/test_nvs_host/test_nvs.cpp +++ b/components/nvs_flash/test_nvs_host/test_nvs.cpp @@ -530,6 +530,10 @@ TEST_CASE("nvs api tests", "[nvs]") TEST_ESP_ERR(ESP_ERR_NVS_INVALID_LENGTH, nvs_get_str(handle_2, "key", buf, &buf_len_short)); CHECK(buf_len_short == buf_len); + size_t buf_len_long = buf_len + 1; + TEST_ESP_OK(nvs_get_str(handle_2, "key", buf, &buf_len_long)); + CHECK(buf_len_long == buf_len); + TEST_ESP_OK(nvs_get_str(handle_2, "key", buf, &buf_len)); CHECK(0 == strcmp(buf, str));