mirror of
https://github.com/bluekitchen/btstack.git
synced 2025-02-20 18:40:31 +00:00
btstack_tlv_posix: only keep last value in memory, fix delete operation
This commit is contained in:
parent
8008788713
commit
39f043d5d3
@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
||||
- L2CAP ERTM: avoid read-after-free on decline of incoming connection
|
||||
- GATT Client: set uuid16 to zero when deserializing uuid128 services, characteristics, and descriptors
|
||||
- att_db_util: fix realloc of att db buffer for large attributes
|
||||
- btstack_tlv_posix: only keep last value in memory, fix delete operation
|
||||
|
||||
### Added
|
||||
- att_db_util: provide GATT Database Hash via att_db_util_hash_calc
|
||||
|
@ -188,26 +188,38 @@ static int btstack_tlv_posix_read_db(btstack_tlv_posix_t * self){
|
||||
file_valid = 1;
|
||||
break;
|
||||
}
|
||||
if (entries_read == sizeof(entry)){
|
||||
uint32_t tag = big_endian_read_32(entry, 0);
|
||||
uint32_t len = big_endian_read_32(entry, 4);
|
||||
// arbitrary safetly check: values < 1000 bytes each
|
||||
if (len > 1000) break;
|
||||
tlv_entry_t * new_entry = (tlv_entry_t *) malloc(sizeof(tlv_entry_t) - DUMMY_SIZE + len);
|
||||
if (!new_entry) return 0;
|
||||
new_entry->tag = tag;
|
||||
new_entry->len = len;
|
||||
// read
|
||||
size_t value_read = fread(&new_entry->value[0], 1, len, self->file);
|
||||
if (value_read == len){
|
||||
// append new entry
|
||||
btstack_linked_list_add(&self->entry_list, (btstack_linked_item_t *) new_entry);
|
||||
} else {
|
||||
// fail
|
||||
free(new_entry);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (entries_read != sizeof(entry)) break;
|
||||
|
||||
uint32_t tag = big_endian_read_32(entry, 0);
|
||||
uint32_t len = big_endian_read_32(entry, 4);
|
||||
|
||||
// arbitrary safety check: values < 1000 bytes each
|
||||
if (len > 1000) break;
|
||||
|
||||
// create new entry for regular tag
|
||||
tlv_entry_t * new_entry = NULL;
|
||||
if (len > 0) {
|
||||
new_entry = (tlv_entry_t *) malloc(sizeof(tlv_entry_t) - DUMMY_SIZE + len);
|
||||
if (!new_entry) return 0;
|
||||
new_entry->tag = tag;
|
||||
new_entry->len = len;
|
||||
|
||||
// read
|
||||
size_t value_read = fread(&new_entry->value[0], 1, len, self->file);
|
||||
if (value_read != len) break;
|
||||
}
|
||||
|
||||
// remove old entry
|
||||
tlv_entry_t * old_entry = btstack_tlv_posix_find_entry(self, tag);
|
||||
if (old_entry){
|
||||
btstack_linked_list_remove(&self->entry_list, (btstack_linked_item_t *) old_entry);
|
||||
free(old_entry);
|
||||
}
|
||||
|
||||
// append new entry
|
||||
if (new_entry){
|
||||
btstack_linked_list_add(&self->entry_list, (btstack_linked_item_t *) new_entry);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -160,6 +160,28 @@ TEST(BSTACK_TLV, TestWriteResetRead){
|
||||
CHECK_EQUAL(buffer, data);
|
||||
}
|
||||
|
||||
TEST(BSTACK_TLV, TestWriteDeleteResetReadDeleteRead){
|
||||
uint32_t tag = TAG('a','b','c','d');
|
||||
uint8_t data = 7;
|
||||
uint8_t buffer = data;
|
||||
int size;
|
||||
|
||||
btstack_tlv_impl->store_tag(&btstack_tlv_context, tag, &buffer, 1);
|
||||
btstack_tlv_impl->delete_tag(&btstack_tlv_context, tag);
|
||||
|
||||
reopen_db();
|
||||
|
||||
size = btstack_tlv_impl->get_tag(&btstack_tlv_context, tag, NULL, 0);
|
||||
CHECK_EQUAL(size, 0);
|
||||
|
||||
// assert that it's really gone and not just shadowed by delete tag
|
||||
btstack_tlv_impl->delete_tag(&btstack_tlv_context, tag);
|
||||
|
||||
size = btstack_tlv_impl->get_tag(&btstack_tlv_context, tag, NULL, 0);
|
||||
CHECK_EQUAL(size, 0);
|
||||
}
|
||||
|
||||
|
||||
int main (int argc, const char * argv[]){
|
||||
hci_dump_open("tlv_test.pklg", HCI_DUMP_PACKETLOGGER);
|
||||
return CommandLineTestRunner::RunAllTests(argc, argv);
|
||||
|
Loading…
x
Reference in New Issue
Block a user