diff --git a/components/nvs_flash/src/nvs_partition_manager.cpp b/components/nvs_flash/src/nvs_partition_manager.cpp index b95db7e816..abf563630f 100644 --- a/components/nvs_flash/src/nvs_partition_manager.cpp +++ b/components/nvs_flash/src/nvs_partition_manager.cpp @@ -1,16 +1,8 @@ -// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at - -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/* + * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #include "esp_partition.h" #include "nvs_partition_manager.hpp" #include "nvs_partition_lookup.hpp" @@ -188,6 +180,10 @@ esp_err_t NVSPartitionManager::open_handle(const char *part_name, uint8_t nsIndex; Storage* sHandle; + if (handle == nullptr) { + return ESP_ERR_INVALID_ARG; + } + if (nvs_storage_list.empty()) { return ESP_ERR_NVS_NOT_INITIALIZED; } @@ -202,14 +198,14 @@ esp_err_t NVSPartitionManager::open_handle(const char *part_name, return err; } - *handle = new (std::nothrow) NVSHandleSimple(open_mode==NVS_READONLY, nsIndex, sHandle); - - if (handle == nullptr) { + NVSHandleSimple* new_handle = new (std::nothrow) NVSHandleSimple(open_mode==NVS_READONLY, nsIndex, sHandle); + if (new_handle == nullptr) { return ESP_ERR_NO_MEM; } - nvs_handles.push_back(*handle); + nvs_handles.push_back(new_handle); + *handle = new_handle; return ESP_OK; } diff --git a/components/nvs_flash/test_nvs_host/test_partition_manager.cpp b/components/nvs_flash/test_nvs_host/test_partition_manager.cpp index b2db3a9cfe..a8d7b630a6 100644 --- a/components/nvs_flash/test_nvs_host/test_partition_manager.cpp +++ b/components/nvs_flash/test_nvs_host/test_partition_manager.cpp @@ -1,16 +1,8 @@ -// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at - -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/* + * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #include "catch.hpp" #include #include @@ -70,6 +62,21 @@ TEST_CASE("Partition manager initializes multiple partitions", "[partition_mgr]" REQUIRE(NVSPartitionManager::get_instance()->deinit_partition(part_1.get_partition_name()) == ESP_OK); } +TEST_CASE("Partition manager open fails on null handle", "[partition_mgr]") +{ + const uint32_t NVS_FLASH_SECTOR = 6; + const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3; + PartitionEmulationFixture f(0, 10, "test"); + + REQUIRE(NVSPartitionManager::get_instance()->init_custom(&f.part, NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN) + == ESP_OK); + + CHECK(NVSPartitionManager::get_instance()->open_handle("test", "ns_1", NVS_READWRITE, nullptr) + == ESP_ERR_INVALID_ARG); + + NVSPartitionManager::get_instance()->deinit_partition("test"); +} + TEST_CASE("Partition manager invalidates handle on partition de-init", "[partition_mgr]") { const uint32_t NVS_FLASH_SECTOR = 6;