From 684d1290a4ad3f62b5b7707ec6d2523d6944de48 Mon Sep 17 00:00:00 2001 From: iphydf Date: Mon, 8 Jan 2024 15:07:03 +0000 Subject: [PATCH] refactor: Assign malloc return to a local variable first. --- .../docker/tox-bootstrapd.sha256 | 2 +- toxcore/DHT.c | 18 ++++++---- toxcore/LAN_discovery.c | 33 ++++++++++--------- toxcore/TCP_common.c | 14 ++++---- toxcore/TCP_server.c | 8 +++-- toxcore/announce.c | 8 +++-- toxcore/events/conference_invite.c | 7 ++-- toxcore/events/conference_message.c | 7 ++-- toxcore/events/conference_peer_name.c | 7 ++-- toxcore/events/conference_title.c | 7 ++-- toxcore/events/events_alloc.c | 15 +++++---- toxcore/events/file_recv.c | 7 ++-- toxcore/events/file_recv_chunk.c | 7 ++-- toxcore/events/friend_lossless_packet.c | 7 ++-- toxcore/events/friend_lossy_packet.c | 7 ++-- toxcore/events/friend_message.c | 7 ++-- toxcore/events/friend_name.c | 7 ++-- toxcore/events/friend_request.c | 7 ++-- toxcore/events/friend_status_message.c | 7 ++-- toxcore/group_announce.c | 21 ++++++------ toxcore/group_chats.c | 7 ++-- toxcore/group_connection.c | 25 +++++++++----- toxcore/group_moderation.c | 17 ++++++---- toxcore/mono_time.c | 10 +++--- toxcore/net_crypto.c | 28 ++++++++-------- toxcore/network.c | 15 +++++---- toxcore/ping_array.c | 16 +++++---- toxcore/shared_key_cache.c | 9 +++-- toxcore/tox.c | 10 +++--- 29 files changed, 198 insertions(+), 142 deletions(-) diff --git a/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 b/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 index 49fd81049ce..d48c85f89f8 100644 --- a/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 +++ b/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 @@ -1 +1 @@ -6d2321d58fa948279b28ae28a888672164eadaa985c43ce99ed0b71a69033c5c /usr/local/bin/tox-bootstrapd +09ea8dc8ebbe0e755be69348da4373ee4568030579b04c979e8d46c519df322c /usr/local/bin/tox-bootstrapd diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 42b8921585e..82e93828a2b 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -448,8 +448,8 @@ int dht_create_packet(const Memory *mem, const Random *rng, const uint8_t *plain, size_t plain_length, uint8_t *packet, size_t length) { - uint8_t *encrypted = (uint8_t *)mem_balloc(mem, plain_length + CRYPTO_MAC_SIZE); uint8_t nonce[CRYPTO_NONCE_SIZE]; + uint8_t *encrypted = (uint8_t *)mem_balloc(mem, plain_length + CRYPTO_MAC_SIZE); if (encrypted == nullptr) { return -1; @@ -2903,23 +2903,27 @@ static State_Load_Status dht_load_state_callback(void *outer, const uint8_t *dat } mem_delete(dht->mem, dht->loaded_nodes_list); + // Copy to loaded_clients_list - dht->loaded_nodes_list = (Node_format *)mem_valloc(dht->mem, MAX_SAVED_DHT_NODES, sizeof(Node_format)); + Node_format *nodes = (Node_format *)mem_valloc(dht->mem, MAX_SAVED_DHT_NODES, sizeof(Node_format)); - if (dht->loaded_nodes_list == nullptr) { + if (nodes == nullptr) { LOGGER_ERROR(dht->log, "could not allocate %u nodes", MAX_SAVED_DHT_NODES); dht->loaded_num_nodes = 0; break; } - const int num = unpack_nodes(dht->loaded_nodes_list, MAX_SAVED_DHT_NODES, nullptr, data, length, false); + const int num = unpack_nodes(nodes, MAX_SAVED_DHT_NODES, nullptr, data, length, false); - if (num > 0) { - dht->loaded_num_nodes = num; - } else { + if (num < 0) { + // Unpack error happened, we ignore it. dht->loaded_num_nodes = 0; + } else { + dht->loaded_num_nodes = num; } + dht->loaded_nodes_list = nodes; + break; } diff --git a/toxcore/LAN_discovery.c b/toxcore/LAN_discovery.c index db995520d90..ce34551d059 100644 --- a/toxcore/LAN_discovery.c +++ b/toxcore/LAN_discovery.c @@ -62,35 +62,38 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns) return nullptr; } - IP_ADAPTER_INFO *pAdapterInfo = (IP_ADAPTER_INFO *)malloc(sizeof(IP_ADAPTER_INFO)); - unsigned long ulOutBufLen = sizeof(IP_ADAPTER_INFO); + IP_ADAPTER_INFO *adapter_info = (IP_ADAPTER_INFO *)malloc(sizeof(IP_ADAPTER_INFO)); - if (pAdapterInfo == nullptr) { + if (adapter_info == nullptr) { free(broadcast); return nullptr; } - if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) { - free(pAdapterInfo); - pAdapterInfo = (IP_ADAPTER_INFO *)malloc(ulOutBufLen); + unsigned long out_buf_len = sizeof(IP_ADAPTER_INFO); - if (pAdapterInfo == nullptr) { + if (GetAdaptersInfo(adapter_info, &out_buf_len) == ERROR_BUFFER_OVERFLOW) { + free(adapter_info); + IP_ADAPTER_INFO *new_adapter_info = (IP_ADAPTER_INFO *)malloc(out_buf_len); + + if (new_adapter_info == nullptr) { free(broadcast); return nullptr; } + + adapter_info = new_adapter_info; } - const int ret = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen); + const int ret = GetAdaptersInfo(adapter_info, &out_buf_len); if (ret == NO_ERROR) { - IP_ADAPTER_INFO *pAdapter = pAdapterInfo; + IP_ADAPTER_INFO *adapter = adapter_info; - while (pAdapter != nullptr) { + while (adapter != nullptr) { IP gateway = {0}; IP subnet_mask = {0}; - if (addr_parse_ip(pAdapter->IpAddressList.IpMask.String, &subnet_mask) - && addr_parse_ip(pAdapter->GatewayList.IpAddress.String, &gateway)) { + if (addr_parse_ip(adapter->IpAddressList.IpMask.String, &subnet_mask) + && addr_parse_ip(adapter->GatewayList.IpAddress.String, &gateway)) { if (net_family_is_ipv4(gateway.family) && net_family_is_ipv4(subnet_mask.family)) { IP *ip = &broadcast->ips[broadcast->count]; ip->family = net_family_ipv4(); @@ -106,12 +109,12 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns) } } - pAdapter = pAdapter->Next; + adapter = adapter->Next; } } - if (pAdapterInfo != nullptr) { - free(pAdapterInfo); + if (adapter_info != nullptr) { + free(adapter_info); } return broadcast; diff --git a/toxcore/TCP_common.c b/toxcore/TCP_common.c index 157ec14485b..13c17da98dd 100644 --- a/toxcore/TCP_common.c +++ b/toxcore/TCP_common.c @@ -105,17 +105,19 @@ static bool add_priority(TCP_Connection *con, const uint8_t *packet, uint16_t si return false; } - new_list->next = nullptr; - new_list->size = size; - new_list->sent = sent; - new_list->data = (uint8_t *)mem_balloc(con->mem, size); + uint8_t *data = (uint8_t *)mem_balloc(con->mem, size); - if (new_list->data == nullptr) { + if (data == nullptr) { mem_delete(con->mem, new_list); return false; } - memcpy(new_list->data, packet, size); + memcpy(data, packet, size); + new_list->data = data; + new_list->size = size; + + new_list->next = nullptr; + new_list->sent = sent; if (p != nullptr) { p->next = new_list; diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c index 9809da1334f..a54047d7788 100644 --- a/toxcore/TCP_server.c +++ b/toxcore/TCP_server.c @@ -968,20 +968,22 @@ TCP_Server *new_tcp_server(const Logger *logger, const Memory *mem, const Random temp->ns = ns; temp->rng = rng; - temp->socks_listening = (Socket *)mem_valloc(mem, num_sockets, sizeof(Socket)); + Socket *socks_listening = (Socket *)mem_valloc(mem, num_sockets, sizeof(Socket)); - if (temp->socks_listening == nullptr) { + if (socks_listening == nullptr) { LOGGER_ERROR(logger, "socket allocation failed"); mem_delete(mem, temp); return nullptr; } + temp->socks_listening = socks_listening; + #ifdef TCP_SERVER_USE_EPOLL temp->efd = epoll_create(8); if (temp->efd == -1) { LOGGER_ERROR(logger, "epoll initialisation failed"); - mem_delete(mem, temp->socks_listening); + mem_delete(mem, socks_listening); mem_delete(mem, temp); return nullptr; } diff --git a/toxcore/announce.c b/toxcore/announce.c index 19f15f41a57..26645b555ba 100644 --- a/toxcore/announce.c +++ b/toxcore/announce.c @@ -243,13 +243,15 @@ bool announce_store_data(Announcements *announce, const uint8_t *data_public_key free(entry->data); } - entry->data = (uint8_t *)malloc(length); + uint8_t *entry_data = (uint8_t *)malloc(length); - if (entry->data == nullptr) { + if (entry_data == nullptr) { + entry->data = nullptr; // TODO(iphydf): Is this necessary? return false; } - memcpy(entry->data, data, length); + memcpy(entry_data, data, length); + entry->data = entry_data; } entry->length = length; diff --git a/toxcore/events/conference_invite.c b/toxcore/events/conference_invite.c index fe7290a1219..a3a4c2380e7 100644 --- a/toxcore/events/conference_invite.c +++ b/toxcore/events/conference_invite.c @@ -81,13 +81,14 @@ static bool tox_event_conference_invite_set_cookie(Tox_Event_Conference_Invite * conference_invite->cookie_length = 0; } - conference_invite->cookie = (uint8_t *)malloc(cookie_length); + uint8_t *cookie_copy = (uint8_t *)malloc(cookie_length); - if (conference_invite->cookie == nullptr) { + if (cookie_copy == nullptr) { return false; } - memcpy(conference_invite->cookie, cookie, cookie_length); + memcpy(cookie_copy, cookie, cookie_length); + conference_invite->cookie = cookie_copy; conference_invite->cookie_length = cookie_length; return true; } diff --git a/toxcore/events/conference_message.c b/toxcore/events/conference_message.c index d6eeb61f569..6910086abc3 100644 --- a/toxcore/events/conference_message.c +++ b/toxcore/events/conference_message.c @@ -95,13 +95,14 @@ static bool tox_event_conference_message_set_message(Tox_Event_Conference_Messag conference_message->message_length = 0; } - conference_message->message = (uint8_t *)malloc(message_length); + uint8_t *message_copy = (uint8_t *)malloc(message_length); - if (conference_message->message == nullptr) { + if (message_copy == nullptr) { return false; } - memcpy(conference_message->message, message, message_length); + memcpy(message_copy, message, message_length); + conference_message->message = message_copy; conference_message->message_length = message_length; return true; } diff --git a/toxcore/events/conference_peer_name.c b/toxcore/events/conference_peer_name.c index 07aaa5a52bf..0859b99c5d1 100644 --- a/toxcore/events/conference_peer_name.c +++ b/toxcore/events/conference_peer_name.c @@ -81,13 +81,14 @@ static bool tox_event_conference_peer_name_set_name(Tox_Event_Conference_Peer_Na conference_peer_name->name_length = 0; } - conference_peer_name->name = (uint8_t *)malloc(name_length); + uint8_t *name_copy = (uint8_t *)malloc(name_length); - if (conference_peer_name->name == nullptr) { + if (name_copy == nullptr) { return false; } - memcpy(conference_peer_name->name, name, name_length); + memcpy(name_copy, name, name_length); + conference_peer_name->name = name_copy; conference_peer_name->name_length = name_length; return true; } diff --git a/toxcore/events/conference_title.c b/toxcore/events/conference_title.c index 13bd8cbb9d3..2043de132fe 100644 --- a/toxcore/events/conference_title.c +++ b/toxcore/events/conference_title.c @@ -80,13 +80,14 @@ static bool tox_event_conference_title_set_title(Tox_Event_Conference_Title *con conference_title->title_length = 0; } - conference_title->title = (uint8_t *)malloc(title_length); + uint8_t *title_copy = (uint8_t *)malloc(title_length); - if (conference_title->title == nullptr) { + if (title_copy == nullptr) { return false; } - memcpy(conference_title->title, title, title_length); + memcpy(title_copy, title, title_length); + conference_title->title = title_copy; conference_title->title_length = title_length; return true; } diff --git a/toxcore/events/events_alloc.c b/toxcore/events/events_alloc.c index b768eab5734..2fc5d363ab0 100644 --- a/toxcore/events/events_alloc.c +++ b/toxcore/events/events_alloc.c @@ -20,17 +20,20 @@ Tox_Events_State *tox_events_alloc(void *user_data) return state; } - state->events = (Tox_Events *)calloc(1, sizeof(Tox_Events)); + Tox_Events *events = (Tox_Events *)calloc(1, sizeof(Tox_Events)); - if (state->events == nullptr) { + if (events == nullptr) { // It's still null => allocation failed. + state->events = nullptr; state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; - } else { - *state->events = (Tox_Events) { - nullptr - }; + return state; } + *events = (Tox_Events) { + nullptr + }; + state->events = events; + return state; } diff --git a/toxcore/events/file_recv.c b/toxcore/events/file_recv.c index a11132d42e0..0aefb23d145 100644 --- a/toxcore/events/file_recv.c +++ b/toxcore/events/file_recv.c @@ -108,13 +108,14 @@ static bool tox_event_file_recv_set_filename(Tox_Event_File_Recv *file_recv, con file_recv->filename_length = 0; } - file_recv->filename = (uint8_t *)malloc(filename_length); + uint8_t *filename_copy = (uint8_t *)malloc(filename_length); - if (file_recv->filename == nullptr) { + if (filename_copy == nullptr) { return false; } - memcpy(file_recv->filename, filename, filename_length); + memcpy(filename_copy, filename, filename_length); + file_recv->filename = filename_copy; file_recv->filename_length = filename_length; return true; } diff --git a/toxcore/events/file_recv_chunk.c b/toxcore/events/file_recv_chunk.c index aa9bcf1c28e..98c667a13a9 100644 --- a/toxcore/events/file_recv_chunk.c +++ b/toxcore/events/file_recv_chunk.c @@ -94,13 +94,14 @@ static bool tox_event_file_recv_chunk_set_data(Tox_Event_File_Recv_Chunk *file_r file_recv_chunk->data_length = 0; } - file_recv_chunk->data = (uint8_t *)malloc(data_length); + uint8_t *data_copy = (uint8_t *)malloc(data_length); - if (file_recv_chunk->data == nullptr) { + if (data_copy == nullptr) { return false; } - memcpy(file_recv_chunk->data, data, data_length); + memcpy(data_copy, data, data_length); + file_recv_chunk->data = data_copy; file_recv_chunk->data_length = data_length; return true; } diff --git a/toxcore/events/friend_lossless_packet.c b/toxcore/events/friend_lossless_packet.c index 0fd26051649..c1d4ec03a19 100644 --- a/toxcore/events/friend_lossless_packet.c +++ b/toxcore/events/friend_lossless_packet.c @@ -67,13 +67,14 @@ static bool tox_event_friend_lossless_packet_set_data(Tox_Event_Friend_Lossless_ friend_lossless_packet->data_length = 0; } - friend_lossless_packet->data = (uint8_t *)malloc(data_length); + uint8_t *data_copy = (uint8_t *)malloc(data_length); - if (friend_lossless_packet->data == nullptr) { + if (data_copy == nullptr) { return false; } - memcpy(friend_lossless_packet->data, data, data_length); + memcpy(data_copy, data, data_length); + friend_lossless_packet->data = data_copy; friend_lossless_packet->data_length = data_length; return true; } diff --git a/toxcore/events/friend_lossy_packet.c b/toxcore/events/friend_lossy_packet.c index a6639ee64eb..e3ba13615b5 100644 --- a/toxcore/events/friend_lossy_packet.c +++ b/toxcore/events/friend_lossy_packet.c @@ -66,13 +66,14 @@ static bool tox_event_friend_lossy_packet_set_data(Tox_Event_Friend_Lossy_Packet friend_lossy_packet->data_length = 0; } - friend_lossy_packet->data = (uint8_t *)malloc(data_length); + uint8_t *data_copy = (uint8_t *)malloc(data_length); - if (friend_lossy_packet->data == nullptr) { + if (data_copy == nullptr) { return false; } - memcpy(friend_lossy_packet->data, data, data_length); + memcpy(data_copy, data, data_length); + friend_lossy_packet->data = data_copy; friend_lossy_packet->data_length = data_length; return true; } diff --git a/toxcore/events/friend_message.c b/toxcore/events/friend_message.c index be5b0607566..0487b7bfdb9 100644 --- a/toxcore/events/friend_message.c +++ b/toxcore/events/friend_message.c @@ -80,13 +80,14 @@ static bool tox_event_friend_message_set_message(Tox_Event_Friend_Message *frien friend_message->message_length = 0; } - friend_message->message = (uint8_t *)malloc(message_length); + uint8_t *message_copy = (uint8_t *)malloc(message_length); - if (friend_message->message == nullptr) { + if (message_copy == nullptr) { return false; } - memcpy(friend_message->message, message, message_length); + memcpy(message_copy, message, message_length); + friend_message->message = message_copy; friend_message->message_length = message_length; return true; } diff --git a/toxcore/events/friend_name.c b/toxcore/events/friend_name.c index 364ddcf9ff7..6dfc3ddf0e1 100644 --- a/toxcore/events/friend_name.c +++ b/toxcore/events/friend_name.c @@ -66,13 +66,14 @@ static bool tox_event_friend_name_set_name(Tox_Event_Friend_Name *friend_name, c friend_name->name_length = 0; } - friend_name->name = (uint8_t *)malloc(name_length); + uint8_t *name_copy = (uint8_t *)malloc(name_length); - if (friend_name->name == nullptr) { + if (name_copy == nullptr) { return false; } - memcpy(friend_name->name, name, name_length); + memcpy(name_copy, name, name_length); + friend_name->name = name_copy; friend_name->name_length = name_length; return true; } diff --git a/toxcore/events/friend_request.c b/toxcore/events/friend_request.c index e7ecf6783fa..401085803dd 100644 --- a/toxcore/events/friend_request.c +++ b/toxcore/events/friend_request.c @@ -67,13 +67,14 @@ static bool tox_event_friend_request_set_message(Tox_Event_Friend_Request *frien friend_request->message_length = 0; } - friend_request->message = (uint8_t *)malloc(message_length); + uint8_t *message_copy = (uint8_t *)malloc(message_length); - if (friend_request->message == nullptr) { + if (message_copy == nullptr) { return false; } - memcpy(friend_request->message, message, message_length); + memcpy(message_copy, message, message_length); + friend_request->message = message_copy; friend_request->message_length = message_length; return true; } diff --git a/toxcore/events/friend_status_message.c b/toxcore/events/friend_status_message.c index c54d154e731..58bb0b97a7e 100644 --- a/toxcore/events/friend_status_message.c +++ b/toxcore/events/friend_status_message.c @@ -66,13 +66,14 @@ static bool tox_event_friend_status_message_set_message(Tox_Event_Friend_Status_ friend_status_message->message_length = 0; } - friend_status_message->message = (uint8_t *)malloc(message_length); + uint8_t *message_copy = (uint8_t *)malloc(message_length); - if (friend_status_message->message == nullptr) { + if (message_copy == nullptr) { return false; } - memcpy(friend_status_message->message, message, message_length); + memcpy(message_copy, message, message_length); + friend_status_message->message = message_copy; friend_status_message->message_length = message_length; return true; } diff --git a/toxcore/group_announce.c b/toxcore/group_announce.c index 3308ecbd2f0..76bf784c561 100644 --- a/toxcore/group_announce.c +++ b/toxcore/group_announce.c @@ -350,22 +350,24 @@ GC_Peer_Announce *gca_add_announce(const Mono_Time *mono_time, GC_Announces_List // No entry for this chat_id exists so we create one if (announces == nullptr) { - announces = (GC_Announces *)calloc(1, sizeof(GC_Announces)); + GC_Announces *new_announces = (GC_Announces *)calloc(1, sizeof(GC_Announces)); - if (announces == nullptr) { + if (new_announces == nullptr) { return nullptr; } - announces->index = 0; - announces->prev_announce = nullptr; + new_announces->index = 0; + new_announces->prev_announce = nullptr; if (gc_announces_list->root_announces != nullptr) { - gc_announces_list->root_announces->prev_announce = announces; + gc_announces_list->root_announces->prev_announce = new_announces; } - announces->next_announce = gc_announces_list->root_announces; - gc_announces_list->root_announces = announces; - memcpy(announces->chat_id, public_announce->chat_public_key, CHAT_ID_SIZE); + new_announces->next_announce = gc_announces_list->root_announces; + gc_announces_list->root_announces = new_announces; + memcpy(new_announces->chat_id, public_announce->chat_public_key, CHAT_ID_SIZE); + + announces = new_announces; } const uint64_t cur_time = mono_time_get(mono_time); @@ -396,8 +398,7 @@ bool gca_is_valid_announce(const GC_Announce *announce) GC_Announces_List *new_gca_list(void) { - GC_Announces_List *announces_list = (GC_Announces_List *)calloc(1, sizeof(GC_Announces_List)); - return announces_list; + return (GC_Announces_List *)calloc(1, sizeof(GC_Announces_List)); } void kill_gca(GC_Announces_List *announces_list) diff --git a/toxcore/group_chats.c b/toxcore/group_chats.c index e8f213719e3..d7647014700 100644 --- a/toxcore/group_chats.c +++ b/toxcore/group_chats.c @@ -4048,13 +4048,14 @@ int gc_founder_set_password(GC_Chat *chat, const uint8_t *password, uint16_t pas const uint16_t oldlen = chat->shared_state.password_length; if (oldlen > 0) { - oldpasswd = (uint8_t *)malloc(oldlen); + uint8_t *tmp = (uint8_t *)malloc(oldlen); - if (oldpasswd == nullptr) { + if (tmp == nullptr) { return -4; } - memcpy(oldpasswd, chat->shared_state.password, oldlen); + memcpy(tmp, chat->shared_state.password, oldlen); + oldpasswd = tmp; } if (!set_gc_password_local(chat, password, password_length)) { diff --git a/toxcore/group_connection.c b/toxcore/group_connection.c index 2a8045a8c95..18d8ce8a188 100644 --- a/toxcore/group_connection.c +++ b/toxcore/group_connection.c @@ -92,23 +92,28 @@ non_null(1, 2) nullable(3) static bool create_array_entry(const Mono_Time *mono_time, GC_Message_Array_Entry *array_entry, const uint8_t *data, uint16_t length, uint8_t packet_type, uint64_t message_id) { - if (length > 0) { + if (length == 0) { + array_entry->data = nullptr; + array_entry->data_length = 0; + } else { if (data == nullptr) { return false; } - array_entry->data = (uint8_t *)malloc(sizeof(uint8_t) * length); + uint8_t *entry_data = (uint8_t *)malloc(sizeof(uint8_t) * length); - if (array_entry->data == nullptr) { + if (entry_data == nullptr) { + array_entry->data = nullptr; // TODO(iphydf): Is this necessary? return false; } - memcpy(array_entry->data, data, length); + memcpy(entry_data, data, length); + array_entry->data = entry_data; + array_entry->data_length = length; } const uint64_t tm = mono_time_get(mono_time); - array_entry->data_length = length; array_entry->packet_type = packet_type; array_entry->message_id = message_id; array_entry->time_added = tm; @@ -392,10 +397,9 @@ static uint16_t reassemble_packet(const Logger *log, GC_Connection *gconn, uint8 return 0; } - assert(*payload == nullptr); - *payload = (uint8_t *)malloc(packet_length); + uint8_t *tmp_payload = (uint8_t *)malloc(packet_length); - if (*payload == nullptr) { + if (tmp_payload == nullptr) { LOGGER_ERROR(log, "Failed to allocate %u bytes for payload buffer", packet_length); return 0; } @@ -409,12 +413,15 @@ static uint16_t reassemble_packet(const Logger *log, GC_Connection *gconn, uint8 entry = &gconn->recv_array[i]; assert(processed + entry->data_length <= packet_length); - memcpy(*payload + processed, entry->data, entry->data_length); + memcpy(tmp_payload + processed, entry->data, entry->data_length); processed += entry->data_length; clear_array_entry(entry); } + assert(*payload == nullptr); + *payload = tmp_payload; + return processed; } diff --git a/toxcore/group_moderation.c b/toxcore/group_moderation.c index 90a95e17fe0..ac99729e02f 100644 --- a/toxcore/group_moderation.c +++ b/toxcore/group_moderation.c @@ -59,14 +59,17 @@ int mod_list_unpack(Moderation *moderation, const uint8_t *data, uint16_t length uint16_t unpacked_len = 0; for (uint16_t i = 0; i < num_mods; ++i) { - tmp_list[i] = (uint8_t *)malloc(sizeof(uint8_t) * MOD_LIST_ENTRY_SIZE); + uint8_t *entry = (uint8_t *)malloc(sizeof(uint8_t) * MOD_LIST_ENTRY_SIZE); - if (tmp_list[i] == nullptr) { + if (entry == nullptr) { + tmp_list[i] = nullptr; // TODO(iphydf): Is this needed? free_uint8_t_pointer_array(moderation->mem, tmp_list, i); return -1; } - memcpy(tmp_list[i], &data[i * MOD_LIST_ENTRY_SIZE], MOD_LIST_ENTRY_SIZE); + memcpy(entry, &data[i * MOD_LIST_ENTRY_SIZE], MOD_LIST_ENTRY_SIZE); + tmp_list[i] = entry; + unpacked_len += MOD_LIST_ENTRY_SIZE; } @@ -208,13 +211,15 @@ bool mod_list_add_entry(Moderation *moderation, const uint8_t *mod_data) moderation->mod_list = tmp_list; - tmp_list[moderation->num_mods] = (uint8_t *)malloc(sizeof(uint8_t) * MOD_LIST_ENTRY_SIZE); + uint8_t *entry = (uint8_t *)malloc(sizeof(uint8_t) * MOD_LIST_ENTRY_SIZE); - if (tmp_list[moderation->num_mods] == nullptr) { + if (entry == nullptr) { return false; } - memcpy(tmp_list[moderation->num_mods], mod_data, MOD_LIST_ENTRY_SIZE); + memcpy(entry, mod_data, MOD_LIST_ENTRY_SIZE); + + tmp_list[moderation->num_mods] = entry; ++moderation->num_mods; return true; diff --git a/toxcore/mono_time.c b/toxcore/mono_time.c index a3e5baa0275..6e979f7c779 100644 --- a/toxcore/mono_time.c +++ b/toxcore/mono_time.c @@ -119,18 +119,20 @@ Mono_Time *mono_time_new(const Memory *mem, mono_time_current_time_cb *current_t } #ifndef ESP_PLATFORM - mono_time->time_update_lock = (pthread_rwlock_t *)mem_alloc(mem, sizeof(pthread_rwlock_t)); + pthread_rwlock_t *mutex = (pthread_rwlock_t *)mem_alloc(mem, sizeof(pthread_rwlock_t)); - if (mono_time->time_update_lock == nullptr) { + if (mutex == nullptr) { mem_delete(mem, mono_time); return nullptr; } - if (pthread_rwlock_init(mono_time->time_update_lock, nullptr) != 0) { - mem_delete(mem, mono_time->time_update_lock); + if (pthread_rwlock_init(mutex, nullptr) != 0) { + mem_delete(mem, mutex); mem_delete(mem, mono_time); return nullptr; } + + mono_time->time_update_lock = mutex; #endif mono_time_set_current_time_callback(mono_time, current_time_callback, user_data); diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 52bc2e848fd..04a79064b60 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -1868,25 +1868,26 @@ static int create_crypto_connection(Net_Crypto *c) } if (id != -1) { - // Memsetting float/double to 0 is non-portable, so we explicitly set them to 0 - c->crypto_connections[id].packet_recv_rate = 0; - c->crypto_connections[id].packet_send_rate = 0; - c->crypto_connections[id].last_packets_left_rem = 0; - c->crypto_connections[id].packet_send_rate_requested = 0; - c->crypto_connections[id].last_packets_left_requested_rem = 0; - c->crypto_connections[id].mutex = (pthread_mutex_t *)mem_alloc(c->mem, sizeof(pthread_mutex_t)); + pthread_mutex_t *mutex = (pthread_mutex_t *)mem_alloc(c->mem, sizeof(pthread_mutex_t)); - if (c->crypto_connections[id].mutex == nullptr) { + if (mutex == nullptr) { pthread_mutex_unlock(&c->connections_mutex); return -1; } - if (pthread_mutex_init(c->crypto_connections[id].mutex, nullptr) != 0) { - mem_delete(c->mem, c->crypto_connections[id].mutex); + if (pthread_mutex_init(mutex, nullptr) != 0) { + mem_delete(c->mem, mutex); pthread_mutex_unlock(&c->connections_mutex); return -1; } + // Memsetting float/double to 0 is non-portable, so we explicitly set them to 0 + c->crypto_connections[id].packet_recv_rate = 0; + c->crypto_connections[id].packet_send_rate = 0; + c->crypto_connections[id].last_packets_left_rem = 0; + c->crypto_connections[id].packet_send_rate_requested = 0; + c->crypto_connections[id].last_packets_left_requested_rem = 0; + c->crypto_connections[id].mutex = mutex; c->crypto_connections[id].status = CRYPTO_CONN_NO_CONNECTION; } @@ -2022,13 +2023,14 @@ non_null(1, 2, 3) nullable(5) static int handle_new_connection_handshake(Net_Crypto *c, const IP_Port *source, const uint8_t *data, uint16_t length, void *userdata) { - New_Connection n_c; - n_c.cookie = (uint8_t *)mem_balloc(c->mem, COOKIE_LENGTH); + uint8_t *cookie = (uint8_t *)mem_balloc(c->mem, COOKIE_LENGTH); - if (n_c.cookie == nullptr) { + if (cookie == nullptr) { return -1; } + New_Connection n_c; + n_c.cookie = cookie; n_c.source = *source; n_c.cookie_length = COOKIE_LENGTH; diff --git a/toxcore/network.c b/toxcore/network.c index b583fd2f1ed..748f1c78398 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -1796,12 +1796,14 @@ int32_t net_getipport(const Memory *mem, const char *node, IP_Port **res, int to #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION if ((true)) { - *res = (IP_Port *)mem_alloc(mem, sizeof(IP_Port)); - assert(*res != nullptr); - IP_Port *ip_port = *res; + IP_Port *ip_port = (IP_Port *)mem_alloc(mem, sizeof(IP_Port)); + if (ip_port == nullptr) { + abort(); + } ip_port->ip.ip.v4.uint32 = net_htonl(0x7F000003); // 127.0.0.3 ip_port->ip.family = *make_tox_family(AF_INET); + *res = ip_port; return 1; } #endif @@ -1838,14 +1840,15 @@ int32_t net_getipport(const Memory *mem, const char *node, IP_Port **res, int to return 0; } - *res = (IP_Port *)mem_valloc(mem, count, sizeof(IP_Port)); + IP_Port *ip_port = (IP_Port *)mem_valloc(mem, count, sizeof(IP_Port)); - if (*res == nullptr) { + if (ip_port == nullptr) { freeaddrinfo(infos); + *res = nullptr; return -1; } - IP_Port *ip_port = *res; + *res = ip_port; for (struct addrinfo *cur = infos; cur != nullptr; cur = cur->ai_next) { if (cur->ai_socktype && type > 0 && cur->ai_socktype != type) { diff --git a/toxcore/ping_array.c b/toxcore/ping_array.c index 7c0ce87e67f..6ba1cf68c85 100644 --- a/toxcore/ping_array.c +++ b/toxcore/ping_array.c @@ -49,14 +49,15 @@ Ping_Array *ping_array_new(const Memory *mem, uint32_t size, uint32_t timeout) return nullptr; } - empty_array->mem = mem; - empty_array->entries = (Ping_Array_Entry *)mem_valloc(mem, size, sizeof(Ping_Array_Entry)); + Ping_Array_Entry *entries = (Ping_Array_Entry *)mem_valloc(mem, size, sizeof(Ping_Array_Entry)); - if (empty_array->entries == nullptr) { + if (entries == nullptr) { mem_delete(mem, empty_array); return nullptr; } + empty_array->mem = mem; + empty_array->entries = entries; empty_array->last_deleted = 0; empty_array->last_added = 0; empty_array->total_size = size; @@ -115,13 +116,16 @@ uint64_t ping_array_add(Ping_Array *array, const Mono_Time *mono_time, const Ran clear_entry(array, index); } - array->entries[index].data = (uint8_t *)mem_balloc(array->mem, length); + uint8_t *entry_data = (uint8_t *)mem_balloc(array->mem, length); - if (array->entries[index].data == nullptr) { + if (entry_data == nullptr) { + array->entries[index].data = nullptr; return 0; } - memcpy(array->entries[index].data, data, length); + memcpy(entry_data, data, length); + + array->entries[index].data = entry_data; array->entries[index].length = length; array->entries[index].ping_time = mono_time_get(mono_time); ++array->last_added; diff --git a/toxcore/shared_key_cache.c b/toxcore/shared_key_cache.c index 8f8f6c1b400..b52869b7a81 100644 --- a/toxcore/shared_key_cache.c +++ b/toxcore/shared_key_cache.c @@ -68,16 +68,19 @@ Shared_Key_Cache *shared_key_cache_new(const Logger *log, const Mono_Time *mono_ res->mem = mem; res->log = log; res->keys_per_slot = keys_per_slot; + // We take one byte from the public key for each bucket and store keys_per_slot elements there const size_t cache_size = 256 * keys_per_slot; - res->keys = (Shared_Key *)mem_valloc(mem, cache_size, sizeof(Shared_Key)); + Shared_Key *keys = (Shared_Key *)mem_valloc(mem, cache_size, sizeof(Shared_Key)); - if (res->keys == nullptr) { + if (keys == nullptr) { mem_delete(mem, res); return nullptr; } - crypto_memlock(res->keys, cache_size * sizeof(Shared_Key)); + crypto_memlock(keys, cache_size * sizeof(Shared_Key)); + + res->keys = keys; return res; } diff --git a/toxcore/tox.c b/toxcore/tox.c index 00914ec1dcf..4a2be6157f7 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -796,21 +796,21 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) } if (tox_options_get_experimental_thread_safety(opts)) { - tox->mutex = (pthread_mutex_t *)mem_alloc(sys->mem, sizeof(pthread_mutex_t)); + pthread_mutex_t *mutex = (pthread_mutex_t *)mem_alloc(sys->mem, sizeof(pthread_mutex_t)); - if (tox->mutex == nullptr) { + if (mutex == nullptr) { SET_ERROR_PARAMETER(error, TOX_ERR_NEW_MALLOC); mem_delete(sys->mem, tox); tox_options_free(default_options); return nullptr; } - pthread_mutexattr_t attr; - pthread_mutexattr_init(&attr); pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); - pthread_mutex_init(tox->mutex, &attr); + pthread_mutex_init(mutex, &attr); + + tox->mutex = mutex; } else { tox->mutex = nullptr; }