From 0426624dcb66591d5e9ef071c8361389e03181b7 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 +- toxav/msi.c | 2 +- toxav/toxav.c | 2 +- toxcore/DHT.c | 20 +++++---- 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.c | 12 ++--- toxcore/group_announce.c | 41 +++++++++++------ toxcore/group_connection.c | 24 ++++++---- toxcore/group_moderation.c | 16 ++++--- toxcore/list.c | 2 +- toxcore/mono_time.c | 10 +++-- toxcore/net_crypto.c | 44 ++++++++++--------- toxcore/network.c | 15 ++++--- toxcore/ping_array.c | 16 ++++--- toxcore/shared_key_cache.c | 9 ++-- toxcore/tox.c | 10 ++--- 32 files changed, 226 insertions(+), 161 deletions(-) diff --git a/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 b/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 index be352b1f..c8db1eba 100644 --- a/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 +++ b/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 @@ -1 +1 @@ -bc830120a87517f830eb85494b769c523bd1696328938d46e9eac1eefea61d38 /usr/local/bin/tox-bootstrapd +d5061af781d04d17bf26174c129b6149e0c8a120ef41133a51a8a7cc5e571e37 /usr/local/bin/tox-bootstrapd diff --git a/toxav/msi.c b/toxav/msi.c index 6375995a..aaa8d551 100644 --- a/toxav/msi.c +++ b/toxav/msi.c @@ -587,7 +587,7 @@ static MSICall *new_call(MSISession *session, uint32_t friend_number) session->calls_tail = friend_number; session->calls_head = friend_number; } else if (session->calls_tail < friend_number) { /* Appending */ - MSICall **tmp = (MSICall **)realloc(session->calls, sizeof(MSICall *) * (friend_number + 1)); + MSICall **tmp = (MSICall **)realloc(session->calls, (friend_number + 1) * sizeof(MSICall *)); if (tmp == nullptr) { free(rc); diff --git a/toxav/toxav.c b/toxav/toxav.c index bdec43b5..ea220838 100644 --- a/toxav/toxav.c +++ b/toxav/toxav.c @@ -1284,7 +1284,7 @@ static ToxAVCall *call_new(ToxAV *av, uint32_t friend_number, Toxav_Err_Call *er av->calls_tail = friend_number; av->calls_head = friend_number; } else if (av->calls_tail < friend_number) { /* Appending */ - ToxAVCall **tmp = (ToxAVCall **)realloc(av->calls, sizeof(ToxAVCall *) * (friend_number + 1)); + ToxAVCall **tmp = (ToxAVCall **)realloc(av->calls, (friend_number + 1) * sizeof(ToxAVCall *)); if (tmp == nullptr) { pthread_mutex_destroy(call->toxav_call_mutex); diff --git a/toxcore/DHT.c b/toxcore/DHT.c index d9a3fd5e..c8c5a2da 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; @@ -2908,23 +2908,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)); - if (dht->loaded_nodes_list == nullptr) { + // Copy to loaded_clients_list + Node_format *nodes = (Node_format *)mem_valloc(dht->mem, MAX_SAVED_DHT_NODES, sizeof(Node_format)); + + 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 db995520..ce34551d 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 157ec144..13c17da9 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 9809da13..a54047d7 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 19f15f41..26645b55 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 fe7290a1..a3a4c238 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 d6eeb61f..6910086a 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 07aaa5a5..0859b99c 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 13bd8cbb..2043de13 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 b768eab5..2fc5d363 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 a11132d4..0aefb23d 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 aa9bcf1c..98c667a1 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 0fd26051..c1d4ec03 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 a6639ee6..e3ba1361 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 be5b0607..0487b7bf 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 364ddcf9..6dfc3ddf 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 e7ecf678..40108580 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 c54d154e..58bb0b97 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.c b/toxcore/group.c index 0086a17e..fea80e40 100644 --- a/toxcore/group.c +++ b/toxcore/group.c @@ -684,7 +684,7 @@ static bool delete_frozen(Group_c *g, uint32_t frozen_index) g->frozen[frozen_index] = g->frozen[g->numfrozen]; } - Group_Peer *const frozen_temp = (Group_Peer *)realloc(g->frozen, sizeof(Group_Peer) * g->numfrozen); + Group_Peer *const frozen_temp = (Group_Peer *)realloc(g->frozen, g->numfrozen * sizeof(Group_Peer)); if (frozen_temp == nullptr) { return false; @@ -725,7 +725,7 @@ static int note_peer_active(Group_Chats *g_c, uint32_t groupnumber, uint16_t pee /* Now thaw the peer */ - Group_Peer *temp = (Group_Peer *)realloc(g->group, sizeof(Group_Peer) * (g->numpeers + 1)); + Group_Peer *temp = (Group_Peer *)realloc(g->group, (g->numpeers + 1) * sizeof(Group_Peer)); if (temp == nullptr) { return -1; @@ -838,7 +838,7 @@ static int addpeer(Group_Chats *g_c, uint32_t groupnumber, const uint8_t *real_p delete_any_peer_with_pk(g_c, groupnumber, real_pk, userdata); - Group_Peer *temp = (Group_Peer *)realloc(g->group, sizeof(Group_Peer) * (g->numpeers + 1)); + Group_Peer *temp = (Group_Peer *)realloc(g->group, (g->numpeers + 1) * sizeof(Group_Peer)); if (temp == nullptr) { return -1; @@ -936,7 +936,7 @@ static bool delpeer(Group_Chats *g_c, uint32_t groupnumber, int peer_index, void g->group[peer_index] = g->group[g->numpeers]; } - Group_Peer *temp = (Group_Peer *)realloc(g->group, sizeof(Group_Peer) * g->numpeers); + Group_Peer *temp = (Group_Peer *)realloc(g->group, g->numpeers * sizeof(Group_Peer)); if (temp == nullptr) { return false; @@ -995,7 +995,7 @@ static bool delete_old_frozen(Group_c *g) qsort(g->frozen, g->numfrozen, sizeof(Group_Peer), cmp_frozen); - Group_Peer *temp = (Group_Peer *)realloc(g->frozen, sizeof(Group_Peer) * g->maxfrozen); + Group_Peer *temp = (Group_Peer *)realloc(g->frozen, g->maxfrozen * sizeof(Group_Peer)); if (temp == nullptr) { return false; @@ -1020,7 +1020,7 @@ static bool freeze_peer(Group_Chats *g_c, uint32_t groupnumber, int peer_index, return false; } - Group_Peer *temp = (Group_Peer *)realloc(g->frozen, sizeof(Group_Peer) * (g->numfrozen + 1)); + Group_Peer *temp = (Group_Peer *)realloc(g->frozen, (g->numfrozen + 1) * sizeof(Group_Peer)); if (temp == nullptr) { return false; diff --git a/toxcore/group_announce.c b/toxcore/group_announce.c index 3308ecbd..0aa704ae 100644 --- a/toxcore/group_announce.c +++ b/toxcore/group_announce.c @@ -339,6 +339,31 @@ int gca_unpack_announces_list(const Logger *log, const uint8_t *data, uint16_t l return announces_count; } +non_null() +static GC_Announces *gca_new_announces( + GC_Announces_List *gc_announces_list, + const GC_Public_Announce *public_announce) +{ + GC_Announces *announces = (GC_Announces *)calloc(1, sizeof(GC_Announces)); + + if (announces == nullptr) { + return nullptr; + } + + announces->index = 0; + announces->prev_announce = nullptr; + + if (gc_announces_list->root_announces != nullptr) { + gc_announces_list->root_announces->prev_announce = 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); + + return announces; +} + GC_Peer_Announce *gca_add_announce(const Mono_Time *mono_time, GC_Announces_List *gc_announces_list, const GC_Public_Announce *public_announce) { @@ -350,22 +375,11 @@ 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)); + announces = gca_new_announces(gc_announces_list, public_announce); if (announces == nullptr) { return nullptr; } - - announces->index = 0; - announces->prev_announce = nullptr; - - if (gc_announces_list->root_announces != nullptr) { - gc_announces_list->root_announces->prev_announce = 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); } const uint64_t cur_time = mono_time_get(mono_time); @@ -396,8 +410,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_connection.c b/toxcore/group_connection.c index 2a8045a8..7f49b603 100644 --- a/toxcore/group_connection.c +++ b/toxcore/group_connection.c @@ -92,23 +92,27 @@ 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(length); - if (array_entry->data == nullptr) { + if (entry_data == nullptr) { 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 +396,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 +412,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 90a95e17..0da113ec 100644 --- a/toxcore/group_moderation.c +++ b/toxcore/group_moderation.c @@ -59,14 +59,16 @@ 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(MOD_LIST_ENTRY_SIZE); - if (tmp_list[i] == nullptr) { + if (entry == nullptr) { 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 +210,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(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/list.c b/toxcore/list.c index 3f6ad228..1096befc 100644 --- a/toxcore/list.c +++ b/toxcore/list.c @@ -123,7 +123,7 @@ static bool resize(BS_List *list, uint32_t new_size) list->data = data; - int *ids = (int *)realloc(list->ids, sizeof(int) * new_size); + int *ids = (int *)realloc(list->ids, new_size * sizeof(int)); if (ids == nullptr) { return false; diff --git a/toxcore/mono_time.c b/toxcore/mono_time.c index a3e5baa0..cf2044af 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 *rwlock = (pthread_rwlock_t *)mem_alloc(mem, sizeof(pthread_rwlock_t)); - if (mono_time->time_update_lock == nullptr) { + if (rwlock == 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(rwlock, nullptr) != 0) { + mem_delete(mem, rwlock); mem_delete(mem, mono_time); return nullptr; } + + mono_time->time_update_lock = rwlock; #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 52bc2e84..1f53e696 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) { + pthread_mutex_t *mutex = (pthread_mutex_t *)mem_alloc(c->mem, sizeof(pthread_mutex_t)); + + if (mutex == nullptr) { + pthread_mutex_unlock(&c->connections_mutex); + return -1; + } + + 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 = (pthread_mutex_t *)mem_alloc(c->mem, sizeof(pthread_mutex_t)); - - if (c->crypto_connections[id].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); - pthread_mutex_unlock(&c->connections_mutex); - return -1; - } - + c->crypto_connections[id].packet_recv_rate = 0.0; + c->crypto_connections[id].packet_send_rate = 0.0; + c->crypto_connections[id].last_packets_left_rem = 0.0; + c->crypto_connections[id].packet_send_rate_requested = 0.0; + c->crypto_connections[id].last_packets_left_requested_rem = 0.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 = {{{{0}}}}; + 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 b583fd2f..748f1c78 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 7c0ce87e..6ba1cf68 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 8f8f6c1b..b52869b7 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 00914ec1..4a2be615 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; }