Skip to content

Commit a7ae356

Browse files
author
Seppo Takalo
committed
TDBStore safety check: Erase if there is valid keys on the free space.
In case our are contains data from previous reset() or reset_area(), we might end up in the situation where free space contains valid key headers, but we have not erased that area yet. This can cause failures if the deinit() and init() because new scan of that area would continue as long as keys are found. This causes keys on the not-yet-erased area to be included in the new instance of TDBStore. To prevent this failure, check after each key-write that our free space does not contain valid key headers. Also make sure that we erase one program unit sector over the master record. If we erased just the master record,first key might is still there, causing next init() to find it. Extend erase area by one program unit, so that build_ram_table() won't find any keys.
1 parent 14d5c3a commit a7ae356

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

UNITTESTS/moduletests/storage/kvstore/TDBStore/moduletest.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ TEST_F(TDBStoreModuleTest, reset)
7272
size_t size, read;
7373
// Write so much, that we are sure that garbage collection have kicked up
7474
for (int i = 0; i < 100; ++i) {
75-
size = sprintf(buf, "data%d", i);
75+
size = sprintf(buf, "reset_%d", i);
7676
EXPECT_EQ(tdb.set("key", buf, size, 0), MBED_SUCCESS);
7777
}
7878
EXPECT_EQ(tdb.reset(), MBED_SUCCESS);
@@ -82,6 +82,17 @@ TEST_F(TDBStoreModuleTest, reset)
8282
EXPECT_NE(tdb.get("key", buf, 100, &read), MBED_SUCCESS);
8383
}
8484

85+
TEST_F(TDBStoreModuleTest, remove)
86+
{
87+
char buf[100];
88+
size_t size;
89+
EXPECT_EQ(tdb.set("key", "data1", 5, 0), MBED_SUCCESS);
90+
EXPECT_EQ(tdb.set("key", "data2", 5, 0), MBED_SUCCESS);
91+
EXPECT_EQ(tdb.remove("key"), MBED_SUCCESS);
92+
// Previous key should not be found
93+
EXPECT_NE(tdb.get("key", buf, 100, &size), MBED_SUCCESS);
94+
}
95+
8596
TEST_F(TDBStoreModuleTest, set_deinit_init_get)
8697
{
8798
char buf[100];

features/storage/kvstore/tdbstore/TDBStore.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ TDBStore::~TDBStore()
144144

145145
int TDBStore::read_area(uint8_t area, uint32_t offset, uint32_t size, void *buf)
146146
{
147+
//Check that we are not crossing area boundary
148+
if (offset + size > _size) {
149+
return MBED_ERROR_READ_FAILED;
150+
}
147151
int os_ret = _buff_bd->read(buf, _area_params[area].address + offset, size);
148152

149153
if (os_ret) {
@@ -645,6 +649,15 @@ int TDBStore::set_finalize(set_handle_t handle)
645649

646650
_free_space_offset = align_up(ih->bd_curr_offset, _prog_size);
647651

652+
// Safety check: If there seems to be valid keys on the free space
653+
// we should erase one sector more, just to ensure that in case of power failure
654+
// next init() would not extend the scan phase to that section as well.
655+
os_ret = read_record(_active_area, _free_space_offset, 0, 0, 0, actual_data_size, 0,
656+
false, false, false, false, hash, flags, next_offset);
657+
if (os_ret == MBED_SUCCESS) {
658+
check_erase_before_write(_active_area, _free_space_offset, sizeof(record_header_t));
659+
}
660+
648661
end:
649662
if ((need_gc) && (ih->bd_base_offset != _master_record_offset)) {
650663
garbage_collection();
@@ -941,6 +954,7 @@ int TDBStore::increment_max_keys(void **ram_table)
941954
// Reallocate ram table with new size
942955
ram_table_entry_t *old_ram_table = (ram_table_entry_t *) _ram_table;
943956
ram_table_entry_t *new_ram_table = new ram_table_entry_t[_max_keys + 1];
957+
memset(new_ram_table, 0, sizeof(ram_table_entry_t)*(_max_keys + 1));
944958

945959
// Copy old content to new table
946960
memcpy(new_ram_table, old_ram_table, sizeof(ram_table_entry_t) * _max_keys);
@@ -987,6 +1001,7 @@ int TDBStore::init()
9871001
_max_keys = initial_max_keys;
9881002

9891003
ram_table = new ram_table_entry_t[_max_keys];
1004+
memset(ram_table, 0, sizeof(ram_table_entry_t) * _max_keys);
9901005
_ram_table = ram_table;
9911006
_num_keys = 0;
9921007

@@ -1127,7 +1142,7 @@ int TDBStore::reset_area(uint8_t area)
11271142
bool copy_reserved_data = do_reserved_data_get(buf, sizeof(buf), 0, buf + RESERVED_AREA_SIZE) == MBED_SUCCESS;
11281143

11291144
// Erase reserved area and master record
1130-
ret = check_erase_before_write(area, 0, _master_record_offset + _master_record_size, true);
1145+
ret = check_erase_before_write(area, 0, _master_record_offset + _master_record_size + _prog_size, true);
11311146
if (ret) {
11321147
return ret;
11331148
}
@@ -1150,7 +1165,7 @@ int TDBStore::reset()
11501165

11511166
// Reset both areas
11521167
for (area = 0; area < _num_areas; area++) {
1153-
ret = check_erase_before_write(area, 0, _master_record_offset + _master_record_size, true);
1168+
ret = check_erase_before_write(area, 0, _master_record_offset + _master_record_size + _prog_size, true);
11541169
if (ret) {
11551170
goto end;
11561171
}
@@ -1160,7 +1175,7 @@ int TDBStore::reset()
11601175
_num_keys = 0;
11611176
_free_space_offset = _master_record_offset;
11621177
_active_area_version = 1;
1163-
1178+
memset(_ram_table, 0, sizeof(ram_table_entry_t) * _max_keys);
11641179
// Write an initial master record on active area
11651180
ret = write_master_record(_active_area, _active_area_version, _free_space_offset);
11661181

features/storage/kvstore/tdbstore/TDBStore.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,8 @@ class TDBStore : public KVStore {
368368
*
369369
* @param[in] area Area.
370370
* @param[in] offset Offset of record in area.
371-
* @param[in] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
372-
* @param[in] data_buf Data buffer.
371+
* @param[out] key Key - must not include '*' '/' '?' ':' ';' '\' '"' '|' ' ' '<' '>' '\'.
372+
* @param[out] data_buf Data buffer.
373373
* @param[in] data_buf_size Data buffer size.
374374
* @param[out] actual_data_size Actual data size.
375375
* @param[in] data_offset Offset in data.

0 commit comments

Comments
 (0)