From 5d2e255bea1070cdb6f9fd5c48889384e2f4f51a Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Thu, 5 Oct 2023 20:49:17 -0700 Subject: [PATCH] fix record deletion - also zero out rest of record when setting --- Arena.cc | 2 +- TLV.cc | 20 +++++++++++++++----- tlv_test.cc | 6 +++++- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/Arena.cc b/Arena.cc index ce75e95..f77c075 100644 --- a/Arena.cc +++ b/Arena.cc @@ -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; } diff --git a/TLV.cc b/TLV.cc index f0b852d..bf52f4a 100644 --- a/TLV.cc +++ b/TLV.cc @@ -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++; + } } diff --git a/tlv_test.cc b/tlv_test.cc index 54f113a..f021c3e 100644 --- a/tlv_test.cc +++ b/tlv_test.cc @@ -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; }