From eadc541657981050b87d66b45d175c5f4e22c3fb Mon Sep 17 00:00:00 2001 From: "oljas.jarasbaev" <oljas.jarasbaev@demlabs.net> Date: Sun, 23 Jul 2023 14:43:53 +0000 Subject: [PATCH] bugfix-9198 --- dap-sdk | 1 + modules/chain/dap_chain.c | 2 +- modules/chain/dap_chain_ledger.c | 59 ++++++++- modules/chain/include/dap_chain_ledger.h | 3 + .../dap_stream_ch_chain_net_srv.c | 119 +++++++++++++++++- modules/common/dap_chain_datum.c | 1 + modules/common/dap_chain_datum_token.c | 12 ++ modules/common/include/dap_chain_common.h | 3 + .../block-pos/dap_chain_cs_block_pos.c | 2 +- .../consensus/dag-poa/dap_chain_cs_dag_poa.c | 1 - .../consensus/esbocs/dap_chain_cs_esbocs.c | 31 ++++- modules/net/dap_chain_net.c | 44 +++++-- modules/net/dap_chain_net_decree.c | 2 +- modules/net/dap_chain_net_tx.c | 41 +++--- modules/net/dap_chain_node_cli_cmd.c | 59 ++++++--- modules/net/dap_chain_node_cli_cmd_tx.c | 12 ++ modules/net/dap_chain_node_dns_server.c | 3 + modules/net/dap_chain_node_ping.c | 6 + modules/net/srv/dap_chain_net_srv.c | 6 + modules/net/srv/dap_chain_net_srv_client.c | 2 +- modules/net/srv/include/dap_chain_net_srv.h | 1 + .../service/datum/dap_chain_net_srv_datum.c | 7 +- .../dap_chain_net_srv_stake_pos_delegate.c | 13 +- modules/service/vpn/dap_chain_net_srv_vpn.c | 19 ++- .../service/vpn/dap_chain_net_vpn_client.c | 3 + .../xchange/dap_chain_net_srv_xchange.c | 10 +- modules/type/blocks/dap_chain_cs_blocks.c | 4 - modules/type/dag/dap_chain_cs_dag.c | 15 ++- modules/wallet/dap_chain_wallet.c | 29 +++++ 29 files changed, 443 insertions(+), 67 deletions(-) create mode 160000 dap-sdk diff --git a/dap-sdk b/dap-sdk new file mode 160000 index 0000000000..a8b0473a00 --- /dev/null +++ b/dap-sdk @@ -0,0 +1 @@ +Subproject commit a8b0473a000912ba69f016f4f092d3eb5b7eaae0 diff --git a/modules/chain/dap_chain.c b/modules/chain/dap_chain.c index 87579e9962..318b8857fe 100644 --- a/modules/chain/dap_chain.c +++ b/modules/chain/dap_chain.c @@ -759,10 +759,10 @@ int dap_cert_chain_file_save(dap_chain_datum_t *datum, char *net_name) char *cert_path_c = dap_canonicalize_filename(cert_path, NULL); // Protect the ca folder from using "/.." in cert_name if(dap_strncmp(s_system_chain_ca_dir, cert_path_c, dap_strlen(s_system_chain_ca_dir))) { + log_it(L_ERROR, "Cert path '%s' is not in ca dir: %s", cert_path_c, s_system_chain_ca_dir); dap_cert_delete(cert); DAP_DELETE(cert_path_c); DAP_DELETE(cert_path); - log_it(L_ERROR, "Cert path '%s' is not in ca dir: %s", cert_path_c, s_system_chain_ca_dir); return -1; } int l_ret = dap_cert_file_save(cert, cert_path_c); diff --git a/modules/chain/dap_chain_ledger.c b/modules/chain/dap_chain_ledger.c index 93700d21d6..68fc6e8676 100644 --- a/modules/chain/dap_chain_ledger.c +++ b/modules/chain/dap_chain_ledger.c @@ -660,6 +660,13 @@ static bool s_ledger_token_update_check(dap_chain_ledger_token_item_t *a_cur_tok } //Check added signs dap_chain_datum_token_t *l_token_tmp = DAP_DUP_SIZE(a_token_update, a_token_update_size); + if (!l_token_tmp) { + log_it(L_ERROR, "Memory allocation error in s_ledger_token_update_check"); + dap_list_free1(l_tsd_list_added_pkeys); + dap_list_free1(l_tsd_list_remote_pkeys); + return false; + } + l_token_tmp->header_native_update.tsd_total_size = 0; isAccepted = true; for (dap_list_t *l_ptr = l_tsd_list_added_pkeys; l_ptr; l_ptr = dap_list_next(l_ptr)) { @@ -840,6 +847,10 @@ static void s_tx_header_print(dap_string_t *a_str_out, dap_chain_datum_tx_t *a_t char * dap_ledger_token_tx_item_list(dap_ledger_t * a_ledger, dap_chain_addr_t *a_addr, const char *a_hash_out_type) { dap_string_t *l_str_out =dap_string_new(NULL); + if (!l_str_out) { + log_it(L_ERROR, "Memory allocation error in dap_ledger_token_tx_item_list"); + return NULL; + } //dap_chain_tx_hash_processed_ht_t *l_tx_data_ht = NULL; dap_chain_ledger_tx_item_t *l_tx_item, *l_tx_tmp; @@ -1078,7 +1089,7 @@ static bool s_ledger_update_token_add_in_hash_table(dap_chain_ledger_token_item_ * @return */ int dap_chain_ledger_token_add(dap_ledger_t *a_ledger, dap_chain_datum_token_t *a_token, size_t a_token_size) { - if (!a_ledger) { + if (!a_ledger || !a_token) { debug_if(s_debug_more, L_ERROR, "NULL ledger, can't add datum with token declaration!"); return -1; } @@ -1112,11 +1123,13 @@ int dap_chain_ledger_token_add(dap_ledger_t *a_ledger, dap_chain_datum_token_t * if (l_token_item) { if (l_token->type != DAP_CHAIN_DATUM_TOKEN_TYPE_UPDATE) { log_it(L_ERROR, "Duplicate token declaration for ticker '%s'", l_token->ticker); + DAP_DEL_Z(l_token); return -3; } if (s_ledger_token_update_check(l_token_item, l_token, l_token_size)) { if (!s_ledger_update_token_add_in_hash_table(l_token_item, l_token, l_token_size)) { log_it(L_ERROR, "Failed to update token with ticker '%s' in ledger", l_token->ticker); + DAP_DEL_Z(l_token); return -5; } if (!IS_ZERO_256(l_token->total_supply)) { @@ -1129,10 +1142,12 @@ int dap_chain_ledger_token_add(dap_ledger_t *a_ledger, dap_chain_datum_token_t * DAP_DELETE(l_token_item->datum_token); } else { log_it(L_ERROR, "Token with ticker '%s' update check failed", l_token->ticker); + DAP_DEL_Z(l_token); return -2; } } else if (l_token->type == DAP_CHAIN_DATUM_TOKEN_TYPE_UPDATE) { log_it(L_WARNING, "Token with ticker '%s' does not yet exist, declare it first", l_token->ticker); + DAP_DEL_Z(l_token); return -6; } @@ -1141,13 +1156,12 @@ int dap_chain_ledger_token_add(dap_ledger_t *a_ledger, dap_chain_datum_token_t * dap_sign_t **l_signs = dap_chain_datum_token_signs_parse(l_token, l_token_size, &l_auth_signs_total, &l_auth_signs_valid); if (!l_signs || !l_auth_signs_total) { log_it(L_ERROR, "No auth signs in token '%s' datum!", l_token->ticker); - DAP_DELETE(l_token); + DAP_DEL_Z(l_token); return -7; } l_token_item = DAP_NEW_Z(dap_chain_ledger_token_item_t); if ( !l_token_item ) { - if (l_token) - DAP_DELETE(l_token); + DAP_DEL_Z(l_token); log_it(L_ERROR, "Memory allocation error in dap_chain_ledger_token_add"); return -8; } @@ -2212,7 +2226,16 @@ static void s_load_cache_gdb_loaded_balances_callback(dap_global_db_context_t *a dap_ledger_private_t * l_ledger_pvt = PVT(l_ledger); for (size_t i = 0; i < a_values_count; i++) { dap_ledger_wallet_balance_t *l_balance_item = DAP_NEW_Z(dap_ledger_wallet_balance_t); + if (!l_balance_item) { + log_it (L_ERROR, "Memory allocation error in s_load_cache_gdb_loaded_balances_callback"); + return; + } l_balance_item->key = DAP_NEW_Z_SIZE(char, strlen(a_values[i].key) + 1); + if (!l_balance_item->key) { + log_it (L_ERROR, "Memory allocation error in s_load_cache_gdb_loaded_balances_callback"); + DAP_DEL_Z(l_balance_item); + return; + } strcpy(l_balance_item->key, a_values[i].key); char *l_ptr = strchr(l_balance_item->key, ' '); if (l_ptr++) { @@ -2474,6 +2497,10 @@ void dap_chain_ledger_load_cache(dap_ledger_t *a_ledger) dap_ledger_t *dap_chain_ledger_create(uint16_t a_flags, char *a_net_name, const char *a_net_native_ticker, dap_list_t *a_poa_certs) { dap_ledger_t *l_ledger = dap_chain_ledger_handle_new(); + if (!l_ledger) { + log_it(L_ERROR, "Memory allocation error indap_chain_ledger_create"); + return NULL; + } l_ledger->net_name = a_net_name; dap_ledger_private_t *l_ledger_pvt = PVT(l_ledger); l_ledger_pvt->net_native_ticker = a_net_native_ticker; @@ -2506,6 +2533,15 @@ dap_ledger_t *dap_chain_ledger_create(uint16_t a_flags, char *a_net_name, const char **l_whitelist = dap_config_get_array_str(l_cfg, "ledger", "hard_accept_list", &l_whitelist_size); for (uint16_t i = 0; i < l_whitelist_size; ++i) { dap_ledger_hal_item_t *l_hal_item = DAP_NEW_Z(dap_ledger_hal_item_t); + if (!l_hal_item) { + log_it(L_ERROR, "Memory allocation error indap_chain_ledger_create"); + DAP_DEL_Z(l_ledger_pvt); + DAP_DEL_Z(l_ledger); + dap_config_close(l_cfg); + DAP_DELETE (l_entry_name); + closedir(l_chains_dir); + return NULL; + } dap_chain_hash_fast_from_str(l_whitelist[i], &l_hal_item->hash); HASH_ADD(hh, s_hal_items, hash, sizeof(l_hal_item->hash), l_hal_item); } @@ -2847,6 +2883,10 @@ static inline int s_token_emission_add(dap_ledger_t *a_ledger, byte_t *a_token_e : &l_ledger_pvt->threshold_emissions_rwlock); if (!l_token_emission_item) { l_token_emission_item = DAP_NEW_Z(dap_chain_ledger_token_emission_item_t); + if ( !l_token_emission_item ) { + log_it(L_ERROR, "Memory allocation error in s_token_emission_add"); + return DAP_CHAIN_LEDGER_EMISSION_ADD_MEMORY_PROBLEM; + } l_token_emission_item->datum_token_emission_size = a_token_emission_size; l_token_emission_item->datum_token_emission_hash = *a_emission_hash; if (l_token_item) { @@ -3128,7 +3168,6 @@ void dap_chain_ledger_addr_get_token_ticker_all_depricated(dap_ledger_t *a_ledge size_t l_tickers_pos = 0; if(l_tx_item) { - l_tickers_size = 10; l_tickers = DAP_NEW_Z_SIZE(char *, l_tickers_size * sizeof(char*)); if ( !l_tickers ) { log_it(L_ERROR, "Memory allocation error in dap_chain_ledger_addr_get_token_ticker_all_depricated"); @@ -3188,6 +3227,11 @@ void dap_chain_ledger_addr_get_token_ticker_all(dap_ledger_t *a_ledger, dap_chai if (l_count && a_tickers){ dap_chain_ledger_token_item_t * l_token_item, *l_tmp; char **l_tickers = DAP_NEW_Z_SIZE(char*, l_count * sizeof(char*)); + if (!l_tickers) { + log_it(L_ERROR, "Memory allocation error in dap_chain_ledger_addr_get_token_ticker_all"); + pthread_rwlock_unlock(&PVT(a_ledger)->balance_accounts_rwlock); + return; + } l_count = 0; HASH_ITER(hh, PVT(a_ledger)->tokens, l_token_item, l_tmp) { l_tickers[l_count] = dap_strdup(l_token_item->ticker); @@ -3203,6 +3247,11 @@ void dap_chain_ledger_addr_get_token_ticker_all(dap_ledger_t *a_ledger, dap_chai size_t l_count = HASH_COUNT(PVT(a_ledger)->balance_accounts); if(l_count && a_tickers){ char **l_tickers = DAP_NEW_Z_SIZE(char*, l_count * sizeof(char*)); + if (!l_tickers) { + log_it(L_ERROR, "Memory allocation error in dap_chain_ledger_addr_get_token_ticker_all"); + pthread_rwlock_unlock(&PVT(a_ledger)->balance_accounts_rwlock); + return; + } l_count = 0; char *l_addr = dap_chain_addr_to_str(a_addr); pthread_rwlock_rdlock(&PVT(a_ledger)->balance_accounts_rwlock); diff --git a/modules/chain/include/dap_chain_ledger.h b/modules/chain/include/dap_chain_ledger.h index 77744f24bb..8e9bbd7f56 100644 --- a/modules/chain/include/dap_chain_ledger.h +++ b/modules/chain/include/dap_chain_ledger.h @@ -166,6 +166,9 @@ void dap_chain_ledger_set_local_cell_id(dap_ledger_t *a_ledger, dap_chain_cell_i DAP_STATIC_INLINE dap_chain_hash_fast_t* dap_chain_node_datum_tx_calc_hash(dap_chain_datum_tx_t *a_tx) { dap_chain_hash_fast_t *tx_hash = DAP_NEW_Z(dap_chain_hash_fast_t); + if (!tx_hash) { + return NULL; + } dap_hash_fast(a_tx, dap_chain_datum_tx_get_size(a_tx), tx_hash); return tx_hash; } diff --git a/modules/channel/chain-net-srv/dap_stream_ch_chain_net_srv.c b/modules/channel/chain-net-srv/dap_stream_ch_chain_net_srv.c index 9adf331969..fa9319b670 100644 --- a/modules/channel/chain-net-srv/dap_stream_ch_chain_net_srv.c +++ b/modules/channel/chain-net-srv/dap_stream_ch_chain_net_srv.c @@ -105,6 +105,12 @@ static inline void s_grace_error(dap_chain_net_srv_grace_t *a_grace, dap_stream_ pthread_mutex_unlock(&a_grace->usage->service->banlist_mutex); else { l_item = DAP_NEW_Z(dap_chain_net_srv_banlist_item_t); + if (!l_item) { + log_it(L_ERROR, "Memory allocation error in s_grace_error"); + DAP_DELETE(a_grace->request); + DAP_DELETE(a_grace); + return; + } l_item->client_pkey_hash = a_grace->usage->client_pkey_hash; l_item->ht_mutex = &a_grace->usage->service->banlist_mutex; l_item->ht_head = &a_grace->usage->service->ban_list; @@ -257,6 +263,16 @@ static void s_service_start(dap_stream_ch_t* a_ch , dap_stream_ch_chain_net_srv_ l_err.usage_id = l_usage->id; // Create one client l_usage->client = DAP_NEW_Z( dap_chain_net_srv_client_remote_t); + if (!l_usage->client) { + log_it(L_ERROR, "Memory allocation errror in s_service_start"); + l_err.code = DAP_STREAM_CH_CHAIN_NET_SRV_PKT_TYPE_RESPONSE_ERROR_CODE_ALLOC_MEMORY_ERROR; + if(a_ch) + dap_stream_ch_pkt_write_unsafe(a_ch, DAP_STREAM_CH_CHAIN_NET_SRV_PKT_TYPE_RESPONSE_ERROR, &l_err, sizeof (l_err)); + if (l_srv && l_srv->callbacks.response_error) + l_srv->callbacks.response_error(l_srv, 0, NULL, &l_err, sizeof(l_err)); + DAP_DEL_Z(l_usage); + return; + } l_usage->client->stream_worker = a_ch->stream_worker; l_usage->client->ch = a_ch; l_usage->client->session_id = a_ch->stream->session->id; @@ -268,7 +284,30 @@ static void s_service_start(dap_stream_ch_t* a_ch , dap_stream_ch_chain_net_srv_ dap_chain_net_srv_grace_t *l_grace = DAP_NEW_Z(dap_chain_net_srv_grace_t); + if (!l_grace) { + log_it(L_ERROR, "Memory allocation errror in s_service_start"); + l_err.code = DAP_STREAM_CH_CHAIN_NET_SRV_PKT_TYPE_RESPONSE_ERROR_CODE_ALLOC_MEMORY_ERROR; + if(a_ch) + dap_stream_ch_pkt_write_unsafe(a_ch, DAP_STREAM_CH_CHAIN_NET_SRV_PKT_TYPE_RESPONSE_ERROR, &l_err, sizeof (l_err)); + if (l_srv && l_srv->callbacks.response_error) + l_srv->callbacks.response_error(l_srv, 0, NULL, &l_err, sizeof(l_err)); + DAP_DEL_Z(l_usage->client); + DAP_DEL_Z(l_usage); + return; + } l_grace->request = DAP_NEW_Z_SIZE(dap_stream_ch_chain_net_srv_pkt_request_t, a_request_size); + if (!l_grace->request) { + log_it(L_ERROR, "Memory allocation errror in s_service_start"); + l_err.code = DAP_STREAM_CH_CHAIN_NET_SRV_PKT_TYPE_RESPONSE_ERROR_CODE_ALLOC_MEMORY_ERROR; + if(a_ch) + dap_stream_ch_pkt_write_unsafe(a_ch, DAP_STREAM_CH_CHAIN_NET_SRV_PKT_TYPE_RESPONSE_ERROR, &l_err, sizeof (l_err)); + if (l_srv && l_srv->callbacks.response_error) + l_srv->callbacks.response_error(l_srv, 0, NULL, &l_err, sizeof(l_err)); + DAP_DEL_Z(l_usage->client); + DAP_DEL_Z(l_usage); + DAP_DEL_Z(l_grace); + return; + } memcpy(l_grace->request, a_request, a_request_size); l_grace->request_size = a_request_size; l_grace->ch_uuid = a_ch->uuid; @@ -285,6 +324,19 @@ static void s_service_start(dap_stream_ch_t* a_ch , dap_stream_ch_chain_net_srv_ size_t l_success_size = sizeof (dap_stream_ch_chain_net_srv_pkt_success_hdr_t ); dap_stream_ch_chain_net_srv_pkt_success_t *l_success = DAP_NEW_Z_SIZE(dap_stream_ch_chain_net_srv_pkt_success_t, l_success_size); + if(!l_success) { + log_it(L_ERROR, "Memory allocation error in s_service_start"); + l_err.code = DAP_STREAM_CH_CHAIN_NET_SRV_PKT_TYPE_RESPONSE_ERROR_CODE_ALLOC_MEMORY_ERROR; + if(a_ch) + dap_stream_ch_pkt_write_unsafe(a_ch, DAP_STREAM_CH_CHAIN_NET_SRV_PKT_TYPE_RESPONSE_ERROR, &l_err, sizeof (l_err)); + if (l_srv && l_srv->callbacks.response_error) + l_srv->callbacks.response_error(l_srv, 0, NULL, &l_err, sizeof(l_err)); + DAP_DEL_Z(l_usage->client); + DAP_DEL_Z(l_usage); + DAP_DEL_Z(l_grace->request); + DAP_DEL_Z(l_grace); + return; + } l_success->hdr.usage_id = l_usage->id; l_success->hdr.net_id.uint64 = l_usage->net->pub.id.uint64; l_success->hdr.srv_uid.uint64 = l_usage->service->uid.uint64; @@ -329,7 +381,12 @@ static void s_grace_period_start(dap_chain_net_srv_grace_t *a_grace) a_grace->usage->is_grace = true; l_price = DAP_NEW_Z(dap_chain_net_srv_price_t); - memcpy(l_price, a_grace->usage->service->pricelist, sizeof(*l_price)); + if (!l_price) { + log_it(L_ERROR, "Memory allocation error in s_grace_period_start"); + s_grace_error(a_grace, l_err); + return; + } + memcpy(l_price, a_grace->usage->service->pricelist, sizeof(*l_price)); a_grace->usage->price = l_price; @@ -339,6 +396,12 @@ static void s_grace_period_start(dap_chain_net_srv_grace_t *a_grace) a_grace->usage->receipt, a_grace->usage->receipt->size); } usages_in_grace_t *l_item = DAP_NEW_Z_SIZE(usages_in_grace_t, sizeof(usages_in_grace_t)); + if (!l_item) { + log_it(L_ERROR, "Memory allocation error in s_grace_period_start"); + DAP_DEL_Z(l_price); + s_grace_error(a_grace, l_err); + return; + } l_item->grace = a_grace; l_item->tx_cond_hash = a_grace->usage->tx_cond_hash; @@ -599,8 +662,24 @@ static bool s_grace_period_finish(usages_in_grace_t *a_grace_item) // DAP_DEL_Z(l_grace->usage->receipt_next); log_it(L_ERROR, "Tx cond have not enough funds"); dap_chain_net_srv_grace_t* l_grace_new = DAP_NEW_Z(dap_chain_net_srv_grace_t); + if (!l_grace_new) { + log_it(L_ERROR, "Memory allocation error in s_grace_period_finish"); + DAP_DELETE(a_grace_item->grace->request); + DAP_DEL_Z(a_grace_item->grace); + HASH_DEL(s_grace_table, a_grace_item); + DAP_DEL_Z(a_grace_item); + return false; + } // Parse the request l_grace_new->request = DAP_NEW_Z_SIZE(dap_stream_ch_chain_net_srv_pkt_request_t, sizeof(dap_stream_ch_chain_net_srv_pkt_request_t)); + if (!l_grace_new->request) { + log_it(L_ERROR, "Memory allocation error in s_grace_period_finish"); + DAP_DELETE(a_grace_item->grace->request); + DAP_DEL_Z(a_grace_item->grace); + HASH_DEL(s_grace_table, a_grace_item); + DAP_DEL_Z(a_grace_item); + return false; + } l_grace_new->request->hdr.net_id = a_grace_item->grace->usage->net->pub.id; memcpy(l_grace_new->request->hdr.token, a_grace_item->grace->usage->token_ticker, strlen(a_grace_item->grace->usage->token_ticker)); l_grace_new->request->hdr.srv_uid = a_grace_item->grace->usage->service->uid; @@ -723,6 +802,10 @@ void s_stream_ch_packet_in(dap_stream_ch_t* a_ch , void* a_arg) break; } dap_stream_ch_chain_net_srv_pkt_request_t *l_request = DAP_NEW_Z_SIZE(dap_stream_ch_chain_net_srv_pkt_request_t, l_ch_pkt->hdr.data_size); + if (!l_request) { + log_it(L_ERROR, "Memory allocation error in s_stream_ch_packet_in"); + return; + } memcpy(l_request, l_ch_pkt->data, l_ch_pkt->hdr.data_size); l_ch_chain_net_srv->srv_uid.uint64 = l_request->hdr.srv_uid.uint64; s_service_start(a_ch, l_request, l_ch_pkt->hdr.data_size); @@ -840,12 +923,20 @@ void s_stream_ch_packet_in(dap_stream_ch_t* a_ch , void* a_arg) if (! l_usage->receipt_next && l_usage->receipt){ DAP_DEL_Z(l_usage->receipt); l_usage->receipt = DAP_NEW_SIZE(dap_chain_datum_tx_receipt_t,l_receipt_size); + if (!l_usage->receipt_next) { + log_it(L_ERROR, "Memory allocation error in s_stream_ch_packet_in"); + return; + } l_is_first_sign = true; l_usage->is_active = true; memcpy( l_usage->receipt, l_receipt, l_receipt_size); } else if (l_usage->receipt_next ){ DAP_DEL_Z(l_usage->receipt_next); l_usage->receipt_next = DAP_NEW_SIZE(dap_chain_datum_tx_receipt_t,l_receipt_size); + if (!l_usage->receipt_next) { + log_it(L_ERROR, "Memory allocation error in s_stream_ch_packet_in"); + return; + } l_usage->is_active = true; memcpy( l_usage->receipt_next, l_receipt, l_receipt_size); } @@ -877,9 +968,20 @@ void s_stream_ch_packet_in(dap_stream_ch_t* a_ch , void* a_arg) // TX not found in ledger and we not in grace, start grace log_it(L_ERROR, "Can't find tx cond. Start grace!"); l_grace = DAP_NEW_Z(dap_chain_net_srv_grace_t); + if (!l_grace) { + log_it(L_ERROR, "Memory allocation error in s_stream_ch_packet_in"); + DAP_DELETE(l_tx_in_hash_str); + return; + } UNUSED(l_grace); // Parse the request l_grace->request = DAP_NEW_Z_SIZE(dap_stream_ch_chain_net_srv_pkt_request_t, sizeof(dap_stream_ch_chain_net_srv_pkt_request_t)); + if (!l_grace->request) { + log_it(L_ERROR, "Memory allocation error in s_stream_ch_packet_in"); + DAP_DEL_Z(l_grace) + DAP_DELETE(l_tx_in_hash_str); + return; + } l_grace->request->hdr.net_id = l_usage->net->pub.id; memcpy(l_grace->request->hdr.token, l_usage->token_ticker, strlen(l_usage->token_ticker)); l_grace->request->hdr.srv_uid = l_usage->service->uid; @@ -897,8 +999,19 @@ void s_stream_ch_packet_in(dap_stream_ch_t* a_ch , void* a_arg) log_it(L_ERROR, "Tx cond have not enough funds"); l_usage->is_waiting_new_tx_cond = true; l_grace = DAP_NEW_Z(dap_chain_net_srv_grace_t); + if (!l_grace) { + log_it(L_ERROR, "Memory allocation error in s_stream_ch_packet_in"); + DAP_DELETE(l_tx_in_hash_str); + return; + } // Parse the request l_grace->request = DAP_NEW_Z_SIZE(dap_stream_ch_chain_net_srv_pkt_request_t, sizeof(dap_stream_ch_chain_net_srv_pkt_request_t)); + if (!l_grace->request) { + log_it(L_ERROR, "Memory allocation error in s_stream_ch_packet_in"); + DAP_DEL_Z(l_grace) + DAP_DELETE(l_tx_in_hash_str); + return; + } l_grace->request->hdr.net_id = l_usage->net->pub.id; memcpy(l_grace->request->hdr.token, l_usage->token_ticker, strlen(l_usage->token_ticker)); l_grace->request->hdr.srv_uid = l_usage->service->uid; @@ -1078,6 +1191,10 @@ void s_stream_ch_packet_in(dap_stream_ch_t* a_ch , void* a_arg) size_t l_success_size = sizeof (dap_stream_ch_chain_net_srv_pkt_success_hdr_t ); dap_stream_ch_chain_net_srv_pkt_success_t *l_success = DAP_NEW_Z_SIZE(dap_stream_ch_chain_net_srv_pkt_success_t, l_success_size); + if(!l_success) { + log_it(L_ERROR, "Memory allocation error in s_stream_ch_packet_in"); + return; + } l_success->hdr.usage_id = l_usage->id; l_success->hdr.net_id.uint64 = l_usage->net->pub.id.uint64; l_success->hdr.srv_uid.uint64 = l_usage->service->uid.uint64; diff --git a/modules/common/dap_chain_datum.c b/modules/common/dap_chain_datum.c index fa21942e37..cb8dc686b6 100644 --- a/modules/common/dap_chain_datum.c +++ b/modules/common/dap_chain_datum.c @@ -359,6 +359,7 @@ bool dap_chain_datum_dump_tx(dap_chain_datum_tx_t *a_datum, dap_sign_t *l_client = DAP_NEW_Z(dap_sign_t); if (!l_client) { log_it(L_ERROR, "Memory allocation error in dap_chain_datum_dump_tx"); + DAP_DEL_Z(l_provider); DAP_DELETE(l_value_str); DAP_DELETE(l_coins_str); return false; diff --git a/modules/common/dap_chain_datum_token.c b/modules/common/dap_chain_datum_token.c index 7dd41df3e2..8cd63f2c7f 100644 --- a/modules/common/dap_chain_datum_token.c +++ b/modules/common/dap_chain_datum_token.c @@ -384,12 +384,20 @@ dap_chain_datum_token_emission_t *dap_chain_datum_emission_read(byte_t *a_emissi { assert(a_emission_serial); assert(a_emission_size); + if(!a_emission_serial || !a_emission_size || *a_emission_size < sizeof(struct dap_chain_emission_header_v0)) { + log_it(L_ERROR, "Invalid params in dap_chain_datum_emission_read"); + return NULL; + } dap_chain_datum_token_emission_t *l_emission = NULL; if (((dap_chain_datum_token_emission_t *)a_emission_serial)->hdr.version == 0) { size_t l_emission_size = *a_emission_size; size_t l_old_hdr_size = sizeof(struct dap_chain_emission_header_v0); size_t l_add_size = sizeof(l_emission->hdr) - l_old_hdr_size; l_emission = DAP_NEW_Z_SIZE(dap_chain_datum_token_emission_t, l_emission_size + l_add_size); + if (!l_emission) { + log_it(L_ERROR, "Memory allocation error in dap_chain_datum_emission_read"); + return NULL; + } l_emission->hdr.version = 2; memcpy(l_emission, a_emission_serial, l_old_hdr_size); memcpy((byte_t *)l_emission + sizeof(l_emission->hdr), @@ -401,6 +409,10 @@ dap_chain_datum_token_emission_t *dap_chain_datum_emission_read(byte_t *a_emissi (*a_emission_size) = l_emission_size; } else { l_emission = DAP_DUP_SIZE(a_emission_serial, *a_emission_size); + if (!l_emission) { + log_it(L_ERROR, "Memory allocation error in dap_chain_datum_emission_read"); + return NULL; + } if (((dap_chain_datum_token_emission_t *)a_emission_serial)->hdr.version == 1) l_emission->hdr.value_256 = dap_chain_uint256_from( ((dap_chain_datum_token_emission_t *)a_emission_serial)->hdr.value); diff --git a/modules/common/include/dap_chain_common.h b/modules/common/include/dap_chain_common.h index 4debbf0e12..2dd92b3c5b 100644 --- a/modules/common/include/dap_chain_common.h +++ b/modules/common/include/dap_chain_common.h @@ -107,6 +107,9 @@ DAP_STATIC_INLINE bool dap_chain_node_addr_str_check(const char *a_addr_str) { DAP_STATIC_INLINE int dap_chain_node_addr_from_str(dap_chain_node_addr_t *a_addr, const char *a_addr_str) { + if (!a_addr){ + return -1; + } if (sscanf(a_addr_str, NODE_ADDR_FP_STR, NODE_ADDR_FPS_ARGS(a_addr)) == 4) return 0; if (sscanf(a_addr_str, "0x%016" DAP_UINT64_FORMAT_x, &a_addr->uint64) == 1) diff --git a/modules/consensus/block-pos/dap_chain_cs_block_pos.c b/modules/consensus/block-pos/dap_chain_cs_block_pos.c index 6efe30b407..da4c1390e4 100644 --- a/modules/consensus/block-pos/dap_chain_cs_block_pos.c +++ b/modules/consensus/block-pos/dap_chain_cs_block_pos.c @@ -95,11 +95,11 @@ static int s_callback_new(dap_chain_t *a_chain, dap_config_t *a_chain_cfg) l_blocks->callback_block_verify = s_callback_block_verify; l_blocks->callback_block_sign = s_callback_block_sign; l_pos->_pvt = DAP_NEW_Z(dap_chain_cs_block_pos_pvt_t); - dap_chain_cs_block_pos_pvt_t *l_pos_pvt = PVT(l_pos); if (!l_pos->_pvt) { log_it(L_ERROR, "Memory allocation error in s_callback_new"); goto lb_err; } + dap_chain_cs_block_pos_pvt_t *l_pos_pvt = PVT(l_pos); l_tokens_hold = dap_config_get_array_str(a_chain_cfg, "block-pos", "stake_tokens", &l_tokens_hold_size); l_tokens_hold_value_str = dap_config_get_array_str(a_chain_cfg, "block-pos", "stake_tokens_value", &l_tokens_hold_value_size); diff --git a/modules/consensus/dag-poa/dap_chain_cs_dag_poa.c b/modules/consensus/dag-poa/dap_chain_cs_dag_poa.c index b10b4866f6..69700039a4 100644 --- a/modules/consensus/dag-poa/dap_chain_cs_dag_poa.c +++ b/modules/consensus/dag-poa/dap_chain_cs_dag_poa.c @@ -303,7 +303,6 @@ static int s_cli_dag_poa(int argc, char ** argv, char **a_str_reply) dap_cli_server_cmd_set_reply_text(a_str_reply, "Can't sign event %s in round.new\n", l_event_hash_str); - ret=-1; } DAP_DELETE(l_event); DAP_DELETE(l_round_item); diff --git a/modules/consensus/esbocs/dap_chain_cs_esbocs.c b/modules/consensus/esbocs/dap_chain_cs_esbocs.c index ea2f6fd86e..3a14c8c0aa 100644 --- a/modules/consensus/esbocs/dap_chain_cs_esbocs.c +++ b/modules/consensus/esbocs/dap_chain_cs_esbocs.c @@ -182,12 +182,12 @@ static int s_callback_new(dap_chain_t *a_chain, dap_config_t *a_chain_cfg) a_chain->callback_get_signing_certificate = s_callback_get_sign_key; l_esbocs->_pvt = DAP_NEW_Z(dap_chain_esbocs_pvt_t); - dap_chain_esbocs_pvt_t *l_esbocs_pvt = PVT(l_esbocs); if (!l_esbocs->_pvt) { log_it(L_ERROR, "Memory allocation error in s_callback_new"); l_ret = - 5; goto lb_err; } + dap_chain_esbocs_pvt_t *l_esbocs_pvt = PVT(l_esbocs); l_esbocs_pvt->debug = dap_config_get_item_bool_default(a_chain_cfg, "esbocs", "consensus_debug", false); l_esbocs_pvt->poa_mode = dap_config_get_item_bool_default(a_chain_cfg, "esbocs", "poa_mode", false); l_esbocs_pvt->round_start_sync_timeout = dap_config_get_item_uint16_default(a_chain_cfg, "esbocs", "round_start_sync_timeout", 15); @@ -354,6 +354,10 @@ static void s_session_load_penaltys(dap_chain_esbocs_session_t *a_session) dap_global_db_del(l_penalty_group, (l_keys + i)->key, NULL, NULL); else { dap_chain_esbocs_penalty_item_t *l_item = DAP_NEW_Z(dap_chain_esbocs_penalty_item_t); + if (!l_item) { + log_it(L_ERROR, "Memory allocation error in s_session_load_penaltys"); + return; + } l_item->signing_addr = *l_addr; l_item->miss_count = DAP_CHAIN_ESBOCS_PENALTY_KICK; HASH_ADD(hh, a_session->penalty, signing_addr, sizeof(dap_chain_addr_t), l_item); @@ -444,6 +448,10 @@ static int s_callback_created(dap_chain_t *a_chain, dap_config_t *a_chain_net_cf } dap_chain_esbocs_session_t *l_session = DAP_NEW_Z(dap_chain_esbocs_session_t); + if(!l_session) { + log_it(L_ERROR, "Memory allocation error in s_callback_created"); + return -8; + } l_session->chain = a_chain; l_session->esbocs = l_esbocs; l_esbocs->session = l_session; @@ -723,6 +731,10 @@ static void s_session_update_penalty(dap_chain_esbocs_session_t *a_session) HASH_FIND(hh, a_session->penalty, l_signing_addr, sizeof(*l_signing_addr), l_item); if (!l_item) { l_item = DAP_NEW_Z(dap_chain_esbocs_penalty_item_t); + if (!l_item) { + log_it(L_ERROR, "Memory allocation error in s_session_update_penalty"); + return; + } l_item->signing_addr = *l_signing_addr; HASH_ADD(hh, a_session->penalty, signing_addr, sizeof(*l_signing_addr), l_item); } @@ -1572,6 +1584,7 @@ static void s_session_round_finish(dap_chain_esbocs_session_t *a_session, dap_ch dap_chain_addr_t * addr = DAP_NEW_Z(dap_chain_addr_t); if (!addr) { log_it(L_ERROR, "Memory allocation error in s_session_round_finish"); + DAP_DEL_Z(tmp); return; } *addr = *PVT(a_session->esbocs)->fee_addr; @@ -1588,6 +1601,10 @@ static void s_session_round_finish(dap_chain_esbocs_session_t *a_session, dap_ch void s_session_sync_queue_add(dap_chain_esbocs_session_t *a_session, dap_chain_esbocs_message_t *a_message, size_t a_message_size) { + if (!a_message) { + log_it(L_ERROR, "Invalid arguments in s_session_sync_queue_add"); + return; + } dap_chain_esbocs_sync_item_t *l_sync_item; HASH_FIND(hh, a_session->sync_items, &a_message->hdr.candidate_hash, sizeof(dap_hash_fast_t), l_sync_item); if (!l_sync_item) { @@ -1624,6 +1641,10 @@ void s_session_validator_mark_online(dap_chain_esbocs_session_t *a_session, dap_ if (!l_item) { log_it(L_ERROR, "Got sync message from validator not in active list nor in penalty list"); l_item = DAP_NEW_Z(dap_chain_esbocs_penalty_item_t); + if (!l_item) { + log_it(L_ERROR, "Memory allocation error in s_session_validator_mark_online"); + return; + } l_item->signing_addr = *a_signing_addr; l_item->miss_count = DAP_CHAIN_ESBOCS_PENALTY_KICK; HASH_ADD(hh, a_session->penalty, signing_addr, sizeof(*a_signing_addr), l_item); @@ -1650,6 +1671,10 @@ void s_session_validator_mark_online(dap_chain_esbocs_session_t *a_session, dap_ static void s_session_directive_process(dap_chain_esbocs_session_t *a_session, dap_chain_esbocs_directive_t *a_directive, dap_chain_hash_fast_t *a_directive_hash) { + if (!a_directive) { + log_it(L_ERROR, "Invalid arguments in s_session_directive_process"); + return; + } if (a_directive->size != s_directive_calc_size(a_directive->type)) { log_it(L_ERROR, "Invalid directive size %u (expected %u)", a_directive->size, s_directive_calc_size(a_directive->type)); @@ -2007,6 +2032,10 @@ static void s_session_packet_in(void *a_arg, dap_chain_node_addr_t *a_sender_nod l_session->cur_round.sync_attempt); if (l_session->db_serial) { dap_chain_esbocs_validator_t *l_validator = DAP_NEW_Z(dap_chain_esbocs_validator_t); + if (!l_validator) { + log_it(L_ERROR, "Memory allocation error in s_session_packet_in"); + goto session_unlock; + } l_validator->node_addr = *dap_chain_net_srv_stake_key_get_node_addr(&l_signing_addr); l_validator->signing_addr = l_signing_addr; dap_list_t *l_validator_list = dap_list_append(NULL, l_validator); diff --git a/modules/net/dap_chain_net.c b/modules/net/dap_chain_net.c index abf0964fa4..769dbc0a79 100644 --- a/modules/net/dap_chain_net.c +++ b/modules/net/dap_chain_net.c @@ -562,7 +562,7 @@ static bool s_net_send_atoms(dap_proc_thread_t *a_thread, void *a_arg) */ static void s_chain_callback_notify(void *a_arg, dap_chain_t *a_chain, dap_chain_cell_id_t a_id, void* a_atom, size_t a_atom_size) { - if (!a_arg || !a_chain) { + if (!a_arg || !a_chain || !a_atom) { log_it(L_ERROR, "Argument is NULL for s_chain_callback_notify"); return; } @@ -1209,6 +1209,10 @@ static bool s_new_balancer_link_request(dap_chain_net_t *a_net, int a_link_repla static void s_prepare_links_from_balancer(dap_chain_net_t *a_net) { + if (!a_net) { + log_it(L_ERROR, "Invalid arguments in s_prepare_links_from_balancer"); + return; + } // Get list of the unique links for l_net size_t l_max_links_count = PVT(a_net)->max_links_count * 2; // Not all will be success for (size_t l_cur_links_count = 0, n = 0; l_cur_links_count < l_max_links_count; n++) { @@ -1220,6 +1224,7 @@ static void s_prepare_links_from_balancer(dap_chain_net_t *a_net) // Start connect to link hubs s_new_balancer_link_request(a_net, 0); l_cur_links_count++; + DAP_DEL_Z(l_link_node_info); } } @@ -2101,12 +2106,13 @@ static int s_cli_net(int argc, char **argv, char **a_str_reply) if( l_hash_hex_str ){ l_ret = dap_global_db_set_sync(l_gdb_group_str, l_hash_hex_str, &c, sizeof(c), false ); DAP_DELETE(l_gdb_group_str); - DAP_DELETE(l_hash_hex_str); if (l_ret) { dap_cli_server_cmd_set_reply_text(a_str_reply, "Can't save public key hash %s in database", l_hash_hex_str); + DAP_DELETE(l_hash_hex_str); return -10; } + DAP_DELETE(l_hash_hex_str); } else{ dap_cli_server_cmd_set_reply_text(a_str_reply, "Can't save NULL public key hash in database"); return -10; @@ -2576,6 +2582,11 @@ int s_net_init(const char * a_net_name, uint16_t a_acl_idx) l_node_addr = dap_chain_node_alias_find(l_net, l_node_alias_str); else { l_node_addr = DAP_NEW_Z(dap_chain_node_addr_t); + if (!l_node_addr) { + log_it(L_ERROR, "Memory allocation error in s_net_init"); + dap_config_close(l_cfg); + return -1; + } if (dap_chain_node_addr_from_str(l_node_addr, l_node_addr_str) == 0) log_it(L_NOTICE, "Parse node addr "NODE_ADDR_FP_STR" successfully", NODE_ADDR_FP_ARGS(l_node_addr)); else @@ -2592,6 +2603,12 @@ int s_net_init(const char * a_net_name, uint16_t a_acl_idx) l_net_pvt->node_info = dap_chain_node_info_read (l_net, l_node_addr); if ( !l_net_pvt->node_info ) { // If not present - create it l_net_pvt->node_info = DAP_NEW_Z(dap_chain_node_info_t); + if (!l_net_pvt->node_info) { + log_it(L_ERROR, "Memory allocation error in s_net_init"); + DAP_DEL_Z(l_net_pvt); + dap_config_close(l_cfg); + return -1; + } l_net_pvt->node_info->hdr.address = *l_node_addr; if (dap_config_get_item_bool_default(g_config,"server","enabled",false) ){ const char * l_ext_addr_v4 = dap_config_get_item_str_default(g_config,"server","ext_address",NULL); @@ -2633,6 +2650,8 @@ int s_net_init(const char * a_net_name, uint16_t a_acl_idx) if (!l_chains_dir) { log_it(L_ERROR, "Can't find any chains for network %s", l_net->pub.name); l_net_pvt->load_mode = false; + closedir(l_chains_dir); + dap_config_close(l_cfg); return -2; } // for sequential loading chains @@ -2643,30 +2662,38 @@ int s_net_init(const char * a_net_name, uint16_t a_acl_idx) if (l_dir_entry->d_name[0]=='\0') continue; char * l_entry_name = strdup(l_dir_entry->d_name); + if (!l_entry_name) { + log_it(L_ERROR, "Memory allocation error in s_net_init"); + dap_config_close(l_cfg); + closedir(l_chains_dir); + return -1; + } if (strlen (l_entry_name) > 4 ){ // It has non zero name excluding file extension if ( strncmp (l_entry_name+ strlen(l_entry_name)-4,".cfg",4) == 0 ) { // its .cfg file l_entry_name [strlen(l_entry_name)-4] = 0; log_it(L_DEBUG,"Open chain config \"%s\"...",l_entry_name); l_chains_path = dap_strdup_printf("network/%s/%s",l_net->pub.name,l_entry_name); - dap_config_t * l_cfg = dap_config_open(l_chains_path); - if(l_cfg) { + dap_config_t * l_cfg_new = dap_config_open(l_chains_path); + if(l_cfg_new) { list_priority *l_chain_prior = DAP_NEW_Z(list_priority); if (!l_chain_prior) { log_it(L_ERROR, "Memory allocation error in s_net_init"); DAP_DELETE (l_entry_name); closedir(l_chains_dir); + dap_config_close(l_cfg_new); dap_config_close(l_cfg); + closedir(l_chains_dir); return -1; } l_chain_prior->prior = dap_config_get_item_uint16_default(l_cfg, "chain", "load_priority", 100); l_chain_prior->chains_path = l_chains_path; // add chain to load list; l_prior_list = dap_list_append(l_prior_list, l_chain_prior); - dap_config_close(l_cfg); + dap_config_close(l_cfg_new); } } } - DAP_DELETE (l_entry_name); + DAP_DEL_Z (l_entry_name); } closedir(l_chains_dir); @@ -2854,7 +2881,8 @@ int s_net_load(dap_chain_net_t *a_net) l_target_state = NET_STATE_OFFLINE; } l_net_pvt->load_mode = false; - dap_chain_ledger_load_end(l_net->pub.ledger); + if (l_net->pub.ledger) + dap_chain_ledger_load_end(l_net->pub.ledger); l_net_pvt->balancer_http = !dap_config_get_item_bool_default(l_cfg, "general", "use_dns_links", false); @@ -3427,6 +3455,8 @@ static uint8_t *s_net_set_acl(dap_chain_hash_fast_t *a_pkey_hash) DAP_DELETE(l_net_list); return l_ret; } + if (l_net_list) + DAP_DELETE(l_net_list); return NULL; } diff --git a/modules/net/dap_chain_net_decree.c b/modules/net/dap_chain_net_decree.c index 0774f586de..ff6656eb21 100644 --- a/modules/net/dap_chain_net_decree.c +++ b/modules/net/dap_chain_net_decree.c @@ -303,7 +303,7 @@ int dap_chain_net_decree_apply(dap_hash_fast_t *a_decree_hash, dap_chain_datum_d int dap_chain_net_decree_load(dap_chain_datum_decree_t * a_decree, dap_chain_t *a_chain, dap_chain_hash_fast_t *a_decree_hash) { int ret_val = 0; - if (!a_chain || !a_chain) + if (!a_chain || !a_decree) { log_it(L_ERROR, "Bad arguments"); return -100; diff --git a/modules/net/dap_chain_net_tx.c b/modules/net/dap_chain_net_tx.c index 40973efac8..4caeb58dd4 100644 --- a/modules/net/dap_chain_net_tx.c +++ b/modules/net/dap_chain_net_tx.c @@ -120,6 +120,7 @@ dap_chain_datum_tx_spends_items_t * dap_chain_net_get_tx_cond_all_with_spends_by if (!l_item_in) { log_it(L_ERROR, "Memory allocation error in dap_chain_net_get_tx_cond_all_with_spends_by_srv_uid"); DAP_DEL_Z(l_datums); + DAP_DEL_Z(l_ret); return NULL; } size_t l_tx_size = dap_chain_datum_tx_get_size(l_tx); @@ -142,6 +143,7 @@ dap_chain_datum_tx_spends_items_t * dap_chain_net_get_tx_cond_all_with_spends_by if (!l_item) { log_it(L_ERROR, "Memory allocation error in dap_chain_net_get_tx_cond_all_with_spends_by_srv_uid"); DAP_DEL_Z(l_datums); + DAP_DEL_Z(l_ret); return NULL; } size_t l_tx_size = dap_chain_datum_tx_get_size(l_tx); @@ -285,7 +287,7 @@ struct get_tx_cond_all_from_tx static void s_get_tx_cond_chain_callback(dap_chain_net_t* a_net, dap_chain_datum_tx_t *a_tx, void *a_arg) { struct get_tx_cond_all_from_tx * l_args = (struct get_tx_cond_all_from_tx* ) a_arg; - dap_hash_fast_t * l_tx_hash = dap_chain_node_datum_tx_calc_hash(a_tx); + if( l_args->ret ){ int l_item_idx = 0; byte_t *l_tx_item; @@ -300,24 +302,31 @@ static void s_get_tx_cond_chain_callback(dap_chain_net_t* a_net, dap_chain_datum } l_item_idx++; } - }else if(dap_hash_fast_compare(l_tx_hash,l_args->tx_begin_hash)){ - // Found condition - int l_item_idx = 0; - byte_t *l_tx_item; - - // Get items from transaction - while ((l_tx_item = dap_chain_datum_tx_item_get(a_tx, &l_item_idx, TX_ITEM_TYPE_OUT_COND , NULL)) != NULL){ - dap_chain_tx_out_cond_t * l_out_cond = (dap_chain_tx_out_cond_t *) l_tx_item; - if ( l_out_cond->header.srv_uid.uint64 == l_args->srv_uid.uint64 ){ // We found output with target service uuid - l_args->tx_last = a_tx; // Record current transaction as the last in tx chain - memcpy(&l_args->tx_last_hash, l_tx_hash, sizeof(*l_tx_hash)); // Record current hash - l_args->tx_last_cond_idx = l_item_idx; - l_args->ret = dap_list_append(NULL, a_tx); - break; + }else if(a_tx){ + dap_hash_fast_t * l_tx_hash = dap_chain_node_datum_tx_calc_hash(a_tx); + if (!l_tx_hash) { + log_it(L_ERROR, "Memory allocation error in s_get_tx_cond_chain_callback"); + return; + } + if (dap_hash_fast_compare(l_tx_hash,l_args->tx_begin_hash)) { + // Found condition + int l_item_idx = 0; + byte_t *l_tx_item; + + // Get items from transaction + while ((l_tx_item = dap_chain_datum_tx_item_get(a_tx, &l_item_idx, TX_ITEM_TYPE_OUT_COND , NULL)) != NULL){ + dap_chain_tx_out_cond_t * l_out_cond = (dap_chain_tx_out_cond_t *) l_tx_item; + if ( l_out_cond->header.srv_uid.uint64 == l_args->srv_uid.uint64 ){ // We found output with target service uuid + l_args->tx_last = a_tx; // Record current transaction as the last in tx chain + memcpy(&l_args->tx_last_hash, l_tx_hash, sizeof(*l_tx_hash)); // Record current hash + l_args->tx_last_cond_idx = l_item_idx; + l_args->ret = dap_list_append(NULL, a_tx); + break; + } } } + DAP_DELETE(l_tx_hash); } - DAP_DELETE(l_tx_hash); } /** diff --git a/modules/net/dap_chain_node_cli_cmd.c b/modules/net/dap_chain_node_cli_cmd.c index d80360984c..b949db958b 100644 --- a/modules/net/dap_chain_node_cli_cmd.c +++ b/modules/net/dap_chain_node_cli_cmd.c @@ -1403,10 +1403,9 @@ int com_node(int a_argc, char ** a_argv, char **a_str_reply) switch (l_res) { case 0: if(l_node_client->cur_node_addr.uint64 != 0) { - - l_sync_request.node_addr.uint64 = l_node_client->cur_node_addr.uint64; log_it(L_INFO, "Node address leased"); l_sync_request.node_addr.uint64 = l_node_client->cur_node_addr.uint64; + // save cur address // already saved // dap_db_set_cur_node_addr_exp(l_sync_request.node_addr.uint64, l_net->pub.name); @@ -2635,7 +2634,7 @@ dap_list_t *s_tickers_list_created(dap_chain_datum_tx_t *a_tx, dap_chain_net_t * int l_item_in_size = 0; void *l_item_in = dap_chain_datum_tx_item_get(a_tx, NULL, TX_ITEM_TYPE_IN_ALL, &l_item_in_size); dap_hash_fast_t l_parent_hash = {0}; - int l_parrent_tx_out_idx; + int l_parrent_tx_out_idx = 0; for (int l_item_in_size_current = 0; l_item_in_size_current < l_item_in_size && !l_token_ticker;) { size_t l_tmp_size = dap_chain_datum_item_tx_get_size(l_item_in); if (l_tmp_size == 0) @@ -3345,7 +3344,6 @@ static int s_parse_common_token_decl_arg(int a_argc, char ** a_argv, char ** a_s dap_cli_server_cmd_find_option_val(a_argv, 0, a_argc, "-chain", &a_params->chain_str); //token_ticker dap_cli_server_cmd_find_option_val(a_argv, 0, a_argc, "-token", &a_params->ticker); - // Token type dap_cli_server_cmd_find_option_val(a_argv, 0, a_argc, "-type", &a_params->type_str); @@ -3362,9 +3360,6 @@ static int s_parse_common_token_decl_arg(int a_argc, char ** a_argv, char ** a_s } else if (strcmp(a_params->type_str, "public_simple") == 0 && !a_update_token) { a_params->type = DAP_CHAIN_DATUM_TOKEN_TYPE_DECL; a_params->subtype = DAP_CHAIN_DATUM_TOKEN_SUBTYPE_PUBLIC; // 256 - } else if (strcmp(a_params->type_str, "CF20") == 0) { - a_params->type = DAP_CHAIN_DATUM_TOKEN_TYPE_DECL; - a_params->subtype = DAP_CHAIN_DATUM_TOKEN_SUBTYPE_NATIVE; // 256 } else if (!a_update_token) { dap_cli_server_cmd_set_reply_text(a_str_reply, "Unknown token type %s was specified. Supported types:\n" @@ -3525,19 +3520,15 @@ static int s_parse_additional_token_decl_arg(int a_argc, char ** a_argv, char ** char *l_remove_signs_dup = strdup(l_remove_signs); char *l_remove_signs_str = strtok_r(l_remove_signs_dup, ",", &l_remove_signs_ptrs); for (; l_remove_signs_str; l_remove_signs_str = strtok_r(NULL, ",", &l_remove_signs_ptrs)) { - dap_hash_fast_t *l_hf = DAP_NEW(dap_hash_fast_t); - char *l_tmp = strdup(l_remove_signs_str); - if (dap_chain_hash_fast_from_str(l_tmp, l_hf) == 0) { - dap_tsd_t *l_hf_tsd = dap_tsd_create(DAP_CHAIN_DATUM_TOKEN_TSD_TYPE_TOTAL_PKEYS_REMOVE, l_hf, sizeof(dap_hash_fast_t)); + dap_hash_fast_t l_hf; + if (dap_chain_hash_fast_from_str(l_remove_signs_str, &l_hf) == 0) { + dap_tsd_t *l_hf_tsd = dap_tsd_create(DAP_CHAIN_DATUM_TOKEN_TSD_TYPE_TOTAL_PKEYS_REMOVE, &l_hf, sizeof(dap_hash_fast_t)); size_t l_hf_tsd_size = dap_tsd_size(l_hf_tsd); l_tsd_list = dap_list_append(l_tsd_list, l_hf_tsd); l_added_tsd_size += l_hf_tsd_size; } - DAP_DELETE(l_hf); - DAP_DELETE(l_tmp); } DAP_DELETE(l_remove_signs_dup); - DAP_DELETE(l_remove_signs_str); l_tsd_total_size += l_added_tsd_size; } //Added new certs @@ -3736,8 +3727,10 @@ int com_token_decl(int a_argc, char ** a_argv, char ** a_str_reply) l_params->subtype = DAP_CHAIN_DATUM_TOKEN_SUBTYPE_SIMPLE; int l_parse_params = s_token_decl_check_params(a_argc,a_argv,a_str_reply,l_params, false); - if (l_parse_params) + if (l_parse_params) { + DAP_DEL_Z(l_params); return l_parse_params; + } dap_chain_datum_token_t * l_datum_token = NULL; size_t l_datum_data_offset = 0; @@ -3747,6 +3740,7 @@ int com_token_decl(int a_argc, char ** a_argv, char ** a_str_reply) if(!l_certs_count){ dap_cli_server_cmd_set_reply_text(a_str_reply, "token_decl command requres at least one valid certificate to sign token"); + DAP_DEL_Z(l_params); return -10; } @@ -3774,6 +3768,7 @@ int com_token_decl(int a_argc, char ** a_argv, char ** a_str_reply) uint16_t l_flag = dap_chain_datum_token_flag_from_str(*l_str_flags); if (l_flag == DAP_CHAIN_DATUM_TOKEN_FLAG_UNDEFINED ){ dap_cli_server_cmd_set_reply_text(a_str_reply, "Flag can't be \"%s\"",*l_str_flags); + DAP_DEL_Z(l_params); return -20; } l_flags |= l_flag; // if we have multiple flags @@ -3784,6 +3779,7 @@ int com_token_decl(int a_argc, char ** a_argv, char ** a_str_reply) dap_chain_datum_token_t *l_delegated_token_from; if (NULL == (l_delegated_token_from = dap_chain_ledger_token_ticker_check(l_net->pub.ledger, l_params->ext.delegated_token_from))) { dap_cli_server_cmd_set_reply_text(a_str_reply,"To create a delegated token %s, can't find token by ticket %s", l_ticker, l_params->ext.delegated_token_from); + DAP_DEL_Z(l_params); return -91; } dap_chain_datum_token_tsd_delegate_from_stake_lock_t l_tsd_section; @@ -3829,7 +3825,13 @@ int com_token_decl(int a_argc, char ** a_argv, char ** a_str_reply) // Create new datum token - l_datum_token = DAP_NEW_Z_SIZE(dap_chain_datum_token_t, sizeof(dap_chain_datum_token_t) + l_params->ext.tsd_total_size) ; + l_datum_token = DAP_NEW_Z_SIZE(dap_chain_datum_token_t, sizeof(dap_chain_datum_token_t) + l_params->ext.tsd_total_size); + if (!l_datum_token) { + log_it(L_ERROR, "Memory allocation error in com_token_decl"); + dap_cli_server_cmd_set_reply_text(a_str_reply, "Out of memory in com_token_decl"); + DAP_DEL_Z(l_params); + return -1; + } l_datum_token->version = 2; l_datum_token->type = l_params->type; l_datum_token->subtype = l_params->subtype; @@ -3889,6 +3891,12 @@ int com_token_decl(int a_argc, char ** a_argv, char ** a_str_reply) }break;//end case DAP_CHAIN_DATUM_TOKEN_SUBTYPE_SIMPLE: { // 256 l_datum_token = DAP_NEW_Z_SIZE(dap_chain_datum_token_t, sizeof(dap_chain_datum_token_t)); + if (!l_datum_token) { + log_it(L_ERROR, "Memory allocation error in com_token_decl"); + dap_cli_server_cmd_set_reply_text(a_str_reply, "Out of memory in com_token_decl"); + DAP_DEL_Z(l_params); + return -1; + } l_datum_token->version = 2; l_datum_token->type = DAP_CHAIN_DATUM_TOKEN_TYPE_DECL; // 256 l_datum_token->subtype = DAP_CHAIN_DATUM_TOKEN_SUBTYPE_SIMPLE; // 256 @@ -3900,6 +3908,7 @@ int com_token_decl(int a_argc, char ** a_argv, char ** a_str_reply) default: dap_cli_server_cmd_set_reply_text(a_str_reply, "Unknown token type"); + DAP_DEL_Z(l_params); return -8; } // If we have more certs than we need signs - use only first part of the list @@ -3915,6 +3924,7 @@ int com_token_decl(int a_argc, char ** a_argv, char ** a_str_reply) if (!l_datum_token || l_datum_token->signs_total == 0){ dap_cli_server_cmd_set_reply_text(a_str_reply, "Token declaration failed. Successful count of certificate signing is 0"); + DAP_DEL_Z(l_params); return -9; } @@ -3939,6 +3949,7 @@ int com_token_decl(int a_argc, char ** a_argv, char ** a_str_reply) dap_cli_server_cmd_set_reply_text(a_str_reply, "No suitable chain for placing token datum found"); DAP_DEL_Z(l_key_str); DAP_DELETE(l_datum); + DAP_DEL_Z(l_params); return -10; } bool l_placed = dap_global_db_set_sync(l_gdb_group_mempool, l_key_str, l_datum, l_datum_size, false) == 0; @@ -4048,7 +4059,11 @@ int com_token_update(int a_argc, char ** a_argv, char ** a_str_reply) case DAP_CHAIN_DATUM_TOKEN_SUBTYPE_NATIVE: { // 256 // Create new datum token - l_datum_token = DAP_NEW_Z_SIZE(dap_chain_datum_token_t, sizeof(dap_chain_datum_token_t) + l_params->ext.tsd_total_size) ; + l_datum_token = DAP_NEW_Z_SIZE(dap_chain_datum_token_t, sizeof(dap_chain_datum_token_t) + l_params->ext.tsd_total_size); + if (!l_datum_token) { + log_it(L_ERROR, "Memory allocation error in com_token_update"); + return -1; + } l_datum_token->version = 2; l_datum_token->type = l_params->type; l_datum_token->subtype = l_params->subtype; @@ -4082,6 +4097,10 @@ int com_token_update(int a_argc, char ** a_argv, char ** a_str_reply) }break;//end case DAP_CHAIN_DATUM_TOKEN_SUBTYPE_SIMPLE: { // 256 l_datum_token = DAP_NEW_Z_SIZE(dap_chain_datum_token_t, sizeof(dap_chain_datum_token_t)); + if (!l_datum_token) { + log_it(L_ERROR, "Memory allocation error in com_token_update"); + return -1; + } l_datum_token->version = 2; l_datum_token->subtype = DAP_CHAIN_DATUM_TOKEN_TYPE_UPDATE; l_datum_token->subtype = DAP_CHAIN_DATUM_TOKEN_SUBTYPE_SIMPLE; // 256 @@ -6483,6 +6502,8 @@ static int s_get_key_from_file(const char *a_file, const char *a_mime, const cha int l_ret = s_sign_file(a_file, l_flags_mime, a_cert_name, a_sign, &l_hash); + if (l_items_mime) + DAP_DELETE(l_items_mime); return l_ret; } @@ -6658,6 +6679,10 @@ static byte_t *s_concat_meta (dap_list_t *a_meta, size_t *a_fullsize) int l_part = 256; int l_power = 1; byte_t *l_buf = DAP_CALLOC(l_part * l_power++, 1); + if (!l_buf) { + log_it(L_ERROR, "Memory allocation error in s_concat_meta"); + return NULL; + } size_t l_counter = 0; size_t l_part_power = l_part; int l_index = 0; diff --git a/modules/net/dap_chain_node_cli_cmd_tx.c b/modules/net/dap_chain_node_cli_cmd_tx.c index e2c9cf7370..a18ad3a788 100644 --- a/modules/net/dap_chain_node_cli_cmd_tx.c +++ b/modules/net/dap_chain_node_cli_cmd_tx.c @@ -212,6 +212,10 @@ char* dap_db_history_addr(dap_chain_addr_t *a_addr, dap_chain_t *a_chain, const }; dap_string_t *l_str_out = dap_string_new(NULL); + if (!l_str_out) { + log_it(L_ERROR, "Memory allocation error in dap_db_history_token_list"); + return NULL; + } dap_chain_tx_hash_processed_ht_t *l_tx_data_ht = NULL; dap_chain_net_t *l_net = dap_chain_net_by_id(a_chain->net_id); if (!l_net) { @@ -417,6 +421,10 @@ static char* dap_db_history_token_list(dap_chain_t * a_chain, const char *a_toke return NULL; } dap_string_t *l_str_out = dap_string_new(NULL); + if (!l_str_out) { + log_it(L_ERROR, "Memory allocation error in dap_db_history_token_list"); + return NULL; + } *a_token_num = 0; size_t l_atom_size = 0; dap_chain_cell_t *l_cell = a_chain->cells; @@ -472,6 +480,10 @@ static char* dap_db_history_filter(dap_chain_t * a_chain, dap_ledger_t *a_ledger return NULL; } dap_string_t *l_str_out = dap_string_new(NULL); + if (!l_str_out) { + log_it(L_ERROR, "Memory allocation error in dap_db_history_filter"); + return NULL; + } // list all transactions dap_tx_data_t *l_tx_data_hash = NULL; dap_chain_cell_t *l_cell = a_chain->cells; diff --git a/modules/net/dap_chain_node_dns_server.c b/modules/net/dap_chain_node_dns_server.c index da023392ec..5711fda111 100644 --- a/modules/net/dap_chain_node_dns_server.c +++ b/modules/net/dap_chain_node_dns_server.c @@ -123,11 +123,14 @@ void dap_dns_client_read(dap_events_socket_t *a_es, void *a_arg) { dap_dns_buf_t *dns_reply = DAP_NEW(dap_dns_buf_t); if (!dns_reply) { log_it(L_ERROR, "Memory allocation error in dap_dns_client_read"); + DAP_DELETE(dns_message); return; } dns_message->data = DAP_NEW_SIZE(char, a_es->buf_in_size + 1); if (!dns_message->data) { log_it(L_ERROR, "Memory allocation error in dap_dns_client_read"); + DAP_DELETE(dns_message); + DAP_DELETE(dns_reply); return; } dns_message->data[a_es->buf_in_size] = 0; diff --git a/modules/net/dap_chain_node_ping.c b/modules/net/dap_chain_node_ping.c index 10d65ae03a..260e9dd188 100644 --- a/modules/net/dap_chain_node_ping.c +++ b/modules/net/dap_chain_node_ping.c @@ -284,6 +284,12 @@ static void* node_ping_background_proc(void *a_arg) s_node_addr_tr = l_node_addr_tmp; l_node_addr_tmp = DAP_NEW(dap_chain_node_addr_t); + if (!l_node_addr_tmp) { + log_it(L_ERROR, "Memory allocation error in node_ping_background_proc"); + dap_list_free_full(l_node_list0, NULL); + DAP_DEL_Z(s_node_addr_ping); + return 0; + } memcpy(l_node_addr_tmp, s_node_addr_ping, sizeof(dap_chain_node_addr_t)); DAP_DELETE(s_node_addr_ping); s_node_addr_ping = l_node_addr_tmp; diff --git a/modules/net/srv/dap_chain_net_srv.c b/modules/net/srv/dap_chain_net_srv.c index dd21d64870..401d562569 100644 --- a/modules/net/srv/dap_chain_net_srv.c +++ b/modules/net/srv/dap_chain_net_srv.c @@ -842,6 +842,12 @@ int dap_chain_net_srv_price_apply_from_my_order(dap_chain_net_srv_t *a_srv, cons if (l_order->node_addr.uint64 == l_node_addr->uint64) { l_err_code = 0; dap_chain_net_srv_price_t *l_price = DAP_NEW_Z(dap_chain_net_srv_price_t); + if (!l_price) { + log_it(L_ERROR, "Memory allocation error in dap_chain_net_srv_price_apply_from_my_order"); + DAP_DEL_Z(l_order); + dap_global_db_objs_delete(l_orders, l_orders_count); + return -1; + } l_price->net = l_net; l_price->net_name = dap_strdup(l_net->pub.name); l_price->value_datoshi = l_order->price; diff --git a/modules/net/srv/dap_chain_net_srv_client.c b/modules/net/srv/dap_chain_net_srv_client.c index 16f080dc10..69ac80d4f1 100644 --- a/modules/net/srv/dap_chain_net_srv_client.c +++ b/modules/net/srv/dap_chain_net_srv_client.c @@ -54,7 +54,7 @@ dap_chain_net_srv_client_t *dap_chain_net_srv_client_create_n_connect(dap_chain_ .delete = s_srv_client_callback_deleted }; dap_chain_node_info_t *l_info = DAP_NEW_Z(dap_chain_node_info_t); - if (!l_ret) { + if (!l_info) { log_it(L_ERROR, "Memory allocation error in dap_chain_net_srv_client_create_n_connect"); DAP_DEL_Z(l_ret); return NULL; diff --git a/modules/net/srv/include/dap_chain_net_srv.h b/modules/net/srv/include/dap_chain_net_srv.h index db494599b1..b4493d9f5b 100755 --- a/modules/net/srv/include/dap_chain_net_srv.h +++ b/modules/net/srv/include/dap_chain_net_srv.h @@ -116,6 +116,7 @@ typedef struct dap_chain_net_srv_price #define DAP_STREAM_CH_CHAIN_NET_SRV_PKT_TYPE_RESPONSE_ERROR_CODE_RECEIPT_BANNED_PKEY_HASH 0x00000503 #define DAP_STREAM_CH_CHAIN_NET_SRV_PKT_TYPE_RESPONSE_ERROR_CODE_PRICE_NOT_FOUND 0x00000600 #define DAP_STREAM_CH_CHAIN_NET_SRV_PKT_TYPE_RESPONSE_ERROR_CODE_WRONG_HASH 0x00000BAD +#define DAP_STREAM_CH_CHAIN_NET_SRV_PKT_TYPE_RESPONSE_ERROR_CODE_ALLOC_MEMORY_ERROR 0x00BADA55 #define DAP_STREAM_CH_CHAIN_NET_SRV_PKT_TYPE_RESPONSE_ERROR_CODE_UNKNOWN 0xffffffff // TYPE_REQUEST diff --git a/modules/service/datum/dap_chain_net_srv_datum.c b/modules/service/datum/dap_chain_net_srv_datum.c index ed5776f87e..58c04bd76a 100644 --- a/modules/service/datum/dap_chain_net_srv_datum.c +++ b/modules/service/datum/dap_chain_net_srv_datum.c @@ -80,13 +80,11 @@ uint8_t * dap_chain_net_srv_file_datum_data_read(char * a_path, size_t *a_data_s if ( fread(l_datum_data, 1, l_datum_data_size, l_file ) != l_datum_data_size ){ log_it(L_ERROR, "Can't read %"DAP_UINT64_FORMAT_U" bytes from the disk!", l_datum_data_size); DAP_DELETE(l_datum_data); - if( l_file ) - fclose(l_file); + fclose(l_file); return NULL; } - } - if( l_file ) fclose(l_file); + } *a_data_size = l_datum_data_size; return l_datum_data; } @@ -146,6 +144,7 @@ static int s_srv_datum_cli(int argc, char ** argv, char **a_str_reply) { size_t l_retbytes; if ( (l_retbytes = fwrite(l_datum->data, 1, l_datum->header.data_size, l_file)) != l_datum->header.data_size ){ log_it(L_ERROR, "Can't write %u bytes on disk (processed only %zu)!", l_datum->header.data_size, l_retbytes); + fclose(l_file); return -3; } fclose(l_file); diff --git a/modules/service/stake/dap_chain_net_srv_stake_pos_delegate.c b/modules/service/stake/dap_chain_net_srv_stake_pos_delegate.c index b09e976a83..1635876090 100644 --- a/modules/service/stake/dap_chain_net_srv_stake_pos_delegate.c +++ b/modules/service/stake/dap_chain_net_srv_stake_pos_delegate.c @@ -330,7 +330,15 @@ static bool s_stake_cache_check_tx(dap_hash_fast_t *a_tx_hash) int dap_chain_net_srv_stake_load_cache(dap_chain_net_t *a_net) { + if (!a_net) { + log_it(L_ERROR, "Invalid argument a_net in dap_chain_net_srv_stake_load_cache"); + return -1; + } dap_ledger_t *l_ledger = a_net->pub.ledger; + if(!l_ledger) { + log_it(L_ERROR, "Invalid arguments l_ledger in dap_chain_net_srv_stake_load_cache"); + return -1; + } if (!dap_chain_ledger_cache_enabled(l_ledger)) return 0; char *l_gdb_group = dap_chain_ledger_get_gdb_group(l_ledger, DAP_CHAIN_NET_SRV_STAKE_POS_DELEGATE_GDB_GROUP); @@ -599,6 +607,7 @@ dap_chain_datum_decree_t *dap_chain_net_srv_stake_decree_approve(dap_chain_net_t l_chain = dap_chain_net_get_chain_by_chain_type(a_net, CHAIN_TYPE_ANCHOR); if (!l_chain) { log_it(L_ERROR, "No chain supported anchor datum type"); + DAP_DEL_Z(l_decree); return NULL; } l_decree->header.common_decree_params.chain_id = l_chain->id; @@ -809,6 +818,7 @@ static dap_chain_datum_decree_t *s_stake_decree_invalidate(dap_chain_net_t *a_ne l_chain = dap_chain_net_get_chain_by_chain_type(a_net, CHAIN_TYPE_ANCHOR); if (!l_chain) { log_it(L_ERROR, "No chain supported anchor datum type"); + DAP_DEL_Z(l_decree); dap_list_free_full(l_tsd_list, NULL); return NULL; } @@ -884,6 +894,7 @@ static dap_chain_datum_decree_t *s_stake_decree_set_min_stake(dap_chain_net_t *a l_chain = dap_chain_net_get_chain_by_chain_type(a_net, CHAIN_TYPE_ANCHOR); if (!l_chain) { log_it(L_ERROR, "No chain supported anchor datum type"); + DAP_DEL_Z(l_decree); dap_list_free_full(l_tsd_list, NULL); return NULL; } @@ -1862,7 +1873,7 @@ static int s_cli_srv_stake(int a_argc, char **a_argv, char **a_str_reply) if (l_tx && (l_decree_hash_str = s_stake_tx_put(l_tx, l_net))) { dap_cli_server_cmd_set_reply_text(a_str_reply, "All m-tokens successfully returned to " "owner. Returning tx hash %s.", l_decree_hash_str); - DAP_DELETE(l_decree_hash_str); + DAP_DEL_Z(l_decree_hash_str); DAP_DELETE(l_tx); } else { char *l_final_tx_hash_str = dap_chain_hash_fast_to_str_new(l_final_tx_hash); diff --git a/modules/service/vpn/dap_chain_net_srv_vpn.c b/modules/service/vpn/dap_chain_net_srv_vpn.c index 34c5e8f2b6..ecbfc62c33 100644 --- a/modules/service/vpn/dap_chain_net_srv_vpn.c +++ b/modules/service/vpn/dap_chain_net_srv_vpn.c @@ -284,6 +284,10 @@ static bool s_tun_client_send_data(dap_chain_net_srv_ch_vpn_info_t * l_ch_vpn_in { assert(a_data_size > sizeof (dap_os_iphdr_t)); ch_vpn_pkt_t *l_pkt_out = DAP_NEW_Z_SIZE(ch_vpn_pkt_t, sizeof(l_pkt_out->header) + a_data_size); + if (!l_pkt_out) { + log_it(L_ERROR, "Memory allocation error in s_tun_client_send_data"); + return false; + } l_pkt_out->header.op_code = VPN_PACKET_OP_CODE_VPN_RECV; l_pkt_out->header.sock_id = s_raw_server->tun_fd; l_pkt_out->header.usage_id = l_ch_vpn_info->usage_id; @@ -294,10 +298,12 @@ static bool s_tun_client_send_data(dap_chain_net_srv_ch_vpn_info_t * l_ch_vpn_in dap_events_socket_t* l_es = dap_context_find(l_ch_vpn_info->worker->context, l_ch_vpn_info->esocket_uuid); if (!l_es) { log_it(L_ERROR, "No esocket %p on worker #%u, lost %zd data", l_ch_vpn_info->esocket, l_ch_vpn_info->worker->id, a_data_size); + DAP_DEL_Z(l_pkt_out); return false; } if (l_es != l_ch_vpn_info->esocket) { log_it(L_ERROR, "Wrong esocket %p on worker #%u, lost %zd data", l_ch_vpn_info->esocket, l_ch_vpn_info->worker->id, a_data_size); + DAP_DEL_Z(l_pkt_out); return false; } /* @@ -321,6 +327,7 @@ static bool s_tun_client_send_data(dap_chain_net_srv_ch_vpn_info_t * l_ch_vpn_in tun_socket_msg_t* l_msg = DAP_NEW_Z(tun_socket_msg_t); if (!l_msg) { log_it(L_ERROR, "Memory allocation error in s_tun_client_send_data"); + DAP_DEL_Z(l_pkt_out); return false; } l_msg->type = TUN_SOCKET_MSG_CH_VPN_SEND; @@ -333,6 +340,7 @@ static bool s_tun_client_send_data(dap_chain_net_srv_ch_vpn_info_t * l_ch_vpn_in log_it(L_WARNING, "Error on sending packet to foreign context queue, lost %zd bytes", a_data_size); DAP_DELETE(l_msg->ch_vpn_send.pkt); DAP_DELETE(l_msg); + DAP_DEL_Z(l_pkt_out); return false; } @@ -347,6 +355,7 @@ static bool s_tun_client_send_data(dap_chain_net_srv_ch_vpn_info_t * l_ch_vpn_in log_it(L_INFO, "Sent packet (%zd bytes) to desitnation %s in foreign context", a_data_size, l_str_daddr); } } + DAP_DEL_Z(l_pkt_out); return true; } @@ -772,8 +781,10 @@ static int s_vpn_tun_create(dap_config_t * g_config) #endif s_tun_event_stream_create(l_worker, l_tun_fd); } - if (l_err) + if (l_err) { + pthread_mutex_unlock(&s_tun_sockets_mutex_started); goto lb_err; + } // Waiting for all the tun sockets while (s_tun_sockets_started != s_tun_sockets_count) @@ -1106,6 +1117,10 @@ void s_ch_vpn_new(dap_stream_ch_t* a_ch, void* a_arg) a_ch->stream->esocket->callbacks.worker_assign_callback = s_ch_vpn_esocket_assigned; a_ch->internal = DAP_NEW_Z(dap_chain_net_srv_ch_vpn_t); + if (!a_ch->internal) { + log_it(L_ERROR, "Memory allocation error in s_ch_vpn_new"); + return; + } dap_chain_net_srv_ch_vpn_t * l_srv_vpn = CH_VPN(a_ch); if(a_ch->stream->session->_inheritor == NULL && a_ch->stream->session != NULL) @@ -1170,7 +1185,7 @@ static void s_ch_vpn_delete(dap_stream_ch_t* a_ch, void* arg) if ( l_is_unleased ){ // If unleased log_it(L_DEBUG, "Unlease address %s and store in treshold", inet_ntoa(l_ch_vpn->addr_ipv4)); dap_chain_net_srv_vpn_item_ipv4_t * l_item_unleased = DAP_NEW_Z(dap_chain_net_srv_vpn_item_ipv4_t); - if (!l_is_unleased) { + if (!l_item_unleased) { log_it(L_ERROR, "Memory allocation error in s_ch_vpn_delete"); pthread_rwlock_unlock(&s_clients_rwlock); return; diff --git a/modules/service/vpn/dap_chain_net_vpn_client.c b/modules/service/vpn/dap_chain_net_vpn_client.c index b7b3337c67..8700755ce1 100644 --- a/modules/service/vpn/dap_chain_net_vpn_client.c +++ b/modules/service/vpn/dap_chain_net_vpn_client.c @@ -169,6 +169,7 @@ static int s_callback_client_success(dap_chain_net_srv_t * a_srv, uint32_t a_usa a_srv_client->ch->stream->channel[DAP_CHAIN_NET_SRV_VPN_ID]->internal : NULL; if ( ! l_srv_ch_vpn ){ log_it(L_ERROR, "No VPN service stream channel, its closed?"); + dap_stream_session_unlock(); return -3; } l_srv_ch_vpn->usage_id = l_usage->id; @@ -183,6 +184,7 @@ static int s_callback_client_success(dap_chain_net_srv_t * a_srv, uint32_t a_usa ch_vpn_pkt_t *pkt_out = (ch_vpn_pkt_t*) calloc(1, sizeof(pkt_out->header) + l_ipv4_str_len); if (!pkt_out) { log_it(L_ERROR, "Memory allocation error in s_callback_client_success"); + dap_stream_session_unlock(); return -1; } @@ -200,6 +202,7 @@ static int s_callback_client_success(dap_chain_net_srv_t * a_srv, uint32_t a_usa // usage is present, we've accepted packets dap_stream_ch_set_ready_to_read_unsafe( l_srv_ch_vpn->ch , true ); + dap_stream_session_unlock(); return 0; } diff --git a/modules/service/xchange/dap_chain_net_srv_xchange.c b/modules/service/xchange/dap_chain_net_srv_xchange.c index c7173809eb..dcc6eb5902 100644 --- a/modules/service/xchange/dap_chain_net_srv_xchange.c +++ b/modules/service/xchange/dap_chain_net_srv_xchange.c @@ -627,7 +627,6 @@ static dap_chain_datum_tx_t *s_xchange_tx_create_exchange(dap_chain_net_srv_xcha log_it(L_ERROR, "Can't add selling coins output because price rate is 0"); return NULL; } - DAP_DELETE(l_buyer_addr); // transfer unselling coins (partial exchange) debug_if(s_debug_more, L_NOTICE, "l_datoshi_cond = %s", dap_chain_balance_to_coins(l_tx_out_cond->header.value)); if (compare256(l_tx_out_cond->header.value, l_datoshi_sell) == 1) { @@ -719,9 +718,10 @@ static dap_chain_datum_tx_t *s_xchange_tx_create_exchange(dap_chain_net_srv_xcha if(dap_chain_datum_tx_add_sign_item(&l_tx, l_seller_key) != 1) { dap_chain_datum_tx_delete(l_tx); log_it( L_ERROR, "Can't add sign output"); + DAP_DELETE(l_buyer_addr); return NULL; } - + DAP_DELETE(l_buyer_addr); return l_tx; } @@ -1166,6 +1166,9 @@ static int s_cli_srv_xchange_order(int a_argc, char **a_argv, int a_arg_index, c while(l_tx_list ){ dap_chain_datum_tx_t * l_tx_cur = (dap_chain_datum_tx_t*) l_tx_list->data; s_string_append_tx_cond_info(l_str_reply, l_net, l_tx_cur ); + + //INFINITE LOOP ???? + } dap_list_free(l_tx_list); *a_str_reply = dap_string_free(l_str_reply, false); @@ -1194,6 +1197,9 @@ static int s_cli_srv_xchange_order(int a_argc, char **a_argv, int a_arg_index, c while(l_tx_list ){ dap_chain_datum_tx_t * l_tx_cur = (dap_chain_datum_tx_t*) l_tx_list->data; s_string_append_tx_cond_info(l_str_reply, l_net, l_tx_cur ); + + //INFINITE LOOP ???? + } dap_list_free(l_tx_list); *a_str_reply = dap_string_free(l_str_reply, false); diff --git a/modules/type/blocks/dap_chain_cs_blocks.c b/modules/type/blocks/dap_chain_cs_blocks.c index 36a723f9e3..788087f6c2 100644 --- a/modules/type/blocks/dap_chain_cs_blocks.c +++ b/modules/type/blocks/dap_chain_cs_blocks.c @@ -1443,10 +1443,6 @@ static dap_chain_atom_ptr_t *s_callback_atom_iter_get_lasts( dap_chain_atom_iter (*a_lasts_size_ptr)[0] = l_block_cache_last->block_size; } dap_chain_atom_ptr_t *l_ret = DAP_NEW_Z(dap_chain_atom_ptr_t); - if (!l_ret) { - log_it(L_ERROR, "Memory allocation error in s_callback_atom_iter_get_lasts"); - return NULL; - } if (!l_ret) { log_it(L_ERROR, "Memory allocation error in s_callback_atom_iter_get_lasts"); return NULL; diff --git a/modules/type/dag/dap_chain_cs_dag.c b/modules/type/dag/dap_chain_cs_dag.c index f064e07284..70e7595459 100644 --- a/modules/type/dag/dap_chain_cs_dag.c +++ b/modules/type/dag/dap_chain_cs_dag.c @@ -448,7 +448,6 @@ static int s_dap_chain_add_datum(dap_chain_cs_dag_t *a_dag, dap_chain_cs_dag_eve int l_ret = dap_chain_datum_add(a_dag->chain, l_datum, l_datum_size, &l_datum_hash); if (l_datum->header.type_id == DAP_CHAIN_DATUM_TX) // && l_ret == 0 PVT(a_dag)->tx_count++; - a_event_item->ts_added = a_event_item->ts_added; a_event_item->datum_hash = l_datum_hash; a_event_item->ret_code = l_ret; unsigned l_hash_item_hashv; @@ -1180,9 +1179,20 @@ static dap_chain_atom_ptr_t* s_chain_callback_atom_iter_get_lasts( dap_chain_ato if( a_lasts_size) *a_lasts_size = l_lasts_size; l_ret = DAP_NEW_Z_SIZE(dap_chain_atom_ptr_t, sizeof(dap_chain_atom_ptr_t) * l_lasts_size); + if (!l_ret) { + log_it(L_ERROR, "Memory allocation error in s_chain_callback_atom_iter_get_lasts"); + pthread_mutex_unlock(&PVT(l_dag)->events_mutex); + return NULL; + } dap_chain_cs_dag_event_item_t * l_event_item = NULL, *l_event_item_tmp = NULL; size_t i = 0; *a_lasts_size_array = DAP_NEW_Z_SIZE(size_t, sizeof(size_t) * l_lasts_size); + if (!*a_lasts_size_array) { + log_it(L_ERROR, "Memory allocation error in s_chain_callback_atom_iter_get_lasts"); + pthread_mutex_unlock(&PVT(l_dag)->events_mutex); + DAP_DEL_Z(l_ret); + return NULL; + } HASH_ITER(hh,PVT(l_dag)->events_lasts_unlinked, l_event_item,l_event_item_tmp){ l_ret[i] = l_event_item->event; (*a_lasts_size_array)[i] = l_event_item->event_size; @@ -1220,7 +1230,7 @@ static dap_chain_atom_ptr_t* s_chain_callback_atom_iter_get_links( dap_chain_ato if( a_links_size) *a_links_size = l_event->header.hash_count; *a_links_size_array = DAP_NEW_Z_SIZE(size_t, l_event->header.hash_count*sizeof (size_t)); - if (!a_links_size_array) { + if (!*a_links_size_array) { log_it(L_ERROR, "Memory allocation error in s_chain_callback_atom_iter_get_links"); DAP_DEL_Z(l_ret); return NULL; @@ -2093,6 +2103,7 @@ static dap_list_t *s_callback_get_atoms(dap_chain_t *a_chain, size_t a_count, si if (a_page < 2) l_offset = 0; if (l_offset > l_count){ + pthread_mutex_unlock(&PVT(l_dag)->events_mutex); return NULL; } dap_list_t *l_list = NULL; diff --git a/modules/wallet/dap_chain_wallet.c b/modules/wallet/dap_chain_wallet.c index e00ffd40e8..2c4f8a0221 100644 --- a/modules/wallet/dap_chain_wallet.c +++ b/modules/wallet/dap_chain_wallet.c @@ -410,6 +410,11 @@ dap_chain_wallet_internal_t *l_wallet_internal; l_wallet_internal->certs_count = 1; l_wallet_internal->certs = DAP_NEW_Z_SIZE(dap_cert_t *,l_wallet_internal->certs_count * sizeof(dap_cert_t *)); assert(l_wallet_internal->certs); + if (!l_wallet_internal->certs) { + log_it(L_ERROR, "Memory allocation error in dap_chain_wallet_create_with_seed"); + dap_chain_wallet_close(l_wallet); + return NULL; + } snprintf(l_wallet_internal->file_name, sizeof(l_wallet_internal->file_name) - 1, "%s/%s%s", a_wallets_path, a_wallet_name, s_wallet_ext); @@ -835,17 +840,41 @@ uint32_t l_csum = CRC32C_INIT, l_csum2 = CRC32C_INIT; * allocate memory for array to keep loaded certs */ l_wallet = DAP_NEW_Z(dap_chain_wallet_t); assert(l_wallet); + if (!l_wallet) { + log_it(L_ERROR, "Memory allocation error in dap_chain_wallet_open_file"); + dap_fileclose(l_fh); + return NULL; + } + DAP_CHAIN_WALLET_INTERNAL_LOCAL_NEW(l_wallet); assert(l_wallet_internal); + if (!l_wallet_internal) { + log_it(L_ERROR, "Memory allocation error in dap_chain_wallet_open_file"); + DAP_DEL_Z(l_wallet); + dap_fileclose(l_fh); + return NULL; + } snprintf(l_wallet->name, DAP_WALLET$SZ_NAME + 1, "%.*s", l_file_hdr.wallet_len, l_wallet_name); strncpy(l_wallet_internal->file_name, a_file_name, sizeof(l_wallet_internal->file_name) - 1); l_wallet_internal->certs_count = l_certs_count; assert(l_wallet_internal->certs_count); + if (!l_wallet_internal->certs_count) { + log_it(L_ERROR, "Count is zero in dap_chain_wallet_open_file"); + DAP_DEL_Z(l_wallet); + dap_fileclose(l_fh); + return NULL; + } l_wallet_internal->certs = DAP_NEW_Z_SIZE(dap_cert_t *, l_wallet_internal->certs_count * sizeof(dap_cert_t *)); assert(l_wallet_internal->certs); + if (!l_wallet_internal->certs) { + log_it(L_ERROR, "Memory allocation error in dap_chain_wallet_open_file"); + DAP_DEL_Z(l_wallet); + dap_fileclose(l_fh); + return NULL; + } #ifdef DAP_OS_WINDOWS LARGE_INTEGER l_offset; -- GitLab