fix record deletion

- also zero out rest of record when setting
This commit is contained in:
Kyle Isom 2023-10-05 20:49:17 -07:00
parent 8b8be9421a
commit 5d2e255bea
3 changed files with 21 additions and 7 deletions

View File

@ -107,7 +107,7 @@ create_arena(Arena &arena, const char *path, size_t size, mode_t mode)
}
}
fd = open(path, O_WRONLY|O_CREAT, mode);
fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, mode);
if (fd == -1) {
return -1;
}

20
TLV.cc
View File

@ -53,9 +53,12 @@ write_to_memory(Arena &arena, uint8_t *cursor, Record &rec)
void
set_record(Record &rec, uint8_t tag, uint8_t len, const char *val)
{
uint8_t trail = TLV_MAX_LEN-len;
rec.Tag = tag;
rec.Len = len;
memcpy(rec.Val, val, len);
memset(rec.Val+len, 0, trail);
}
@ -127,14 +130,21 @@ delete_record(Arena &arena, uint8_t *cursor)
return;
}
uintptr_t len = cursor[1] + 2;
uintptr_t stop = (uintptr_t)arena.size - (uintptr_t)cursor;
uint8_t len = cursor[1] + 2;
uint8_t *stop = arena.store + arena.size;
stop -= len;
for (uintptr_t i = (uintptr_t)cursor; i < stop; i++) {
cursor[i] = cursor[i+len];
};
while (cursor != stop) {
cursor[0] = cursor[len];
cursor++;
}
stop += len;
while (cursor != stop) {
cursor[0] = 0;
cursor++;
}
}

View File

@ -60,8 +60,12 @@ tlv_test_suite(Arena &backend)
TLV::set_record(rec4, 3, TEST_STRLEN3, TEST_STR3);
assert(TLV::write_to_memory(backend, NULL, rec4));
rec4.Tag = 2;
cursor = TLV::find_tag(backend, NULL, rec4);
assert(cursor != NULL);
TLV::delete_record(backend, cursor);
assert(cursor[0] == TAG_EMPTY);
assert(cursor[0] == 3);
return true;
}