From 75b24c5e7902b2a41cc09533ffddbfc43f13bcc4 Mon Sep 17 00:00:00 2001 From: Sam Chen Date: Mon, 29 Jun 2020 15:08:24 +0800 Subject: [PATCH] Fixed #33, memory leak detected in bundle_miner. --- utils/bundle_miner.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/utils/bundle_miner.c b/utils/bundle_miner.c index 79a72bf..50943a2 100644 --- a/utils/bundle_miner.c +++ b/utils/bundle_miner.c @@ -103,7 +103,7 @@ retcode_t bundle_miner_mine(byte_t const *const bundle_normalized_max, uint8_t c bundle_miner_ctx_t *const ctxs, size_t num_ctxs, bool *const optimal_index_found) { retcode_t ret = RC_OK; uint32_t rounded_count = count + (num_ctxs - count % num_ctxs); - thread_handle_t *threads = (thread_handle_t *)malloc(sizeof(thread_handle_t) * num_ctxs); + thread_handle_t *threads = NULL; uint64_t start_index = 0; double probability = 1.0; @@ -115,8 +115,10 @@ retcode_t bundle_miner_mine(byte_t const *const bundle_normalized_max, uint8_t c return RC_UTILS_BUNDLE_MINER_BAD_PARAM; } + threads = (thread_handle_t *)malloc(sizeof(thread_handle_t) * num_ctxs); if (ctxs == NULL || threads == NULL) { - return RC_OOM; + ret = RC_OOM; + goto done; } start_index = trits_to_long(essence + OBSOLETE_TAG_OFFSET, NUM_TRITS_OBSOLETE_TAG); @@ -154,7 +156,9 @@ retcode_t bundle_miner_mine(byte_t const *const bundle_normalized_max, uint8_t c done: - free(threads); + if (threads) { + free(threads); + } return ret; }