From fa15b326e8faa5a8b8949bada7308d2aa0ce1aee Mon Sep 17 00:00:00 2001 From: marktwtn Date: Sat, 2 Feb 2019 00:31:51 +0800 Subject: [PATCH] Fix memory double free error The memory double free error occurs if we initialize and destroy dcurl repeatedly. dcurl maintains a list of nodes for different hardwares. The error is caused by forgetting to remove the nodes from the list. To fix it, add the corresponding function list_del() in dcurl_destroy(). Besides fixing the error, one of the testcases is modified to repeatedly test the initialization and destruction of dcurl N times. The value of N equals to 5. Close #94. --- src/dcurl.c | 1 + tests/test-dcurl.c | 40 ++++++++++++++++++++++------------------ 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/dcurl.c b/src/dcurl.c index b635e50..d310dd3 100644 --- a/src/dcurl.c +++ b/src/dcurl.c @@ -94,6 +94,7 @@ void dcurl_destroy() list_for_each(p, &IMPL_LIST) { impl = list_entry(p, ImplContext, list); destroyImplContext(impl); + list_del(p); } } diff --git a/tests/test-dcurl.c b/tests/test-dcurl.c index f02c96d..040cb2e 100644 --- a/tests/test-dcurl.c +++ b/tests/test-dcurl.c @@ -2,6 +2,8 @@ #include "common.h" #include "dcurl.h" +#define LOOP_MAX 5 + int main() { char *trytes = @@ -47,27 +49,29 @@ int main() int mwm = 9; - /* test dcurl Implementation with mwm = 9 */ - dcurl_init(); - int8_t *ret_trytes = dcurl_entry((int8_t *) trytes, mwm, 0); - assert(ret_trytes); - dcurl_destroy(); + for (int loop_count = 0; loop_count < LOOP_MAX; loop_count++) { + /* test dcurl Implementation with mwm = 9 */ + dcurl_init(); + int8_t *ret_trytes = dcurl_entry((int8_t *) trytes, mwm, 0); + assert(ret_trytes); + dcurl_destroy(); - Trytes_t *trytes_t = initTrytes(ret_trytes, 2673); - assert(trytes_t); - Trytes_t *hash_trytes = hashTrytes(trytes_t); - assert(hash_trytes); + Trytes_t *trytes_t = initTrytes(ret_trytes, 2673); + assert(trytes_t); + Trytes_t *hash_trytes = hashTrytes(trytes_t); + assert(hash_trytes); - /* Validation */ - Trits_t *ret_trits = trits_from_trytes(hash_trytes); - for (int i = 243 - 1; i >= 243 - mwm; i--) { - assert(ret_trits->data[i] == 0); - } + /* Validation */ + Trits_t *ret_trits = trits_from_trytes(hash_trytes); + for (int i = 243 - 1; i >= 243 - mwm; i--) { + assert(ret_trits->data[i] == 0); + } - free(ret_trytes); - freeTrobject(trytes_t); - freeTrobject(hash_trytes); - freeTrobject(ret_trits); + free(ret_trytes); + freeTrobject(trytes_t); + freeTrobject(hash_trytes); + freeTrobject(ret_trits); + } return 0; }