bugfix (nvs): fixed wrong pointer check

This commit is contained in:
Michael (XIAO Xufeng) 2021-11-25 02:48:11 +00:00 committed by Jakob Hasse
parent c84dfb3a80
commit 43643716a2
3 changed files with 33 additions and 32 deletions

View File

@ -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;
}

View File

@ -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 <algorithm>
#include <cstring>
@ -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;

View File

@ -1489,7 +1489,6 @@ components/nvs_flash/src/nvs_partition.cpp
components/nvs_flash/src/nvs_partition.hpp
components/nvs_flash/src/nvs_partition_lookup.cpp
components/nvs_flash/src/nvs_partition_lookup.hpp
components/nvs_flash/src/nvs_partition_manager.cpp
components/nvs_flash/src/nvs_partition_manager.hpp
components/nvs_flash/src/nvs_platform.hpp
components/nvs_flash/src/nvs_storage.cpp
@ -1513,7 +1512,6 @@ components/nvs_flash/test_nvs_host/test_nvs_handle.cpp
components/nvs_flash/test_nvs_host/test_nvs_initialization.cpp
components/nvs_flash/test_nvs_host/test_nvs_partition.cpp
components/nvs_flash/test_nvs_host/test_nvs_storage.cpp
components/nvs_flash/test_nvs_host/test_partition_manager.cpp
components/nvs_flash/test_nvs_host/test_spi_flash_emulation.cpp
components/openssl/include/internal/ssl3.h
components/openssl/include/internal/ssl_cert.h