From 9dfad8001752bbf1d606b569720b319d53a46c3f Mon Sep 17 00:00:00 2001 From: iphydf Date: Mon, 17 Jan 2022 01:18:52 +0000 Subject: [PATCH] cleanup: Remove our only use of flexible array members in toxcore. We still have them in toxav. That will need to be cleaned up later. Flexible array members have very limited usefulness. In this particular case, it's almost entirely useless. It confuses static analysers and is yet one more C feature we need to understand and support. It is also the only reason we need special support in tokstyle for calloc with a `+` operator in the member size. --- .../docker/tox-bootstrapd.sha256 | 2 +- toxcore/DHT.c | 2 +- toxcore/TCP_common.c | 17 +++++++++---- toxcore/TCP_common.h | 2 +- toxcore/TCP_server.c | 4 +--- toxcore/group.c | 10 ++++---- toxcore/network.c | 24 +++++++++---------- 7 files changed, 33 insertions(+), 28 deletions(-) diff --git a/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 b/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 index f49cac52..3677c5ea 100644 --- a/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 +++ b/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 @@ -1 +1 @@ -01ff907eae6d12ec2fb597bc0d7bf2549aadf40a8b6bc608f0e910feabb97eec /usr/local/bin/tox-bootstrapd +8802a0afed0bcd3acaafebe22faf1f758935e3914d52472fc3e0c74e055fbc38 /usr/local/bin/tox-bootstrapd diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 45f981cd..8767c294 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -1614,7 +1614,7 @@ int dht_getfriendip(const DHT *dht, const uint8_t *public_key, IP_Port *ip_port) return -1; } - DHT_Friend *const frnd = &dht->friends_list[friend_index]; + const DHT_Friend *const frnd = &dht->friends_list[friend_index]; const uint32_t client_index = index_of_client_pk(frnd->client_list, MAX_FRIEND_CLIENTS, public_key); if (client_index == -1) { diff --git a/toxcore/TCP_common.c b/toxcore/TCP_common.c index 037974e0..46111d94 100644 --- a/toxcore/TCP_common.c +++ b/toxcore/TCP_common.c @@ -13,6 +13,7 @@ void wipe_priority_list(TCP_Priority_List *p) while (p) { TCP_Priority_List *pp = p; p = p->next; + free(pp->data); free(pp); } } @@ -69,6 +70,7 @@ int send_pending_data(TCP_Connection *con) TCP_Priority_List *pp = p; p = p->next; + free(pp->data); free(pp); } @@ -88,15 +90,22 @@ int send_pending_data(TCP_Connection *con) bool add_priority(TCP_Connection *con, const uint8_t *packet, uint16_t size, uint16_t sent) { TCP_Priority_List *p = con->priority_queue_end; - TCP_Priority_List *new_list = (TCP_Priority_List *)calloc(1, sizeof(TCP_Priority_List) + size); + TCP_Priority_List *new_list = (TCP_Priority_List *)calloc(1, sizeof(TCP_Priority_List)); - if (!new_list) { - return 0; + if (new_list == nullptr) { + return false; } new_list->next = nullptr; new_list->size = size; new_list->sent = sent; + new_list->data = (uint8_t *)malloc(size); + + if (new_list->data == nullptr) { + free(new_list); + return false; + } + memcpy(new_list->data, packet, size); if (p) { @@ -106,7 +115,7 @@ bool add_priority(TCP_Connection *con, const uint8_t *packet, uint16_t size, uin } con->priority_queue_end = new_list; - return 1; + return true; } /** return 1 on success. diff --git a/toxcore/TCP_common.h b/toxcore/TCP_common.h index f6273df4..528ab0f1 100644 --- a/toxcore/TCP_common.h +++ b/toxcore/TCP_common.h @@ -14,7 +14,7 @@ struct TCP_Priority_List { TCP_Priority_List *next; uint16_t size; uint16_t sent; - uint8_t data[]; + uint8_t *data; }; void wipe_priority_list(TCP_Priority_List *p); diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c index d7368fca..8cdfbeed 100644 --- a/toxcore/TCP_server.c +++ b/toxcore/TCP_server.c @@ -191,9 +191,7 @@ static int add_accepted(TCP_Server *tcp_server, const Mono_Time *mono_time, TCP_ index = tcp_server->num_accepted_connections; } else { - uint32_t i; - - for (i = tcp_server->size_accepted_connections; i != 0; --i) { + for (uint32_t i = tcp_server->size_accepted_connections; i != 0; --i) { if (tcp_server->accepted_connection_array[i - 1].status == TCP_STATUS_NO_STATUS) { index = i - 1; break; diff --git a/toxcore/group.c b/toxcore/group.c index 49b03dbb..e2009418 100644 --- a/toxcore/group.c +++ b/toxcore/group.c @@ -1069,7 +1069,7 @@ static int add_conn_to_groupchat(Group_Chats *g_c, int friendcon_id, Group_c *g, return ind; } -static unsigned int send_peer_introduced(Group_Chats *g_c, int friendcon_id, uint16_t group_num); +static unsigned int send_peer_introduced(const Group_Chats *g_c, int friendcon_id, uint16_t group_num); /** Removes reason for keeping connection. * @@ -1483,7 +1483,7 @@ static bool try_send_rejoin(Group_Chats *g_c, Group_c *g, const uint8_t *real_pk return true; } -static unsigned int send_peer_query(Group_Chats *g_c, int friendcon_id, uint16_t group_num); +static unsigned int send_peer_query(const Group_Chats *g_c, int friendcon_id, uint16_t group_num); static bool send_invite_response(Group_Chats *g_c, int groupnumber, uint32_t friendnumber, const uint8_t *data, uint16_t length); @@ -2168,7 +2168,7 @@ static int handle_packet_rejoin(Group_Chats *g_c, int friendcon_id, const uint8_ /** return 1 on success. * return 0 on failure */ -static unsigned int send_peer_introduced(Group_Chats *g_c, int friendcon_id, uint16_t group_num) +static unsigned int send_peer_introduced(const Group_Chats *g_c, int friendcon_id, uint16_t group_num) { uint8_t packet[1]; packet[0] = PEER_INTRODUCED_ID; @@ -2179,7 +2179,7 @@ static unsigned int send_peer_introduced(Group_Chats *g_c, int friendcon_id, uin /** return 1 on success. * return 0 on failure */ -static unsigned int send_peer_query(Group_Chats *g_c, int friendcon_id, uint16_t group_num) +static unsigned int send_peer_query(const Group_Chats *g_c, int friendcon_id, uint16_t group_num) { uint8_t packet[1]; packet[0] = PEER_QUERY_ID; @@ -2189,7 +2189,7 @@ static unsigned int send_peer_query(Group_Chats *g_c, int friendcon_id, uint16_t /** return number of peers sent on success. * return 0 on failure. */ -static unsigned int send_peers(Group_Chats *g_c, const Group_c *g, int friendcon_id, uint16_t group_num) +static unsigned int send_peers(const Group_Chats *g_c, const Group_c *g, int friendcon_id, uint16_t group_num) { uint8_t response_packet[MAX_CRYPTO_DATA_SIZE - (1 + sizeof(uint16_t))]; response_packet[0] = PEER_RESPONSE_ID; diff --git a/toxcore/network.c b/toxcore/network.c index add265c3..c0e26c97 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -988,9 +988,8 @@ Networking_Core *new_networking_ex(const Logger *log, IP ip, uint16_t port_from, */ uint16_t port_to_try = port_from; *portptr = net_htons(port_to_try); - int tries; - for (tries = port_from; tries <= port_to; ++tries) { + for (uint16_t tries = port_from; tries <= port_to; ++tries) { #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION int res = 0; #else @@ -1313,13 +1312,7 @@ int addr_resolve(const char *address, IP *to, IP *extra) Family tox_family = to->family; int family = make_family(tox_family); - struct addrinfo *server = nullptr; - struct addrinfo *walker = nullptr; - struct addrinfo hints; - int rc; - int result = 0; - int done = 0; - + struct addrinfo hints; memset(&hints, 0, sizeof(hints)); hints.ai_family = family; hints.ai_socktype = SOCK_DGRAM; // type of socket Tox uses. @@ -1328,7 +1321,9 @@ int addr_resolve(const char *address, IP *to, IP *extra) return 0; } - rc = getaddrinfo(address, nullptr, &hints, &server); + struct addrinfo *server = nullptr; + + const int rc = getaddrinfo(address, nullptr, &hints, &server); // Lookup failed. if (rc != 0) { @@ -1340,14 +1335,17 @@ int addr_resolve(const char *address, IP *to, IP *extra) IP ip6; ip_init(&ip6, 1); // ipv6enabled = 1 - for (walker = server; (walker != nullptr) && !done; walker = walker->ai_next) { + int result = 0; + bool done = false; + + for (struct addrinfo *walker = server; walker != nullptr && !done; walker = walker->ai_next) { switch (walker->ai_family) { case AF_INET: { if (walker->ai_family == family) { /* AF_INET requested, done */ struct sockaddr_in *addr = (struct sockaddr_in *)(void *)walker->ai_addr; get_ip4(&to->ip.v4, &addr->sin_addr); result = TOX_ADDR_RESOLVE_INET; - done = 1; + done = true; } else if (!(result & TOX_ADDR_RESOLVE_INET)) { /* AF_UNSPEC requested, store away */ struct sockaddr_in *addr = (struct sockaddr_in *)(void *)walker->ai_addr; get_ip4(&ip4.ip.v4, &addr->sin_addr); @@ -1363,7 +1361,7 @@ int addr_resolve(const char *address, IP *to, IP *extra) struct sockaddr_in6 *addr = (struct sockaddr_in6 *)(void *)walker->ai_addr; get_ip6(&to->ip.v6, &addr->sin6_addr); result = TOX_ADDR_RESOLVE_INET6; - done = 1; + done = true; } } else if (!(result & TOX_ADDR_RESOLVE_INET6)) { /* AF_UNSPEC requested, store away */ if (walker->ai_addrlen == sizeof(struct sockaddr_in6)) {