From 14097b0bccdac8bedfc0cae24d821e75f060bad8 Mon Sep 17 00:00:00 2001 From: Artem Pikulin Date: Sat, 17 Nov 2018 16:18:55 +0700 Subject: [PATCH] Fix mem leak caused by electrum_txhistory_loop. --- iguana/exchanges/LP_etomic.c | 18 ++++++++++++------ iguana/exchanges/LP_socket.c | 13 ++++++++----- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/iguana/exchanges/LP_etomic.c b/iguana/exchanges/LP_etomic.c index d7b9710f3..0ec33c3c3 100644 --- a/iguana/exchanges/LP_etomic.c +++ b/iguana/exchanges/LP_etomic.c @@ -844,8 +844,10 @@ void LP_etomic_txhistory_loop(void *_coin) result = eth_tx_history_etherscan(coin->smartaddr); if (result) { json = cJSON_Parse(result); - if (json && is_cJSON_Array(jobj(json, "result"))) { - LP_etomic_process_tx_history_json(coin, jobj(json, "result")); + if (json) { + if (is_cJSON_Array(jobj(json, "result"))) { + LP_etomic_process_tx_history_json(coin, jobj(json, "result")); + } free_json(json); } free(result); @@ -856,8 +858,10 @@ void LP_etomic_txhistory_loop(void *_coin) result = internal_eth_tx_history_etherscan(coin->smartaddr); if (result) { json = cJSON_Parse(result); - if (json && is_cJSON_Array(jobj(json, "result"))) { - LP_etomic_process_tx_history_json(coin, jobj(json, "result")); + if (json) { + if (is_cJSON_Array(jobj(json, "result"))) { + LP_etomic_process_tx_history_json(coin, jobj(json, "result")); + } free_json(json); } free(result); @@ -866,8 +870,10 @@ void LP_etomic_txhistory_loop(void *_coin) result = erc20_tx_history_etherscan(coin->smartaddr, coin->etomic); if (result) { json = cJSON_Parse(result); - if (json && is_cJSON_Array(jobj(json, "result"))) { - LP_etomic_process_tx_history_json(coin, jobj(json, "result")); + if (json) { + if (is_cJSON_Array(jobj(json, "result"))) { + LP_etomic_process_tx_history_json(coin, jobj(json, "result")); + } free_json(json); } free(result); diff --git a/iguana/exchanges/LP_socket.c b/iguana/exchanges/LP_socket.c index 42e3c3ba2..e7ddb802b 100644 --- a/iguana/exchanges/LP_socket.c +++ b/iguana/exchanges/LP_socket.c @@ -1237,8 +1237,10 @@ void LP_electrum_get_tx_until_success(struct iguana_info *coin, char *tx_hash, c electrum_jsonarg(coin->symbol, coin->electrum, res, "blockchain.transaction.get", params, ELECTRUM_TIMEOUT); if (jobj(*res, "error") != NULL) { - printf("Error getting electrum tx %s %s %s\n", coin->symbol, jstri(params, 0), jprint(*res, 1)); + char *msg = jprint(*res, 1); + printf("Error getting electrum tx %s %s %s\n", coin->symbol, jstri(params, 0), msg); *res = cJSON_CreateObject(); + free(msg); sleep(5); continue; } else { @@ -1290,16 +1292,16 @@ void LP_electrum_txhistory_loop(void *_coin) ELECTRUM_TIMEOUT); } if (jobj(history, "error") != NULL) { - printf("Error getting electrum history of coin %s %s\n", coin->symbol, jprint(history, 1)); + char *msg = jprint(history, 1); + printf("Error getting electrum history of coin %s %s\n", coin->symbol, msg); + free(msg); sleep(10); continue; } int history_size = cJSON_GetArraySize(history); for (int i = history_size - 1; i >= 0; i--) { cJSON *history_item = jitem(history, i); - cJSON *tx_item = cJSON_CreateObject(); char *tx_hash = jstr(history_item, "tx_hash"); - LP_electrum_get_tx_until_success(coin, tx_hash, &tx_item); struct LP_tx_history_item *iter; int found = 0; int confirmed = 0; @@ -1325,7 +1327,8 @@ void LP_electrum_txhistory_loop(void *_coin) } else { continue; } - + cJSON *tx_item = cJSON_CreateObject(); + LP_electrum_get_tx_until_success(coin, tx_hash, &tx_item); if (!found) { strcpy(item->txid, jstr(tx_item, "txid")); // receive by default, but if at least 1 vin contains our address the category is send