Skip to content

Commit eec536b

Browse files
author
Cruz Monrreal
authored
Merge pull request #8986 from davidsaada/david_tdbstore_fixes
Fix a few bugs in TDBStore and KV config
2 parents 40e13cf + e1bd5c9 commit eec536b

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

features/storage/kvstore/conf/kv_config.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,12 @@ int _storage_config_TDB_INTERNAL()
572572
return MBED_ERROR_INVALID_ARGUMENT;
573573
}
574574

575+
ret = kvstore_config.internal_bd->deinit();
576+
if (ret != MBED_SUCCESS) {
577+
tr_error("KV Config: Fail to deinit internal BlockDevice.");
578+
return MBED_ERROR_FAILED_OPERATION;
579+
}
580+
575581
static TDBStore tdb_internal(kvstore_config.internal_bd);
576582
kvstore_config.internal_store = &tdb_internal;
577583

@@ -737,7 +743,7 @@ int _storage_config_tdb_external_common()
737743

738744
if (_calculate_blocksize_match_tdbstore(kvstore_config.external_bd) != MBED_SUCCESS) {
739745
tr_error("KV Config: Can not create TDBStore with less then 2 sector.");
740-
return MBED_ERROR_INVALID_ARGUMENT;
746+
return MBED_ERROR_INVALID_SIZE;
741747
}
742748

743749
static TDBStore tdb_external(kvstore_config.external_bd);

features/storage/kvstore/kv_map/KVMap.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@ int KVMap::attach(const char *partition_name, kvstore_config_t *kv_config)
7878

7979
void KVMap::deinit_partition(kv_map_entry_t *partition)
8080
{
81-
free(partition->partition_name);
82-
8381
if (partition->kv_config == NULL) {
8482
return;
8583
}
@@ -88,22 +86,15 @@ void KVMap::deinit_partition(kv_map_entry_t *partition)
8886
partition->kv_config->external_store->deinit();
8987
}
9088

89+
// TODO: this should be removed after FS APIs are standardized
9190
if (partition->kv_config->external_fs != NULL) {
9291
partition->kv_config->external_fs->unmount();
9392
}
9493

95-
if (partition->kv_config->external_bd != NULL) {
96-
partition->kv_config->external_bd->deinit();
97-
}
98-
9994
if (partition->kv_config->internal_store != NULL) {
10095
partition->kv_config->internal_store->deinit();
10196
}
10297

103-
if (partition->kv_config->internal_bd != NULL) {
104-
partition->kv_config->internal_bd->deinit();
105-
}
106-
10798
if (partition->kv_config->kvstore_main_instance != NULL) {
10899
partition->kv_config->kvstore_main_instance->deinit();
109100
}

features/storage/kvstore/tdbstore/TDBStore.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include <string.h>
2323
#include <stdio.h>
2424
#include "mbed_error.h"
25-
#include "mbed_assert.h"
2625
#include "mbed_wait_api.h"
2726
#include "MbedCRC.h"
2827

@@ -228,6 +227,11 @@ int TDBStore::read_record(uint8_t area, uint32_t offset, char *key,
228227

229228
total_size = key_size + data_size;
230229

230+
// Make sure our read sizes didn't cause any wraparounds
231+
if ((total_size < key_size) || (total_size < data_size)) {
232+
return MBED_ERROR_INVALID_DATA_DETECTED;
233+
}
234+
231235
if (offset + total_size >= _size) {
232236
return MBED_ERROR_INVALID_DATA_DETECTED;
233237
}
@@ -883,6 +887,7 @@ int TDBStore::build_ram_table()
883887

884888
if (ret == MBED_ERROR_ITEM_NOT_FOUND) {
885889
// Key doesn't exist, need to add it to RAM table
890+
ret = MBED_SUCCESS;
886891

887892
if (flags & delete_flag) {
888893
continue;
@@ -946,6 +951,10 @@ int TDBStore::init()
946951

947952
_mutex.lock();
948953

954+
if (_is_initialized) {
955+
goto end;
956+
}
957+
949958
_max_keys = initial_max_keys;
950959

951960
ram_table = new ram_table_entry_t[_max_keys];

0 commit comments

Comments
 (0)