diff --git a/other/analysis/run-cppcheck b/other/analysis/run-cppcheck index 0e586ba810..e91b8ceef4 100755 --- a/other/analysis/run-cppcheck +++ b/other/analysis/run-cppcheck @@ -16,6 +16,7 @@ CPPCHECK+=("--error-exitcode=1") CPPCHECK+=("--suppress=unmatchedSuppression") # We don't cast function pointers, which cppcheck suggests here. CPPCHECK+=("--suppress=constParameterCallback") +CPPCHECK+=("--suppress=constParameterPointer") # This disagrees with clang's warnings. CPPCHECK+=("--suppress=invalidPrintfArgType_uint") # False positives in switch statements. diff --git a/other/docker/misra/Makefile b/other/docker/misra/Makefile index 9f6e32a319..01c73b6c06 100644 --- a/other/docker/misra/Makefile +++ b/other/docker/misra/Makefile @@ -42,6 +42,10 @@ SUPPRESSIONS += 10.8 # # Reason: this is needed for generic callbacks to make any sense. SUPPRESSIONS += 11.5 +# A conversion shall not remove any const, volatile or _Atomic qualification from the type pointed to by a pointer. +# +# Reason: we need to cast from _Nullable to _Nonnull. +SUPPRESSIONS += 11.8 # The precedence of operators within expressions should be made explicit. # # Reason: this asks us to add a lot of extra parentheses that don't really help diff --git a/other/event_tooling/generate_event_c.cpp b/other/event_tooling/generate_event_c.cpp index 3583b4f628..8c136dd7cd 100644 --- a/other/event_tooling/generate_event_c.cpp +++ b/other/event_tooling/generate_event_c.cpp @@ -417,7 +417,7 @@ void generate_event_impl(const std::string& event_name, const std::vector(user_data)->name_, + std::printf("[%s] %c %s:%u(%s): %s\n", static_cast(user_data)->name_, tox_log_level_name(level), file, line, func, message); }); diff --git a/testing/fuzzing/protodump_reduce.cc b/testing/fuzzing/protodump_reduce.cc index 53d39d0b24..905da9d203 100644 --- a/testing/fuzzing/protodump_reduce.cc +++ b/testing/fuzzing/protodump_reduce.cc @@ -152,7 +152,7 @@ void TestEndToEnd(Fuzz_Data &input) const char *message, void *user_data) { // Log to stdout. if (PROTODUMP_DEBUG) { - std::printf("[tox1] %c %s:%d(%s): %s\n", tox_log_level_name(level), file, line, + std::printf("[tox1] %c %s:%u(%s): %s\n", tox_log_level_name(level), file, line, func, message); } }); diff --git a/toxav/rtp.h b/toxav/rtp.h index aba31b541b..9dd623aead 100644 --- a/toxav/rtp.h +++ b/toxav/rtp.h @@ -78,9 +78,9 @@ struct RTPHeader { uint64_t flags; /** - * The full 32 bit data offset of the current data chunk. The @ref - * offset_lower data member contains the lower 16 bits of this value. For - * frames smaller than 64KiB, @ref offset_full and @ref offset_lower are + * The full 32 bit data offset of the current data chunk. The + * @ref offset_lower data member contains the lower 16 bits of this value. + * For frames smaller than 64KiB, @ref offset_full and @ref offset_lower are * equal. */ uint32_t offset_full; diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 025de96782..d3f808f32a 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -57,8 +57,8 @@ #define KEYS_TIMEOUT 600 typedef struct DHT_Friend_Callback { - dht_ip_cb *ip_callback; - void *data; + dht_ip_cb *_Nullable ip_callback; + void *_Nullable data; int32_t number; } DHT_Friend_Callback; @@ -88,17 +88,17 @@ const Node_format empty_node_format = {{0}}; static_assert(sizeof(empty_dht_friend.lock_flags) * 8 == DHT_FRIEND_MAX_LOCKS, "Bitfield size and number of locks don't match"); typedef struct Cryptopacket_Handler { - cryptopacket_handler_cb *function; - void *object; + cryptopacket_handler_cb *_Nullable function; + void *_Nullable object; } Cryptopacket_Handler; struct DHT { - const Logger *log; - const Network *ns; - Mono_Time *mono_time; - const Memory *mem; - const Random *rng; - Networking_Core *net; + const Logger *_Nonnull log; + const Network *_Nonnull ns; + Mono_Time *_Nonnull mono_time; + const Memory *_Nonnull mem; + const Random *_Nonnull rng; + Networking_Core *_Nonnull net; bool hole_punching_enabled; bool lan_discovery_enabled; @@ -111,18 +111,18 @@ struct DHT { uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE]; uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE]; - DHT_Friend *friends_list; + DHT_Friend *_Nullable friends_list; uint16_t num_friends; - Node_format *loaded_nodes_list; + Node_format *_Nullable loaded_nodes_list; uint32_t loaded_num_nodes; unsigned int loaded_nodes_index; - Shared_Key_Cache *shared_keys_recv; - Shared_Key_Cache *shared_keys_sent; + Shared_Key_Cache *_Nonnull shared_keys_recv; + Shared_Key_Cache *_Nonnull shared_keys_sent; - struct Ping *ping; - Ping_Array *dht_ping_array; + struct Ping *_Nonnull ping; + Ping_Array *_Nonnull dht_ping_array; uint64_t cur_time; Cryptopacket_Handler cryptopackethandlers[256]; @@ -130,7 +130,7 @@ struct DHT { Node_format to_bootstrap[MAX_CLOSE_TO_BOOTSTRAP_NODES]; unsigned int num_to_bootstrap; - dht_nodes_response_cb *nodes_response_callback; + dht_nodes_response_cb *_Nullable nodes_response_callback; }; const uint8_t *dht_friend_public_key(const DHT_Friend *dht_friend) @@ -734,7 +734,7 @@ int get_close_nodes( return get_somewhat_close_nodes( dht->cur_time, public_key, nodes_list, sa_family, dht->close_clientlist, - dht->friends_list, dht->num_friends, + (const DHT_Friend * _Nonnull)dht->friends_list, dht->num_friends, is_lan, want_announce); } @@ -849,9 +849,9 @@ static bool store_node_ok(const Client_data *_Nonnull client, uint64_t cur_time, } typedef struct Client_data_Cmp { - const Memory *mem; + const Memory *_Nonnull mem; uint64_t cur_time; - const uint8_t *comp_public_key; + const uint8_t *_Nonnull comp_public_key; } Client_data_Cmp; static int client_data_cmp(const Client_data_Cmp *_Nonnull cmp, const Client_data *_Nonnull entry1, const Client_data *_Nonnull entry2) @@ -2509,14 +2509,16 @@ DHT *new_dht(const Logger *log, const Memory *mem, const Random *rng, const Netw dht->hole_punching_enabled = hole_punching_enabled; dht->lan_discovery_enabled = lan_discovery_enabled; - dht->ping = ping_new(mem, mono_time, rng, dht); + struct Ping *temp_ping = ping_new(mem, mono_time, rng, dht); - if (dht->ping == nullptr) { + if (temp_ping == nullptr) { LOGGER_ERROR(log, "failed to initialise ping"); kill_dht(dht); return nullptr; } + dht->ping = temp_ping; + networking_registerhandler(dht->net, NET_PACKET_NODES_REQUEST, &handle_nodes_request, dht); networking_registerhandler(dht->net, NET_PACKET_NODES_RESPONSE, &handle_nodes_response, dht); networking_registerhandler(dht->net, NET_PACKET_CRYPTO, &cryptopacket_handle, dht); @@ -2529,23 +2531,36 @@ DHT *new_dht(const Logger *log, const Memory *mem, const Random *rng, const Netw crypto_new_keypair(rng, dht->self_public_key, dht->self_secret_key); - dht->shared_keys_recv = shared_key_cache_new(log, mono_time, mem, dht->self_secret_key, KEYS_TIMEOUT, MAX_KEYS_PER_SLOT); - dht->shared_keys_sent = shared_key_cache_new(log, mono_time, mem, dht->self_secret_key, KEYS_TIMEOUT, MAX_KEYS_PER_SLOT); + Shared_Key_Cache *const temp_shared_keys_recv = shared_key_cache_new(log, mono_time, mem, dht->self_secret_key, KEYS_TIMEOUT, MAX_KEYS_PER_SLOT); + + if (temp_shared_keys_recv == nullptr) { + LOGGER_ERROR(log, "failed to initialise shared key cache"); + kill_dht(dht); + return nullptr; + } + + dht->shared_keys_recv = temp_shared_keys_recv; + + Shared_Key_Cache *const temp_shared_keys_sent = shared_key_cache_new(log, mono_time, mem, dht->self_secret_key, KEYS_TIMEOUT, MAX_KEYS_PER_SLOT); - if (dht->shared_keys_recv == nullptr || dht->shared_keys_sent == nullptr) { + if (temp_shared_keys_sent == nullptr) { LOGGER_ERROR(log, "failed to initialise shared key cache"); kill_dht(dht); return nullptr; } - dht->dht_ping_array = ping_array_new(mem, DHT_PING_ARRAY_SIZE, PING_TIMEOUT); + dht->shared_keys_sent = temp_shared_keys_sent; - if (dht->dht_ping_array == nullptr) { + Ping_Array *const temp_ping_array = ping_array_new(mem, DHT_PING_ARRAY_SIZE, PING_TIMEOUT); + + if (temp_ping_array == nullptr) { LOGGER_ERROR(log, "failed to initialise ping array"); kill_dht(dht); return nullptr; } + dht->dht_ping_array = temp_ping_array; + for (uint32_t i = 0; i < DHT_FAKE_FRIEND_NUMBER; ++i) { uint8_t random_public_key_bytes[CRYPTO_PUBLIC_KEY_SIZE]; uint8_t random_secret_key_bytes[CRYPTO_SECRET_KEY_SIZE]; @@ -2600,7 +2615,7 @@ void kill_dht(DHT *dht) networking_registerhandler(dht->net, NET_PACKET_NODES_RESPONSE, nullptr, nullptr); networking_registerhandler(dht->net, NET_PACKET_CRYPTO, nullptr, nullptr); networking_registerhandler(dht->net, NET_PACKET_LAN_DISCOVERY, nullptr, nullptr); - cryptopacket_registerhandler(dht, CRYPTO_PACKET_NAT_PING, nullptr, nullptr); + cryptopacket_registerhandler((DHT * _Nonnull)dht, CRYPTO_PACKET_NAT_PING, nullptr, nullptr); shared_key_cache_free(dht->shared_keys_recv); shared_key_cache_free(dht->shared_keys_sent); diff --git a/toxcore/DHT.h b/toxcore/DHT.h index 43ddbadb07..2bfa8ef3e0 100644 --- a/toxcore/DHT.h +++ b/toxcore/DHT.h @@ -212,7 +212,7 @@ int unpack_nodes(Node_format *_Nonnull nodes, uint16_t max_num_nodes, uint16_t * uint16_t length, bool tcp_enabled); /*----------------------------------------------------------------------------------*/ -typedef int cryptopacket_handler_cb(void *_Nonnull object, const IP_Port *_Nonnull source, const uint8_t *_Nonnull source_pubkey, +typedef int cryptopacket_handler_cb(void *_Nullable object, const IP_Port *_Nonnull source, const uint8_t *_Nonnull source_pubkey, const uint8_t *_Nonnull packet, uint16_t length, void *_Nullable userdata); typedef struct DHT DHT; diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index a04e1f639a..815013a24d 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -1555,7 +1555,7 @@ int send_file_data(const Messenger *m, int32_t friendnumber, uint32_t filenumber * @return true if there's still work to do, false otherwise. * */ -static bool do_all_filetransfers(Messenger *_Nonnull m, int32_t friendnumber, void *_Nonnull userdata, uint32_t *_Nonnull free_slots) +static bool do_all_filetransfers(Messenger *_Nonnull m, int32_t friendnumber, void *_Nullable userdata, uint32_t *_Nonnull free_slots) { Friend *const friendcon = &m->friendlist[friendnumber]; @@ -2426,7 +2426,7 @@ static bool self_announce_group(const Messenger *_Nonnull m, GC_Chat *_Nonnull c GC_Public_Announce announce = {{{{{0}}}}}; const bool ip_port_is_set = chat->self_udp_status != SELF_UDP_STATUS_NONE; - const int tcp_num = tcp_copy_connected_relays(chat->tcp_conn, announce.base_announce.tcp_relays, + const int tcp_num = tcp_copy_connected_relays((const TCP_Connections * _Nonnull)chat->tcp_conn, announce.base_announce.tcp_relays, GCA_MAX_ANNOUNCED_TCP_RELAYS); if (tcp_num == 0 && !ip_port_is_set) { @@ -2514,7 +2514,7 @@ void do_messenger(Messenger *m, void *userdata) local_ip_port.port = net_htons(m->options.tcp_server_port); local_ip_port.ip.family = net_family_ipv4(); local_ip_port.ip.ip.v4 = get_ip4_loopback(); - add_tcp_relay(m->net_crypto, &local_ip_port, tcp_server_public_key(m->tcp_server)); + add_tcp_relay(m->net_crypto, &local_ip_port, tcp_server_public_key((const TCP_Server * _Nonnull)m->tcp_server)); } } @@ -2524,7 +2524,7 @@ void do_messenger(Messenger *m, void *userdata) } if (m->tcp_server != nullptr) { - do_tcp_server(m->tcp_server, m->mono_time); + do_tcp_server((TCP_Server * _Nonnull)m->tcp_server, m->mono_time); } do_net_crypto(m->net_crypto, userdata); @@ -3374,21 +3374,24 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random * m->mem = mem; m->rng = rng; m->ns = ns; + m->forwarding = nullptr; + m->announce = nullptr; + m->tcp_server = nullptr; - m->fr = friendreq_new(mem); - - if (m->fr == nullptr) { + Friend_Requests *fr = friendreq_new(mem); + if (fr == nullptr) { mem_delete(mem, m); return nullptr; } + m->fr = fr; - m->log = logger_new(mem); - - if (m->log == nullptr) { + Logger *log = logger_new(mem); + if (log == nullptr) { friendreq_kill(m->fr); mem_delete(mem, m); return nullptr; } + m->log = log; logger_callback_log(m->log, options->log_callback, options->log_context, options->log_user_data); @@ -3400,15 +3403,16 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random * options->udp_disabled = true; } + Networking_Core *net; if (options->udp_disabled) { - m->net = new_networking_no_udp(m->log, m->mem, m->ns); + net = new_networking_no_udp(m->log, m->mem, m->ns); } else { IP ip; ip_init(&ip, options->ipv6enabled); - m->net = new_networking_ex(m->log, m->mem, m->ns, &ip, options->port_range[0], options->port_range[1], &net_err); + net = new_networking_ex(m->log, m->mem, m->ns, &ip, options->port_range[0], options->port_range[1], &net_err); } - if (m->net == nullptr) { + if (net == nullptr) { friendreq_kill(m->fr); if (error != nullptr && net_err == 1) { @@ -3420,20 +3424,20 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random * mem_delete(mem, m); return nullptr; } + m->net = net; - m->dht = new_dht(m->log, m->mem, m->rng, m->ns, m->mono_time, m->net, options->hole_punching_enabled, options->local_discovery_enabled); - - if (m->dht == nullptr) { + DHT *dht = new_dht(m->log, m->mem, m->rng, m->ns, m->mono_time, m->net, options->hole_punching_enabled, options->local_discovery_enabled); + if (dht == nullptr) { kill_networking(m->net); friendreq_kill(m->fr); logger_kill(m->log); mem_delete(mem, m); return nullptr; } + m->dht = dht; - m->tcp_np = netprof_new(m->log, mem); - - if (m->tcp_np == nullptr) { + Net_Profile *tcp_np = netprof_new(m->log, mem); + if (tcp_np == nullptr) { LOGGER_WARNING(m->log, "TCP netprof initialisation failed"); kill_dht(m->dht); kill_networking(m->net); @@ -3442,10 +3446,10 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random * mem_delete(mem, m); return nullptr; } + m->tcp_np = tcp_np; - m->net_crypto = new_net_crypto(m->log, m->mem, m->rng, m->ns, m->mono_time, m->dht, &options->proxy_info, m->tcp_np); - - if (m->net_crypto == nullptr) { + Net_Crypto *net_crypto = new_net_crypto(m->log, m->mem, m->rng, m->ns, m->mono_time, m->dht, &options->proxy_info, m->tcp_np); + if (net_crypto == nullptr) { LOGGER_WARNING(m->log, "net_crypto initialisation failed"); netprof_kill(mem, m->tcp_np); @@ -3456,10 +3460,10 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random * mem_delete(mem, m); return nullptr; } + m->net_crypto = net_crypto; - m->group_announce = new_gca_list(m->mem); - - if (m->group_announce == nullptr) { + GC_Announces_List *group_announce = new_gca_list(m->mem); + if (group_announce == nullptr) { LOGGER_WARNING(m->log, "DHT group chats initialisation failed"); kill_net_crypto(m->net_crypto); @@ -3471,35 +3475,33 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random * mem_delete(mem, m); return nullptr; } + m->group_announce = group_announce; if (options->dht_announcements_enabled) { m->forwarding = new_forwarding(m->log, m->mem, m->rng, m->mono_time, m->dht); if (m->forwarding != nullptr) { - m->announce = new_announcements(m->log, m->mem, m->rng, m->mono_time, m->forwarding); - } else { - m->announce = nullptr; + m->announce = new_announcements(m->log, m->mem, m->rng, m->mono_time, (Forwarding * _Nonnull)m->forwarding); } - } else { - m->forwarding = nullptr; - m->announce = nullptr; } - m->onion = new_onion(m->log, m->mem, m->mono_time, m->rng, m->dht); - m->onion_a = new_onion_announce(m->log, m->mem, m->rng, m->mono_time, m->dht); - m->onion_c = new_onion_client(m->log, m->mem, m->rng, m->mono_time, m->net_crypto); - if (m->onion_c != nullptr) { - m->fr_c = new_friend_connections(m->log, m->mem, m->mono_time, m->ns, m->onion_c, options->local_discovery_enabled); + Onion *onion = new_onion(m->log, m->mem, m->mono_time, m->rng, m->dht); + Onion_Announce *onion_a = new_onion_announce(m->log, m->mem, m->rng, m->mono_time, m->dht); + Onion_Client *onion_c = new_onion_client(m->log, m->mem, m->rng, m->mono_time, m->net_crypto); + Friend_Connections *fr_c = nullptr; + + if (onion_c != nullptr) { + fr_c = new_friend_connections(m->log, m->mem, m->mono_time, m->ns, onion_c, options->local_discovery_enabled); } if ((options->dht_announcements_enabled && (m->forwarding == nullptr || m->announce == nullptr)) || - m->onion == nullptr || m->onion_a == nullptr || m->onion_c == nullptr || m->fr_c == nullptr) { + onion == nullptr || onion_a == nullptr || onion_c == nullptr || fr_c == nullptr) { LOGGER_WARNING(m->log, "onion initialisation failed"); - kill_onion(m->onion); - kill_onion_announce(m->onion_a); - kill_onion_client(m->onion_c); + kill_onion(onion); + kill_onion_announce(onion_a); + kill_onion_client(onion_c); kill_gca(m->group_announce); - kill_friend_connections(m->fr_c); + kill_friend_connections(fr_c); kill_announcements(m->announce); kill_forwarding(m->forwarding); kill_net_crypto(m->net_crypto); @@ -3511,12 +3513,15 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random * mem_delete(mem, m); return nullptr; } + m->onion = onion; + m->onion_a = onion_a; + m->onion_c = onion_c; + m->fr_c = fr_c; gca_onion_init(m->group_announce, m->onion_a); - m->group_handler = new_dht_groupchats(m); - - if (m->group_handler == nullptr) { + GC_Session *group_handler = new_dht_groupchats(m); + if (group_handler == nullptr) { LOGGER_WARNING(m->log, "conferences initialisation failed"); kill_onion(m->onion); @@ -3535,6 +3540,7 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random * mem_delete(mem, m); return nullptr; } + m->group_handler = group_handler; if (options->tcp_server_port != 0) { m->tcp_server = new_tcp_server(m->log, m->mem, m->rng, m->ns, options->ipv6enabled, 1, @@ -3614,7 +3620,7 @@ void kill_messenger(Messenger *m) kill_networking(m->net); for (uint32_t i = 0; i < m->numfriends; ++i) { - clear_receipts(m, i); + clear_receipts((Messenger * _Nonnull)m, i); } mem_delete(m->mem, m->friendlist); diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index b45c1dd336..d090abdec4 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -241,28 +241,28 @@ typedef struct Friend { } Friend; struct Messenger { - Logger *_Nullable log; - Mono_Time *_Nullable mono_time; - const Memory *_Nullable mem; - const Random *_Nullable rng; - const Network *_Nullable ns; + Logger *_Nonnull log; + Mono_Time *_Nonnull mono_time; + const Memory *_Nonnull mem; + const Random *_Nonnull rng; + const Network *_Nonnull ns; Networking_Core *_Nonnull net; Net_Crypto *_Nonnull net_crypto; - Net_Profile *_Nullable tcp_np; + Net_Profile *_Nonnull tcp_np; DHT *_Nonnull dht; Forwarding *_Nullable forwarding; Announcements *_Nullable announce; - Onion *_Nullable onion; - Onion_Announce *_Nullable onion_a; - Onion_Client *_Nullable onion_c; + Onion *_Nonnull onion; + Onion_Announce *_Nonnull onion_a; + Onion_Client *_Nonnull onion_c; - Friend_Connections *_Nullable fr_c; + Friend_Connections *_Nonnull fr_c; TCP_Server *_Nullable tcp_server; - Friend_Requests *_Nullable fr; + Friend_Requests *_Nonnull fr; uint8_t name[MAX_NAME_LENGTH]; uint16_t name_length; diff --git a/toxcore/TCP_client.c b/toxcore/TCP_client.c index 4defce7a03..3f36fd63fa 100644 --- a/toxcore/TCP_client.c +++ b/toxcore/TCP_client.c @@ -52,23 +52,23 @@ struct TCP_Client_Connection { uint64_t ping_request_id; TCP_Client_Conn connections[NUM_CLIENT_CONNECTIONS]; - tcp_routing_response_cb *response_callback; - void *response_callback_object; - tcp_routing_status_cb *status_callback; - void *status_callback_object; - tcp_routing_data_cb *data_callback; - void *data_callback_object; - tcp_oob_data_cb *oob_data_callback; - void *oob_data_callback_object; + tcp_routing_response_cb *_Nullable response_callback; + void *_Nullable response_callback_object; + tcp_routing_status_cb *_Nullable status_callback; + void *_Nullable status_callback_object; + tcp_routing_data_cb *_Nullable data_callback; + void *_Nullable data_callback_object; + tcp_oob_data_cb *_Nullable oob_data_callback; + void *_Nullable oob_data_callback_object; - tcp_onion_response_cb *onion_callback; - void *onion_callback_object; + tcp_onion_response_cb *_Nullable onion_callback; + void *_Nullable onion_callback_object; - forwarded_response_cb *forwarded_response_callback; - void *forwarded_response_callback_object; + forwarded_response_cb *_Nullable forwarded_response_callback; + void *_Nullable forwarded_response_callback_object; /* Can be used by user. */ - void *custom_object; + void *_Nullable custom_object; uint32_t custom_uint; }; @@ -630,7 +630,7 @@ TCP_Client_Connection *new_tcp_connection( return nullptr; } - if (!connect_sock_to(ns, logger, mem, sock, ip_port, proxy_info)) { + if (!connect_sock_to(ns, logger, mem, sock, ip_port, (const TCP_Proxy_Info * _Nonnull)proxy_info)) { Ip_Ntoa ip_ntoa; LOGGER_WARNING(logger, "Failed to connect TCP socket to %s:%u", net_ip_ntoa(&ip_port->ip, &ip_ntoa), net_ntohs(ip_port->port)); @@ -711,7 +711,7 @@ static int handle_tcp_client_routing_response(TCP_Client_Connection *_Nonnull co memcpy(conn->connections[con_id].public_key, data + 2, CRYPTO_PUBLIC_KEY_SIZE); if (conn->response_callback != nullptr) { - conn->response_callback(conn->response_callback_object, con_id, conn->connections[con_id].public_key); + conn->response_callback((void *_Nonnull)conn->response_callback_object, con_id, conn->connections[con_id].public_key); } return 0; @@ -736,7 +736,7 @@ static int handle_tcp_client_connection_notification(TCP_Client_Connection *_Non conn->connections[con_id].status = 2; if (conn->status_callback != nullptr) { - conn->status_callback(conn->status_callback_object, conn->connections[con_id].number, con_id, + conn->status_callback((void *_Nonnull)conn->status_callback_object, conn->connections[con_id].number, con_id, conn->connections[con_id].status); } @@ -766,7 +766,7 @@ static int handle_tcp_client_disconnect_notification(TCP_Client_Connection *_Non conn->connections[con_id].status = 1; if (conn->status_callback != nullptr) { - conn->status_callback(conn->status_callback_object, conn->connections[con_id].number, con_id, + conn->status_callback((void *_Nonnull)conn->status_callback_object, conn->connections[con_id].number, con_id, conn->connections[con_id].status); } @@ -813,7 +813,7 @@ static int handle_tcp_client_oob_recv(TCP_Client_Connection *_Nonnull conn, cons } if (conn->oob_data_callback != nullptr) { - conn->oob_data_callback(conn->oob_data_callback_object, data + 1, data + 1 + CRYPTO_PUBLIC_KEY_SIZE, + conn->oob_data_callback((void *_Nonnull)conn->oob_data_callback_object, data + 1, data + 1 + CRYPTO_PUBLIC_KEY_SIZE, length - (1 + CRYPTO_PUBLIC_KEY_SIZE), userdata); } @@ -854,7 +854,7 @@ static int handle_tcp_client_packet(const Logger *_Nonnull logger, TCP_Client_Co case TCP_PACKET_ONION_RESPONSE: { if (conn->onion_callback != nullptr) { - conn->onion_callback(conn->onion_callback_object, data + 1, length - 1, userdata); + conn->onion_callback((void *_Nonnull)conn->onion_callback_object, data + 1, length - 1, userdata); } return 0; } @@ -874,7 +874,7 @@ static int handle_tcp_client_packet(const Logger *_Nonnull logger, TCP_Client_Co const uint8_t con_id = data[0] - NUM_RESERVED_PORTS; if (conn->data_callback != nullptr) { - conn->data_callback(conn->data_callback_object, conn->connections[con_id].number, con_id, data + 1, length - 1, + conn->data_callback((void *_Nonnull)conn->data_callback_object, conn->connections[con_id].number, con_id, data + 1, length - 1, userdata); } } @@ -1036,6 +1036,6 @@ void kill_tcp_connection(TCP_Client_Connection *tcp_connection) wipe_priority_list(tcp_connection->con.mem, tcp_connection->con.priority_queue_start); kill_sock(tcp_connection->con.ns, tcp_connection->con.sock); - crypto_memzero(tcp_connection, sizeof(TCP_Client_Connection)); + crypto_memzero((void *_Nonnull)tcp_connection, sizeof(TCP_Client_Connection)); mem_delete(mem, tcp_connection); } diff --git a/toxcore/TCP_connection.c b/toxcore/TCP_connection.c index 6b2f994620..eac34884fb 100644 --- a/toxcore/TCP_connection.c +++ b/toxcore/TCP_connection.c @@ -25,33 +25,33 @@ #include "util.h" struct TCP_Connections { - const Logger *logger; - const Memory *mem; - const Random *rng; - Mono_Time *mono_time; - const Network *ns; - DHT *dht; + const Logger *_Nonnull logger; + const Memory *_Nonnull mem; + const Random *_Nonnull rng; + Mono_Time *_Nonnull mono_time; + const Network *_Nonnull ns; + DHT *_Nonnull dht; uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE]; uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE]; - TCP_Connection_to *connections; + TCP_Connection_to *_Nullable connections; uint32_t connections_length; /* Length of connections array. */ - TCP_con *tcp_connections; + TCP_con *_Nullable tcp_connections; uint32_t tcp_connections_length; /* Length of tcp_connections array. */ - tcp_data_cb *tcp_data_callback; - void *tcp_data_callback_object; + tcp_data_cb *_Nullable tcp_data_callback; + void *_Nullable tcp_data_callback_object; - tcp_oob_cb *tcp_oob_callback; - void *tcp_oob_callback_object; + tcp_oob_cb *_Nullable tcp_oob_callback; + void *_Nullable tcp_oob_callback_object; - tcp_onion_cb *tcp_onion_callback; - void *tcp_onion_callback_object; + tcp_onion_cb *_Nullable tcp_onion_callback; + void *_Nullable tcp_onion_callback_object; - forwarded_response_cb *tcp_forwarded_response_callback; - void *tcp_forwarded_response_callback_object; + forwarded_response_cb *_Nullable tcp_forwarded_response_callback; + void *_Nullable tcp_forwarded_response_callback_object; TCP_Proxy_Info proxy_info; @@ -59,7 +59,7 @@ struct TCP_Connections { uint16_t onion_num_conns; /* Network profile for all TCP client packets. */ - Net_Profile *net_profile; + Net_Profile *_Nullable net_profile; }; static const TCP_Connection_to empty_tcp_connection_to = {0}; @@ -80,7 +80,7 @@ uint32_t tcp_connections_count(const TCP_Connections *tcp_c) * @retval -1 if mem_vrealloc fails. * @retval 0 if it succeeds. */ -static int realloc_tcp_connection_to(const Memory *_Nonnull mem, TCP_Connection_to *_Nullable *_Nonnull array, size_t num) +static int realloc_tcp_connection_to(const Memory *_Nonnull mem, TCP_Connection_to *_Nullable *array, size_t num) { if (num == 0) { mem_delete(mem, *array); @@ -100,7 +100,7 @@ static int realloc_tcp_connection_to(const Memory *_Nonnull mem, TCP_Connection_ return 0; } -static int realloc_tcp_con(const Memory *_Nonnull mem, TCP_con *_Nullable *_Nonnull array, size_t num) +static int realloc_tcp_con(const Memory *_Nonnull mem, TCP_con *_Nullable *array, size_t num) { if (num == 0) { mem_delete(mem, *array); @@ -331,7 +331,11 @@ int send_packet_tcp_connection(const TCP_Connections *tcp_c, int connections_num continue; } - ret = send_data(tcp_c->logger, tcp_con->connection, connection_id, packet, length); + if (tcp_con->connection == nullptr) { + LOGGER_ERROR(tcp_c->logger, "TCP connection is null for connection number %u", tcp_con_num); + continue; + } + ret = send_data(tcp_c->logger, (TCP_Client_Connection * _Nonnull)tcp_con->connection, connection_id, packet, length); if (ret == 0) { limit_reached = true; @@ -366,7 +370,11 @@ int send_packet_tcp_connection(const TCP_Connections *tcp_c, int connections_num continue; } - if (send_oob_packet(tcp_c->logger, tcp_con->connection, con_to->public_key, packet, length) == 1) { + if (tcp_con->connection == nullptr) { + LOGGER_ERROR(tcp_c->logger, "TCP connection is null for connection number %u", tcp_con_num); + continue; + } + if (send_oob_packet(tcp_c->logger, (TCP_Client_Connection * _Nonnull)tcp_con->connection, con_to->public_key, packet, length) == 1) { sent_any = true; } } @@ -406,7 +414,11 @@ int get_random_tcp_onion_conn_number(const TCP_Connections *tcp_c) static int get_conn_number_by_ip_port(const TCP_Connections *_Nonnull tcp_c, const IP_Port *_Nonnull ip_port) { for (uint32_t i = 0; i < tcp_c->tcp_connections_length; ++i) { - const IP_Port conn_ip_port = tcp_con_ip_port(tcp_c->tcp_connections[i].connection); + if (tcp_c->tcp_connections[i].connection == nullptr) { + LOGGER_ERROR(tcp_c->logger, "TCP connection is null for connection index %u", i); + continue; + } + const IP_Port conn_ip_port = tcp_con_ip_port((const TCP_Client_Connection * _Nonnull)tcp_c->tcp_connections[i].connection); if (ipport_equal(ip_port, &conn_ip_port) && tcp_c->tcp_connections[i].status == TCP_CONN_CONNECTED) { @@ -430,7 +442,11 @@ bool tcp_get_random_conn_ip_port(const TCP_Connections *tcp_c, IP_Port *ip_port) return false; } - *ip_port = tcp_con_ip_port(tcp_c->tcp_connections[index].connection); + if (tcp_c->tcp_connections[index].connection == nullptr) { + LOGGER_ERROR(tcp_c->logger, "TCP connection is null for connection index %d", index); + return false; + } + *ip_port = tcp_con_ip_port((const TCP_Client_Connection * _Nonnull)tcp_c->tcp_connections[index].connection); return true; } @@ -447,7 +463,11 @@ int tcp_send_onion_request(TCP_Connections *tcp_c, uint32_t tcp_connections_numb } if (tcp_c->tcp_connections[tcp_connections_number].status == TCP_CONN_CONNECTED) { - const int ret = send_onion_request(tcp_c->logger, tcp_c->tcp_connections[tcp_connections_number].connection, data, + if (tcp_c->tcp_connections[tcp_connections_number].connection == nullptr) { + LOGGER_ERROR(tcp_c->logger, "TCP connection is null for connection number %u", tcp_connections_number); + return -1; + } + const int ret = send_onion_request(tcp_c->logger, (TCP_Client_Connection * _Nonnull)tcp_c->tcp_connections[tcp_connections_number].connection, data, length); if (ret == 1) { @@ -477,15 +497,23 @@ int tcp_send_forward_request(const Logger *logger, TCP_Connections *tcp_c, const } if (chain_length == 0) { - return send_forward_request_tcp(logger, tcp_c->tcp_connections[index].connection, dht_node, data, + if (tcp_c->tcp_connections[index].connection == nullptr) { + LOGGER_ERROR(tcp_c->logger, "TCP connection is null for connection index %d", index); + return -1; + } + return send_forward_request_tcp(logger, (TCP_Client_Connection * _Nonnull)tcp_c->tcp_connections[index].connection, dht_node, data, data_length) == 1 ? 0 : -1; } const uint16_t len = forward_chain_packet_size(chain_length, data_length); VLA(uint8_t, packet, len); + if (tcp_c->tcp_connections[index].connection == nullptr) { + LOGGER_ERROR(tcp_c->logger, "TCP connection is null for connection index %d", index); + return -1; + } return create_forward_chain_packet(chain_keys, chain_length, data, data_length, packet) - && send_forward_request_tcp(logger, tcp_c->tcp_connections[index].connection, dht_node, packet, len) == 1 ? 0 : -1; + && send_forward_request_tcp(logger, (TCP_Client_Connection * _Nonnull)tcp_c->tcp_connections[index].connection, dht_node, packet, len) == 1 ? 0 : -1; } /** @brief Send an oob packet via the TCP relay corresponding to tcp_connections_number. @@ -506,7 +534,11 @@ int tcp_send_oob_packet(const TCP_Connections *tcp_c, unsigned int tcp_connectio return -1; } - const int ret = send_oob_packet(tcp_c->logger, tcp_con->connection, public_key, packet, length); + if (tcp_con->connection == nullptr) { + LOGGER_ERROR(tcp_c->logger, "TCP connection is null for tcp_con"); + return -1; + } + const int ret = send_oob_packet(tcp_c->logger, (TCP_Client_Connection * _Nonnull)tcp_con->connection, public_key, packet, length); if (ret == 1) { return 0; @@ -623,7 +655,11 @@ static int find_tcp_connection_relay(const TCP_Connections *tcp_c, const uint8_t return i; } } else { - if (pk_equal(tcp_con_public_key(tcp_con->connection), relay_pk)) { + if (tcp_con->connection == nullptr) { + LOGGER_ERROR(tcp_c->logger, "TCP connection is null for tcp_con"); + return -1; + } + if (pk_equal(tcp_con_public_key((const TCP_Client_Connection * _Nonnull)tcp_con->connection), relay_pk)) { return i; } } @@ -690,7 +726,11 @@ int kill_tcp_connection_to(TCP_Connections *tcp_c, int connections_number) } if (tcp_con->status == TCP_CONN_CONNECTED) { - send_disconnect_request(tcp_c->logger, tcp_con->connection, con_to->connections[i].connection_id); + if (tcp_con->connection == nullptr) { + LOGGER_ERROR(tcp_c->logger, "TCP connection is null for tcp_con"); + continue; + } + send_disconnect_request(tcp_c->logger, (TCP_Client_Connection * _Nonnull)tcp_con->connection, con_to->connections[i].connection_id); } if (con_to->connections[i].status == TCP_CONNECTIONS_STATUS_ONLINE) { @@ -907,9 +947,13 @@ static int reconnect_tcp_relay_connection(TCP_Connections *_Nonnull tcp_c, int t return -1; } - const IP_Port ip_port = tcp_con_ip_port(tcp_con->connection); + if (tcp_con->connection == nullptr) { + LOGGER_ERROR(tcp_c->logger, "TCP connection is null for tcp_con"); + return -1; + } + const IP_Port ip_port = tcp_con_ip_port((const TCP_Client_Connection * _Nonnull)tcp_con->connection); uint8_t relay_pk[CRYPTO_PUBLIC_KEY_SIZE]; - memcpy(relay_pk, tcp_con_public_key(tcp_con->connection), CRYPTO_PUBLIC_KEY_SIZE); + memcpy(relay_pk, tcp_con_public_key((const TCP_Client_Connection * _Nonnull)tcp_con->connection), CRYPTO_PUBLIC_KEY_SIZE); kill_tcp_connection(tcp_con->connection); tcp_con->connection = new_tcp_connection(tcp_c->logger, tcp_c->mem, tcp_c->mono_time, tcp_c->rng, tcp_c->ns, &ip_port, relay_pk, tcp_c->self_public_key, tcp_c->self_secret_key, &tcp_c->proxy_info, tcp_c->net_profile); @@ -957,8 +1001,12 @@ static int sleep_tcp_relay_connection(TCP_Connections *_Nonnull tcp_c, int tcp_c return -1; } - tcp_con->ip_port = tcp_con_ip_port(tcp_con->connection); - memcpy(tcp_con->relay_pk, tcp_con_public_key(tcp_con->connection), CRYPTO_PUBLIC_KEY_SIZE); + if (tcp_con->connection == nullptr) { + LOGGER_ERROR(tcp_c->logger, "TCP connection is null for tcp_con"); + return -1; + } + tcp_con->ip_port = tcp_con_ip_port((const TCP_Client_Connection * _Nonnull)tcp_con->connection); + memcpy(tcp_con->relay_pk, tcp_con_public_key((const TCP_Client_Connection * _Nonnull)tcp_con->connection), CRYPTO_PUBLIC_KEY_SIZE); kill_tcp_connection(tcp_con->connection); tcp_con->connection = nullptr; @@ -1032,7 +1080,11 @@ static int send_tcp_relay_routing_request(const TCP_Connections *_Nonnull tcp_c, return -1; } - if (send_routing_request(tcp_c->logger, tcp_con->connection, public_key) != 1) { + if (tcp_con->connection == nullptr) { + LOGGER_ERROR(tcp_c->logger, "TCP connection is null for tcp_con"); + return -1; + } + if (send_routing_request(tcp_c->logger, (TCP_Client_Connection * _Nonnull)tcp_con->connection, public_key) != 1) { return -1; } @@ -1067,7 +1119,11 @@ static int tcp_response_callback(void *_Nonnull object, uint8_t connection_id, c return -1; } - set_tcp_connection_number(tcp_con->connection, connection_id, connections_number); + if (tcp_con->connection == nullptr) { + LOGGER_ERROR(tcp_c->logger, "TCP connection is null for tcp_con"); + return -1; + } + set_tcp_connection_number((TCP_Client_Connection * _Nonnull)tcp_con->connection, connection_id, connections_number); return 0; } @@ -1134,7 +1190,7 @@ static int tcp_conn_data_callback(void *_Nonnull object, uint32_t number, uint8_ } if (tcp_c->tcp_data_callback != nullptr) { - tcp_c->tcp_data_callback(tcp_c->tcp_data_callback_object, con_to->id, data, length, userdata); + tcp_c->tcp_data_callback((void *_Nonnull)tcp_c->tcp_data_callback_object, con_to->id, data, length, userdata); } return 0; @@ -1167,7 +1223,7 @@ static int tcp_conn_oob_callback(void *_Nonnull object, const uint8_t *_Nonnull } if (tcp_c->tcp_oob_callback != nullptr) { - tcp_c->tcp_oob_callback(tcp_c->tcp_oob_callback_object, public_key, tcp_connections_number, data, length, userdata); + tcp_c->tcp_oob_callback((void *_Nonnull)tcp_c->tcp_oob_callback_object, public_key, tcp_connections_number, data, length, userdata); } return 0; @@ -1427,8 +1483,12 @@ static bool copy_tcp_relay_conn(const TCP_Connections *_Nonnull tcp_c, Node_form return false; } - memcpy(tcp_relay->public_key, tcp_con_public_key(tcp_con->connection), CRYPTO_PUBLIC_KEY_SIZE); - tcp_relay->ip_port = tcp_con_ip_port(tcp_con->connection); + if (tcp_con->connection == nullptr) { + LOGGER_ERROR(tcp_c->logger, "TCP connection is null for tcp_con"); + return false; + } + memcpy(tcp_relay->public_key, tcp_con_public_key((const TCP_Client_Connection * _Nonnull)tcp_con->connection), CRYPTO_PUBLIC_KEY_SIZE); + tcp_relay->ip_port = tcp_con_ip_port((const TCP_Client_Connection * _Nonnull)tcp_con->connection); Family *const family = &tcp_relay->ip_port.ip.family; @@ -1600,7 +1660,11 @@ static void do_tcp_conns(const Logger *_Nonnull logger, TCP_Connections *_Nonnul } if (tcp_con->status != TCP_CONN_SLEEPING) { - do_tcp_connection(logger, tcp_c->mono_time, tcp_con->connection, userdata); + if (tcp_con->connection == nullptr) { + LOGGER_ERROR(logger, "TCP connection is null for tcp_con"); + continue; + } + do_tcp_connection(logger, tcp_c->mono_time, (TCP_Client_Connection * _Nonnull)tcp_con->connection, userdata); /* callbacks can change TCP connection address. */ tcp_con = get_tcp_connection(tcp_c, i); @@ -1608,7 +1672,11 @@ static void do_tcp_conns(const Logger *_Nonnull logger, TCP_Connections *_Nonnul // Make sure the TCP connection wasn't dropped in any of the callbacks. assert(tcp_con != nullptr); - if (tcp_con_status(tcp_con->connection) == TCP_CLIENT_DISCONNECTED) { + if (tcp_con->connection == nullptr) { + LOGGER_ERROR(logger, "TCP connection is null for tcp_con"); + continue; + } + if (tcp_con_status((const TCP_Client_Connection * _Nonnull)tcp_con->connection) == TCP_CLIENT_DISCONNECTED) { if (tcp_con->status == TCP_CONN_CONNECTED) { reconnect_tcp_relay_connection(tcp_c, i); } else { @@ -1618,7 +1686,11 @@ static void do_tcp_conns(const Logger *_Nonnull logger, TCP_Connections *_Nonnul continue; } - if (tcp_con->status == TCP_CONN_VALID && tcp_con_status(tcp_con->connection) == TCP_CLIENT_CONFIRMED) { + if (tcp_con->connection == nullptr) { + LOGGER_ERROR(logger, "TCP connection is null for tcp_con"); + continue; + } + if (tcp_con->status == TCP_CONN_VALID && tcp_con_status((const TCP_Client_Connection * _Nonnull)tcp_con->connection) == TCP_CLIENT_CONFIRMED) { tcp_relay_on_online(tcp_c, i); } @@ -1646,7 +1718,7 @@ static void kill_nonused_tcp(TCP_Connections *_Nullable tcp_c) return; } - const uint32_t num_online = tcp_connected_relays_count(tcp_c); + const uint32_t num_online = tcp_connected_relays_count((const TCP_Connections * _Nonnull)tcp_c); if (num_online <= RECOMMENDED_FRIEND_TCP_CONNECTIONS) { return; @@ -1656,7 +1728,7 @@ static void kill_nonused_tcp(TCP_Connections *_Nullable tcp_c) uint32_t kill_count = 0; for (uint32_t i = 0; i < tcp_c->tcp_connections_length && kill_count < max_kill_count; ++i) { - const TCP_con *tcp_con = get_tcp_connection(tcp_c, i); + const TCP_con *tcp_con = get_tcp_connection((const TCP_Connections * _Nonnull)tcp_c, i); if (tcp_con == nullptr) { continue; @@ -1668,7 +1740,7 @@ static void kill_nonused_tcp(TCP_Connections *_Nullable tcp_c) } if (mono_time_is_timeout(tcp_c->mono_time, tcp_con->connected_time, TCP_CONNECTION_ANNOUNCE_TIMEOUT)) { - kill_tcp_relay_connection(tcp_c, i); + kill_tcp_relay_connection((TCP_Connections * _Nonnull)tcp_c, i); ++kill_count; } } diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c index cad12ee397..8963117fa4 100644 --- a/toxcore/TCP_server.c +++ b/toxcore/TCP_server.c @@ -64,18 +64,18 @@ typedef struct TCP_Secure_Connection { static const TCP_Secure_Connection empty_tcp_secure_connection = {{nullptr}}; struct TCP_Server { - const Logger *logger; - const Memory *mem; - const Random *rng; - const Network *ns; - Onion *onion; - Forwarding *forwarding; + const Logger *_Nonnull logger; + const Memory *_Nonnull mem; + const Random *_Nonnull rng; + const Network *_Nonnull ns; + Onion *_Nullable onion; + Forwarding *_Nullable forwarding; #ifdef TCP_SERVER_USE_EPOLL int efd; uint64_t last_run_pinged; #endif /* TCP_SERVER_USE_EPOLL */ - Socket *socks_listening; + Socket *_Nullable socks_listening; unsigned int num_listening_socks; uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; @@ -85,7 +85,7 @@ struct TCP_Server { TCP_Secure_Connection unconfirmed_connection_queue[MAX_INCOMING_CONNECTIONS]; uint16_t unconfirmed_connection_queue_index; - TCP_Secure_Connection *accepted_connection_array; + TCP_Secure_Connection *_Nullable accepted_connection_array; uint32_t size_accepted_connections; uint32_t num_accepted_connections; @@ -94,7 +94,7 @@ struct TCP_Server { BS_List accepted_key_list; /* Network profile for all TCP server packets. */ - Net_Profile *net_profile; + Net_Profile *_Nullable net_profile; }; static_assert(sizeof(TCP_Server) < 7 * 1024 * 1024, @@ -275,7 +275,7 @@ static void kill_tcp_secure_connection(TCP_Secure_Connection *_Nullable con) } kill_sock(con->con.ns, con->con.sock); - wipe_secure_connection(con); + wipe_secure_connection((TCP_Secure_Connection * _Nonnull)con); } static int rm_connection_index(TCP_Server *_Nonnull tcp_server, TCP_Secure_Connection *_Nonnull con, uint8_t con_number); @@ -749,7 +749,7 @@ static int handle_tcp_packet(TCP_Server *_Nonnull tcp_server, uint32_t con_id, c } const IP_Port source = con_id_to_ip_port(con_id, con->identifier); - onion_send_1(tcp_server->onion, data + 1 + CRYPTO_NONCE_SIZE, length - (1 + CRYPTO_NONCE_SIZE), &source, + onion_send_1((const Onion * _Nonnull)tcp_server->onion, data + 1 + CRYPTO_NONCE_SIZE, length - (1 + CRYPTO_NONCE_SIZE), &source, data + 1); } @@ -786,7 +786,7 @@ static int handle_tcp_packet(TCP_Server *_Nonnull tcp_server, uint32_t con_id, c return -1; } - send_forwarding(tcp_server->forwarding, &dest, sendback_data, sendback_data_len, forward_data, forward_data_len); + send_forwarding((const Forwarding * _Nonnull)tcp_server->forwarding, &dest, sendback_data, sendback_data_len, forward_data, forward_data_len); return 0; } @@ -1020,12 +1020,12 @@ TCP_Server *new_tcp_server(const Logger *logger, const Memory *mem, const Random if (onion != nullptr) { temp->onion = onion; - set_callback_handle_recv_1(onion, &handle_onion_recv_1, temp); + set_callback_handle_recv_1((Onion * _Nonnull)onion, &handle_onion_recv_1, temp); } if (forwarding != nullptr) { temp->forwarding = forwarding; - set_callback_forward_reply(forwarding, &handle_forward_reply_tcp, temp); + set_callback_forward_reply((Forwarding * _Nonnull)forwarding, &handle_forward_reply_tcp, temp); } memcpy(temp->secret_key, secret_key, CRYPTO_SECRET_KEY_SIZE); @@ -1381,11 +1381,11 @@ void kill_tcp_server(TCP_Server *tcp_server) } if (tcp_server->onion != nullptr) { - set_callback_handle_recv_1(tcp_server->onion, nullptr, nullptr); + set_callback_handle_recv_1((Onion * _Nonnull)tcp_server->onion, nullptr, nullptr); } if (tcp_server->forwarding != nullptr) { - set_callback_forward_reply(tcp_server->forwarding, nullptr, nullptr); + set_callback_forward_reply((Forwarding * _Nonnull)tcp_server->forwarding, nullptr, nullptr); } bs_list_free(&tcp_server->accepted_key_list); @@ -1399,7 +1399,7 @@ void kill_tcp_server(TCP_Server *tcp_server) wipe_secure_connection(&tcp_server->unconfirmed_connection_queue[i]); } - free_accepted_connection_array(tcp_server); + free_accepted_connection_array((TCP_Server * _Nonnull)tcp_server); crypto_memzero(tcp_server->secret_key, sizeof(tcp_server->secret_key)); diff --git a/toxcore/announce.c b/toxcore/announce.c index b8dfb56625..66a7b40a5f 100644 --- a/toxcore/announce.c +++ b/toxcore/announce.c @@ -51,22 +51,22 @@ uint8_t announce_response_of_request_type(uint8_t request_type) typedef struct Announce_Entry { uint64_t store_until; uint8_t data_public_key[CRYPTO_PUBLIC_KEY_SIZE]; - uint8_t *data; + uint8_t *_Nullable data; uint32_t length; } Announce_Entry; struct Announcements { - const Logger *log; - const Memory *mem; - const Random *rng; - Forwarding *forwarding; - const Mono_Time *mono_time; - DHT *dht; - Networking_Core *net; - const uint8_t *public_key; - const uint8_t *secret_key; - - Shared_Key_Cache *shared_keys; + const Logger *_Nonnull log; + const Memory *_Nonnull mem; + const Random *_Nonnull rng; + Forwarding *_Nonnull forwarding; + const Mono_Time *_Nonnull mono_time; + DHT *_Nonnull dht; + Networking_Core *_Nonnull net; + const uint8_t *_Nonnull public_key; + const uint8_t *_Nonnull secret_key; + + Shared_Key_Cache *_Nonnull shared_keys; uint8_t hmac_key[CRYPTO_HMAC_KEY_SIZE]; int32_t synch_offset; @@ -328,7 +328,11 @@ static int create_reply_plain_data_search_request(Announcements *_Nonnull announ } else { *p = 1; ++p; - crypto_sha256(p, stored->data, stored->length); + if (stored->data == nullptr) { + LOGGER_ERROR(announce->log, "Stored data is null for public key."); + return -1; + } + crypto_sha256(p, (const uint8_t *_Nonnull)stored->data, stored->length); p += CRYPTO_SHA256_SIZE; } @@ -462,7 +466,11 @@ static int create_reply_plain_store_announce_request(Announcements *_Nonnull ann } uint8_t stored_hash[CRYPTO_SHA256_SIZE]; - crypto_sha256(stored_hash, stored->data, stored->length); + if (stored->data == nullptr) { + LOGGER_ERROR(announce->log, "Stored data is null for public key."); + return -1; + } + crypto_sha256(stored_hash, (const uint8_t *_Nonnull)stored->data, stored->length); if (!crypto_sha256_eq(announcement, stored_hash)) { delete_entry(stored); @@ -635,11 +643,12 @@ Announcements *new_announcements(const Logger *log, const Memory *mem, const Ran announce->public_key = dht_get_self_public_key(announce->dht); announce->secret_key = dht_get_self_secret_key(announce->dht); new_hmac_key(announce->rng, announce->hmac_key); - announce->shared_keys = shared_key_cache_new(log, mono_time, mem, announce->secret_key, KEYS_TIMEOUT, MAX_KEYS_PER_SLOT); - if (announce->shared_keys == nullptr) { + Shared_Key_Cache *const shared_keys = shared_key_cache_new(log, mono_time, mem, announce->secret_key, KEYS_TIMEOUT, MAX_KEYS_PER_SLOT); + if (shared_keys == nullptr) { mem_delete(announce->mem, announce); return nullptr; } + announce->shared_keys = shared_keys; announce->start_time = mono_time_get(announce->mono_time); diff --git a/toxcore/bin_pack.c b/toxcore/bin_pack.c index 595bb0dcda..ab5df3d031 100644 --- a/toxcore/bin_pack.c +++ b/toxcore/bin_pack.c @@ -13,7 +13,7 @@ #include "logger.h" struct Bin_Pack { - uint8_t *bytes; + uint8_t *_Nullable bytes; uint32_t bytes_size; uint32_t bytes_pos; cmp_ctx_t ctx; diff --git a/toxcore/bin_unpack.c b/toxcore/bin_unpack.c index dfbb8cc7a3..883071e0d7 100644 --- a/toxcore/bin_unpack.c +++ b/toxcore/bin_unpack.c @@ -13,9 +13,9 @@ #include "mem.h" struct Bin_Unpack { - const Memory *mem; + const Memory *_Nonnull mem; - const uint8_t *bytes; + const uint8_t *_Nonnull bytes; uint32_t bytes_size; cmp_ctx_t ctx; }; diff --git a/toxcore/crypto_core.c b/toxcore/crypto_core.c index 9f140aca10..e2718f9ddb 100644 --- a/toxcore/crypto_core.c +++ b/toxcore/crypto_core.c @@ -102,8 +102,8 @@ static uint8_t *crypto_malloc(const Memory *_Nonnull mem, size_t bytes) static void crypto_free(const Memory *_Nonnull mem, uint8_t *_Nullable ptr, size_t bytes) { if (ptr != nullptr) { - crypto_memzero(ptr, bytes); - crypto_memunlock(ptr, bytes); + crypto_memzero((uint8_t *_Nonnull)ptr, bytes); + crypto_memunlock((uint8_t *_Nonnull)ptr, bytes); } mem_delete(mem, ptr); diff --git a/toxcore/crypto_core.h b/toxcore/crypto_core.h index 82b31e393d..d23f3294bd 100644 --- a/toxcore/crypto_core.h +++ b/toxcore/crypto_core.h @@ -106,7 +106,7 @@ typedef struct Random_Funcs { * CSPRNG and use `os_random` below. */ typedef struct Random { - const Random_Funcs *_Nullable funcs; + const Random_Funcs *_Nonnull funcs; void *_Nullable obj; } Random; diff --git a/toxcore/events/conference_connected.c b/toxcore/events/conference_connected.c index bf1f9e3ad5..194a6f3ea9 100644 --- a/toxcore/events/conference_connected.c +++ b/toxcore/events/conference_connected.c @@ -86,7 +86,7 @@ Tox_Event_Conference_Connected *tox_event_conference_connected_new(const Memory void tox_event_conference_connected_free(Tox_Event_Conference_Connected *conference_connected, const Memory *mem) { if (conference_connected != nullptr) { - tox_event_conference_connected_destruct(conference_connected, mem); + tox_event_conference_connected_destruct((Tox_Event_Conference_Connected * _Nonnull)conference_connected, mem); } mem_delete(mem, conference_connected); } diff --git a/toxcore/events/conference_invite.c b/toxcore/events/conference_invite.c index 01523f9667..841b110915 100644 --- a/toxcore/events/conference_invite.c +++ b/toxcore/events/conference_invite.c @@ -150,7 +150,7 @@ Tox_Event_Conference_Invite *tox_event_conference_invite_new(const Memory *mem) void tox_event_conference_invite_free(Tox_Event_Conference_Invite *conference_invite, const Memory *mem) { if (conference_invite != nullptr) { - tox_event_conference_invite_destruct(conference_invite, mem); + tox_event_conference_invite_destruct((Tox_Event_Conference_Invite * _Nonnull)conference_invite, mem); } mem_delete(mem, conference_invite); } diff --git a/toxcore/events/conference_message.c b/toxcore/events/conference_message.c index 3ee530e8ef..7c5dfe22d2 100644 --- a/toxcore/events/conference_message.c +++ b/toxcore/events/conference_message.c @@ -164,7 +164,7 @@ Tox_Event_Conference_Message *tox_event_conference_message_new(const Memory *mem void tox_event_conference_message_free(Tox_Event_Conference_Message *conference_message, const Memory *mem) { if (conference_message != nullptr) { - tox_event_conference_message_destruct(conference_message, mem); + tox_event_conference_message_destruct((Tox_Event_Conference_Message * _Nonnull)conference_message, mem); } mem_delete(mem, conference_message); } diff --git a/toxcore/events/conference_peer_list_changed.c b/toxcore/events/conference_peer_list_changed.c index d05dda8288..5486804e4e 100644 --- a/toxcore/events/conference_peer_list_changed.c +++ b/toxcore/events/conference_peer_list_changed.c @@ -86,7 +86,7 @@ Tox_Event_Conference_Peer_List_Changed *tox_event_conference_peer_list_changed_n void tox_event_conference_peer_list_changed_free(Tox_Event_Conference_Peer_List_Changed *conference_peer_list_changed, const Memory *mem) { if (conference_peer_list_changed != nullptr) { - tox_event_conference_peer_list_changed_destruct(conference_peer_list_changed, mem); + tox_event_conference_peer_list_changed_destruct((Tox_Event_Conference_Peer_List_Changed * _Nonnull)conference_peer_list_changed, mem); } mem_delete(mem, conference_peer_list_changed); } diff --git a/toxcore/events/conference_peer_name.c b/toxcore/events/conference_peer_name.c index 285d7a48bb..b23d25e2c4 100644 --- a/toxcore/events/conference_peer_name.c +++ b/toxcore/events/conference_peer_name.c @@ -148,7 +148,7 @@ Tox_Event_Conference_Peer_Name *tox_event_conference_peer_name_new(const Memory void tox_event_conference_peer_name_free(Tox_Event_Conference_Peer_Name *conference_peer_name, const Memory *mem) { if (conference_peer_name != nullptr) { - tox_event_conference_peer_name_destruct(conference_peer_name, mem); + tox_event_conference_peer_name_destruct((Tox_Event_Conference_Peer_Name * _Nonnull)conference_peer_name, mem); } mem_delete(mem, conference_peer_name); } diff --git a/toxcore/events/conference_title.c b/toxcore/events/conference_title.c index 7baeaee8ff..95ef1e3060 100644 --- a/toxcore/events/conference_title.c +++ b/toxcore/events/conference_title.c @@ -148,7 +148,7 @@ Tox_Event_Conference_Title *tox_event_conference_title_new(const Memory *mem) void tox_event_conference_title_free(Tox_Event_Conference_Title *conference_title, const Memory *mem) { if (conference_title != nullptr) { - tox_event_conference_title_destruct(conference_title, mem); + tox_event_conference_title_destruct((Tox_Event_Conference_Title * _Nonnull)conference_title, mem); } mem_delete(mem, conference_title); } diff --git a/toxcore/events/dht_nodes_response.c b/toxcore/events/dht_nodes_response.c index a4b9dc1520..1e0b29288e 100644 --- a/toxcore/events/dht_nodes_response.c +++ b/toxcore/events/dht_nodes_response.c @@ -133,7 +133,7 @@ Tox_Event_Dht_Nodes_Response *tox_event_dht_nodes_response_new(const Memory *mem void tox_event_dht_nodes_response_free(Tox_Event_Dht_Nodes_Response *dht_nodes_response, const Memory *mem) { if (dht_nodes_response != nullptr) { - tox_event_dht_nodes_response_destruct(dht_nodes_response, mem); + tox_event_dht_nodes_response_destruct((Tox_Event_Dht_Nodes_Response * _Nonnull)dht_nodes_response, mem); } mem_delete(mem, dht_nodes_response); } @@ -198,7 +198,9 @@ void tox_events_handle_dht_nodes_response( Tox *tox, const uint8_t public_key[TOX_PUBLIC_KEY_SIZE], const char *ip, uint32_t ip_length, uint16_t port, void *user_data) { - Tox_Event_Dht_Nodes_Response *dht_nodes_response = tox_event_dht_nodes_response_alloc(user_data); + void *_Nonnull object = (void *_Nonnull)user_data; + + Tox_Event_Dht_Nodes_Response *dht_nodes_response = tox_event_dht_nodes_response_alloc(object); if (dht_nodes_response == nullptr) { return; @@ -207,6 +209,6 @@ void tox_events_handle_dht_nodes_response( const Tox_System *sys = tox_get_system(tox); tox_event_dht_nodes_response_set_public_key(dht_nodes_response, public_key); - tox_event_dht_nodes_response_set_ip(dht_nodes_response, ip, ip_length, sys->mem); + tox_event_dht_nodes_response_set_ip(dht_nodes_response, ip, ip_length, (const Memory * _Nonnull)sys->mem); tox_event_dht_nodes_response_set_port(dht_nodes_response, port); } diff --git a/toxcore/events/file_chunk_request.c b/toxcore/events/file_chunk_request.c index 5562e5539e..c11ba44321 100644 --- a/toxcore/events/file_chunk_request.c +++ b/toxcore/events/file_chunk_request.c @@ -133,7 +133,7 @@ Tox_Event_File_Chunk_Request *tox_event_file_chunk_request_new(const Memory *mem void tox_event_file_chunk_request_free(Tox_Event_File_Chunk_Request *file_chunk_request, const Memory *mem) { if (file_chunk_request != nullptr) { - tox_event_file_chunk_request_destruct(file_chunk_request, mem); + tox_event_file_chunk_request_destruct((Tox_Event_File_Chunk_Request * _Nonnull)file_chunk_request, mem); } mem_delete(mem, file_chunk_request); } diff --git a/toxcore/events/file_recv.c b/toxcore/events/file_recv.c index 00b015da01..01919bc686 100644 --- a/toxcore/events/file_recv.c +++ b/toxcore/events/file_recv.c @@ -176,7 +176,7 @@ Tox_Event_File_Recv *tox_event_file_recv_new(const Memory *mem) void tox_event_file_recv_free(Tox_Event_File_Recv *file_recv, const Memory *mem) { if (file_recv != nullptr) { - tox_event_file_recv_destruct(file_recv, mem); + tox_event_file_recv_destruct((Tox_Event_File_Recv * _Nonnull)file_recv, mem); } mem_delete(mem, file_recv); } diff --git a/toxcore/events/file_recv_chunk.c b/toxcore/events/file_recv_chunk.c index 09a1eda8cb..93ce3d4e34 100644 --- a/toxcore/events/file_recv_chunk.c +++ b/toxcore/events/file_recv_chunk.c @@ -162,7 +162,7 @@ Tox_Event_File_Recv_Chunk *tox_event_file_recv_chunk_new(const Memory *mem) void tox_event_file_recv_chunk_free(Tox_Event_File_Recv_Chunk *file_recv_chunk, const Memory *mem) { if (file_recv_chunk != nullptr) { - tox_event_file_recv_chunk_destruct(file_recv_chunk, mem); + tox_event_file_recv_chunk_destruct((Tox_Event_File_Recv_Chunk * _Nonnull)file_recv_chunk, mem); } mem_delete(mem, file_recv_chunk); } diff --git a/toxcore/events/file_recv_control.c b/toxcore/events/file_recv_control.c index e5a650e198..dd146bea10 100644 --- a/toxcore/events/file_recv_control.c +++ b/toxcore/events/file_recv_control.c @@ -121,7 +121,7 @@ Tox_Event_File_Recv_Control *tox_event_file_recv_control_new(const Memory *mem) void tox_event_file_recv_control_free(Tox_Event_File_Recv_Control *file_recv_control, const Memory *mem) { if (file_recv_control != nullptr) { - tox_event_file_recv_control_destruct(file_recv_control, mem); + tox_event_file_recv_control_destruct((Tox_Event_File_Recv_Control * _Nonnull)file_recv_control, mem); } mem_delete(mem, file_recv_control); } diff --git a/toxcore/events/friend_connection_status.c b/toxcore/events/friend_connection_status.c index 64b04ab45d..7248ac43ad 100644 --- a/toxcore/events/friend_connection_status.c +++ b/toxcore/events/friend_connection_status.c @@ -107,7 +107,7 @@ Tox_Event_Friend_Connection_Status *tox_event_friend_connection_status_new(const void tox_event_friend_connection_status_free(Tox_Event_Friend_Connection_Status *friend_connection_status, const Memory *mem) { if (friend_connection_status != nullptr) { - tox_event_friend_connection_status_destruct(friend_connection_status, mem); + tox_event_friend_connection_status_destruct((Tox_Event_Friend_Connection_Status * _Nonnull)friend_connection_status, mem); } mem_delete(mem, friend_connection_status); } diff --git a/toxcore/events/friend_lossless_packet.c b/toxcore/events/friend_lossless_packet.c index 7b9e6e9183..6a1f6fac86 100644 --- a/toxcore/events/friend_lossless_packet.c +++ b/toxcore/events/friend_lossless_packet.c @@ -134,7 +134,7 @@ Tox_Event_Friend_Lossless_Packet *tox_event_friend_lossless_packet_new(const Mem void tox_event_friend_lossless_packet_free(Tox_Event_Friend_Lossless_Packet *friend_lossless_packet, const Memory *mem) { if (friend_lossless_packet != nullptr) { - tox_event_friend_lossless_packet_destruct(friend_lossless_packet, mem); + tox_event_friend_lossless_packet_destruct((Tox_Event_Friend_Lossless_Packet * _Nonnull)friend_lossless_packet, mem); } mem_delete(mem, friend_lossless_packet); } diff --git a/toxcore/events/friend_lossy_packet.c b/toxcore/events/friend_lossy_packet.c index 22096d4bdc..def938af48 100644 --- a/toxcore/events/friend_lossy_packet.c +++ b/toxcore/events/friend_lossy_packet.c @@ -134,7 +134,7 @@ Tox_Event_Friend_Lossy_Packet *tox_event_friend_lossy_packet_new(const Memory *m void tox_event_friend_lossy_packet_free(Tox_Event_Friend_Lossy_Packet *friend_lossy_packet, const Memory *mem) { if (friend_lossy_packet != nullptr) { - tox_event_friend_lossy_packet_destruct(friend_lossy_packet, mem); + tox_event_friend_lossy_packet_destruct((Tox_Event_Friend_Lossy_Packet * _Nonnull)friend_lossy_packet, mem); } mem_delete(mem, friend_lossy_packet); } diff --git a/toxcore/events/friend_message.c b/toxcore/events/friend_message.c index 0d59a6bc07..64bc974305 100644 --- a/toxcore/events/friend_message.c +++ b/toxcore/events/friend_message.c @@ -150,7 +150,7 @@ Tox_Event_Friend_Message *tox_event_friend_message_new(const Memory *mem) void tox_event_friend_message_free(Tox_Event_Friend_Message *friend_message, const Memory *mem) { if (friend_message != nullptr) { - tox_event_friend_message_destruct(friend_message, mem); + tox_event_friend_message_destruct((Tox_Event_Friend_Message * _Nonnull)friend_message, mem); } mem_delete(mem, friend_message); } diff --git a/toxcore/events/friend_name.c b/toxcore/events/friend_name.c index 4f1c6e2285..1dcb01d1a0 100644 --- a/toxcore/events/friend_name.c +++ b/toxcore/events/friend_name.c @@ -134,7 +134,7 @@ Tox_Event_Friend_Name *tox_event_friend_name_new(const Memory *mem) void tox_event_friend_name_free(Tox_Event_Friend_Name *friend_name, const Memory *mem) { if (friend_name != nullptr) { - tox_event_friend_name_destruct(friend_name, mem); + tox_event_friend_name_destruct((Tox_Event_Friend_Name * _Nonnull)friend_name, mem); } mem_delete(mem, friend_name); } diff --git a/toxcore/events/friend_read_receipt.c b/toxcore/events/friend_read_receipt.c index b4443bd389..fd45c89c5f 100644 --- a/toxcore/events/friend_read_receipt.c +++ b/toxcore/events/friend_read_receipt.c @@ -105,7 +105,7 @@ Tox_Event_Friend_Read_Receipt *tox_event_friend_read_receipt_new(const Memory *m void tox_event_friend_read_receipt_free(Tox_Event_Friend_Read_Receipt *friend_read_receipt, const Memory *mem) { if (friend_read_receipt != nullptr) { - tox_event_friend_read_receipt_destruct(friend_read_receipt, mem); + tox_event_friend_read_receipt_destruct((Tox_Event_Friend_Read_Receipt * _Nonnull)friend_read_receipt, mem); } mem_delete(mem, friend_read_receipt); } diff --git a/toxcore/events/friend_request.c b/toxcore/events/friend_request.c index b331d11ef7..5c53923a6b 100644 --- a/toxcore/events/friend_request.c +++ b/toxcore/events/friend_request.c @@ -128,7 +128,7 @@ Tox_Event_Friend_Request *tox_event_friend_request_new(const Memory *mem) void tox_event_friend_request_free(Tox_Event_Friend_Request *friend_request, const Memory *mem) { if (friend_request != nullptr) { - tox_event_friend_request_destruct(friend_request, mem); + tox_event_friend_request_destruct((Tox_Event_Friend_Request * _Nonnull)friend_request, mem); } mem_delete(mem, friend_request); } @@ -203,5 +203,5 @@ void tox_events_handle_friend_request(Tox *tox, const uint8_t *public_key, const const Tox_System *sys = tox_get_system(tox); tox_event_friend_request_set_public_key(friend_request, public_key); - tox_event_friend_request_set_message(friend_request, message, length, sys->mem); + tox_event_friend_request_set_message(friend_request, message, length, (const Memory * _Nonnull)sys->mem); } diff --git a/toxcore/events/friend_status.c b/toxcore/events/friend_status.c index 99d39bc877..1eb95a762e 100644 --- a/toxcore/events/friend_status.c +++ b/toxcore/events/friend_status.c @@ -107,7 +107,7 @@ Tox_Event_Friend_Status *tox_event_friend_status_new(const Memory *mem) void tox_event_friend_status_free(Tox_Event_Friend_Status *friend_status, const Memory *mem) { if (friend_status != nullptr) { - tox_event_friend_status_destruct(friend_status, mem); + tox_event_friend_status_destruct((Tox_Event_Friend_Status * _Nonnull)friend_status, mem); } mem_delete(mem, friend_status); } diff --git a/toxcore/events/friend_status_message.c b/toxcore/events/friend_status_message.c index c2e77d9cdd..b1e064b3d3 100644 --- a/toxcore/events/friend_status_message.c +++ b/toxcore/events/friend_status_message.c @@ -134,7 +134,7 @@ Tox_Event_Friend_Status_Message *tox_event_friend_status_message_new(const Memor void tox_event_friend_status_message_free(Tox_Event_Friend_Status_Message *friend_status_message, const Memory *mem) { if (friend_status_message != nullptr) { - tox_event_friend_status_message_destruct(friend_status_message, mem); + tox_event_friend_status_message_destruct((Tox_Event_Friend_Status_Message * _Nonnull)friend_status_message, mem); } mem_delete(mem, friend_status_message); } diff --git a/toxcore/events/friend_typing.c b/toxcore/events/friend_typing.c index fda20baf99..cb651eb76f 100644 --- a/toxcore/events/friend_typing.c +++ b/toxcore/events/friend_typing.c @@ -105,7 +105,7 @@ Tox_Event_Friend_Typing *tox_event_friend_typing_new(const Memory *mem) void tox_event_friend_typing_free(Tox_Event_Friend_Typing *friend_typing, const Memory *mem) { if (friend_typing != nullptr) { - tox_event_friend_typing_destruct(friend_typing, mem); + tox_event_friend_typing_destruct((Tox_Event_Friend_Typing * _Nonnull)friend_typing, mem); } mem_delete(mem, friend_typing); } diff --git a/toxcore/events/group_custom_packet.c b/toxcore/events/group_custom_packet.c index 65af4d489a..f411894404 100644 --- a/toxcore/events/group_custom_packet.c +++ b/toxcore/events/group_custom_packet.c @@ -148,7 +148,7 @@ Tox_Event_Group_Custom_Packet *tox_event_group_custom_packet_new(const Memory *m void tox_event_group_custom_packet_free(Tox_Event_Group_Custom_Packet *group_custom_packet, const Memory *mem) { if (group_custom_packet != nullptr) { - tox_event_group_custom_packet_destruct(group_custom_packet, mem); + tox_event_group_custom_packet_destruct((Tox_Event_Group_Custom_Packet * _Nonnull)group_custom_packet, mem); } mem_delete(mem, group_custom_packet); } diff --git a/toxcore/events/group_custom_private_packet.c b/toxcore/events/group_custom_private_packet.c index eb58100888..75d1bf33ee 100644 --- a/toxcore/events/group_custom_private_packet.c +++ b/toxcore/events/group_custom_private_packet.c @@ -148,7 +148,7 @@ Tox_Event_Group_Custom_Private_Packet *tox_event_group_custom_private_packet_new void tox_event_group_custom_private_packet_free(Tox_Event_Group_Custom_Private_Packet *group_custom_private_packet, const Memory *mem) { if (group_custom_private_packet != nullptr) { - tox_event_group_custom_private_packet_destruct(group_custom_private_packet, mem); + tox_event_group_custom_private_packet_destruct((Tox_Event_Group_Custom_Private_Packet * _Nonnull)group_custom_private_packet, mem); } mem_delete(mem, group_custom_private_packet); } diff --git a/toxcore/events/group_invite.c b/toxcore/events/group_invite.c index 47959e5de8..5d30a339e4 100644 --- a/toxcore/events/group_invite.c +++ b/toxcore/events/group_invite.c @@ -176,7 +176,7 @@ Tox_Event_Group_Invite *tox_event_group_invite_new(const Memory *mem) void tox_event_group_invite_free(Tox_Event_Group_Invite *group_invite, const Memory *mem) { if (group_invite != nullptr) { - tox_event_group_invite_destruct(group_invite, mem); + tox_event_group_invite_destruct((Tox_Event_Group_Invite * _Nonnull)group_invite, mem); } mem_delete(mem, group_invite); } diff --git a/toxcore/events/group_join_fail.c b/toxcore/events/group_join_fail.c index 8931f7b748..815ef2f30c 100644 --- a/toxcore/events/group_join_fail.c +++ b/toxcore/events/group_join_fail.c @@ -107,7 +107,7 @@ Tox_Event_Group_Join_Fail *tox_event_group_join_fail_new(const Memory *mem) void tox_event_group_join_fail_free(Tox_Event_Group_Join_Fail *group_join_fail, const Memory *mem) { if (group_join_fail != nullptr) { - tox_event_group_join_fail_destruct(group_join_fail, mem); + tox_event_group_join_fail_destruct((Tox_Event_Group_Join_Fail * _Nonnull)group_join_fail, mem); } mem_delete(mem, group_join_fail); } diff --git a/toxcore/events/group_message.c b/toxcore/events/group_message.c index b8da841f5f..60ee216956 100644 --- a/toxcore/events/group_message.c +++ b/toxcore/events/group_message.c @@ -178,7 +178,7 @@ Tox_Event_Group_Message *tox_event_group_message_new(const Memory *mem) void tox_event_group_message_free(Tox_Event_Group_Message *group_message, const Memory *mem) { if (group_message != nullptr) { - tox_event_group_message_destruct(group_message, mem); + tox_event_group_message_destruct((Tox_Event_Group_Message * _Nonnull)group_message, mem); } mem_delete(mem, group_message); } diff --git a/toxcore/events/group_moderation.c b/toxcore/events/group_moderation.c index d6224796ba..c438ef4fe6 100644 --- a/toxcore/events/group_moderation.c +++ b/toxcore/events/group_moderation.c @@ -135,7 +135,7 @@ Tox_Event_Group_Moderation *tox_event_group_moderation_new(const Memory *mem) void tox_event_group_moderation_free(Tox_Event_Group_Moderation *group_moderation, const Memory *mem) { if (group_moderation != nullptr) { - tox_event_group_moderation_destruct(group_moderation, mem); + tox_event_group_moderation_destruct((Tox_Event_Group_Moderation * _Nonnull)group_moderation, mem); } mem_delete(mem, group_moderation); } diff --git a/toxcore/events/group_password.c b/toxcore/events/group_password.c index 5c692339ef..0208a63e48 100644 --- a/toxcore/events/group_password.c +++ b/toxcore/events/group_password.c @@ -134,7 +134,7 @@ Tox_Event_Group_Password *tox_event_group_password_new(const Memory *mem) void tox_event_group_password_free(Tox_Event_Group_Password *group_password, const Memory *mem) { if (group_password != nullptr) { - tox_event_group_password_destruct(group_password, mem); + tox_event_group_password_destruct((Tox_Event_Group_Password * _Nonnull)group_password, mem); } mem_delete(mem, group_password); } diff --git a/toxcore/events/group_peer_exit.c b/toxcore/events/group_peer_exit.c index 764426660e..9df0183534 100644 --- a/toxcore/events/group_peer_exit.c +++ b/toxcore/events/group_peer_exit.c @@ -206,7 +206,7 @@ Tox_Event_Group_Peer_Exit *tox_event_group_peer_exit_new(const Memory *mem) void tox_event_group_peer_exit_free(Tox_Event_Group_Peer_Exit *group_peer_exit, const Memory *mem) { if (group_peer_exit != nullptr) { - tox_event_group_peer_exit_destruct(group_peer_exit, mem); + tox_event_group_peer_exit_destruct((Tox_Event_Group_Peer_Exit * _Nonnull)group_peer_exit, mem); } mem_delete(mem, group_peer_exit); } diff --git a/toxcore/events/group_peer_join.c b/toxcore/events/group_peer_join.c index 4fd13f9af2..15dcab659e 100644 --- a/toxcore/events/group_peer_join.c +++ b/toxcore/events/group_peer_join.c @@ -105,7 +105,7 @@ Tox_Event_Group_Peer_Join *tox_event_group_peer_join_new(const Memory *mem) void tox_event_group_peer_join_free(Tox_Event_Group_Peer_Join *group_peer_join, const Memory *mem) { if (group_peer_join != nullptr) { - tox_event_group_peer_join_destruct(group_peer_join, mem); + tox_event_group_peer_join_destruct((Tox_Event_Group_Peer_Join * _Nonnull)group_peer_join, mem); } mem_delete(mem, group_peer_join); } diff --git a/toxcore/events/group_peer_limit.c b/toxcore/events/group_peer_limit.c index e652f7a7c4..1410a6d6eb 100644 --- a/toxcore/events/group_peer_limit.c +++ b/toxcore/events/group_peer_limit.c @@ -105,7 +105,7 @@ Tox_Event_Group_Peer_Limit *tox_event_group_peer_limit_new(const Memory *mem) void tox_event_group_peer_limit_free(Tox_Event_Group_Peer_Limit *group_peer_limit, const Memory *mem) { if (group_peer_limit != nullptr) { - tox_event_group_peer_limit_destruct(group_peer_limit, mem); + tox_event_group_peer_limit_destruct((Tox_Event_Group_Peer_Limit * _Nonnull)group_peer_limit, mem); } mem_delete(mem, group_peer_limit); } diff --git a/toxcore/events/group_peer_name.c b/toxcore/events/group_peer_name.c index 6009fd7511..7f59fd94a2 100644 --- a/toxcore/events/group_peer_name.c +++ b/toxcore/events/group_peer_name.c @@ -148,7 +148,7 @@ Tox_Event_Group_Peer_Name *tox_event_group_peer_name_new(const Memory *mem) void tox_event_group_peer_name_free(Tox_Event_Group_Peer_Name *group_peer_name, const Memory *mem) { if (group_peer_name != nullptr) { - tox_event_group_peer_name_destruct(group_peer_name, mem); + tox_event_group_peer_name_destruct((Tox_Event_Group_Peer_Name * _Nonnull)group_peer_name, mem); } mem_delete(mem, group_peer_name); } diff --git a/toxcore/events/group_peer_status.c b/toxcore/events/group_peer_status.c index f193e2b767..569f5e3c4c 100644 --- a/toxcore/events/group_peer_status.c +++ b/toxcore/events/group_peer_status.c @@ -121,7 +121,7 @@ Tox_Event_Group_Peer_Status *tox_event_group_peer_status_new(const Memory *mem) void tox_event_group_peer_status_free(Tox_Event_Group_Peer_Status *group_peer_status, const Memory *mem) { if (group_peer_status != nullptr) { - tox_event_group_peer_status_destruct(group_peer_status, mem); + tox_event_group_peer_status_destruct((Tox_Event_Group_Peer_Status * _Nonnull)group_peer_status, mem); } mem_delete(mem, group_peer_status); } diff --git a/toxcore/events/group_privacy_state.c b/toxcore/events/group_privacy_state.c index 9d50e5052c..1351a0a64f 100644 --- a/toxcore/events/group_privacy_state.c +++ b/toxcore/events/group_privacy_state.c @@ -107,7 +107,7 @@ Tox_Event_Group_Privacy_State *tox_event_group_privacy_state_new(const Memory *m void tox_event_group_privacy_state_free(Tox_Event_Group_Privacy_State *group_privacy_state, const Memory *mem) { if (group_privacy_state != nullptr) { - tox_event_group_privacy_state_destruct(group_privacy_state, mem); + tox_event_group_privacy_state_destruct((Tox_Event_Group_Privacy_State * _Nonnull)group_privacy_state, mem); } mem_delete(mem, group_privacy_state); } diff --git a/toxcore/events/group_private_message.c b/toxcore/events/group_private_message.c index d36e4c01d7..d1a842c6fb 100644 --- a/toxcore/events/group_private_message.c +++ b/toxcore/events/group_private_message.c @@ -178,7 +178,7 @@ Tox_Event_Group_Private_Message *tox_event_group_private_message_new(const Memor void tox_event_group_private_message_free(Tox_Event_Group_Private_Message *group_private_message, const Memory *mem) { if (group_private_message != nullptr) { - tox_event_group_private_message_destruct(group_private_message, mem); + tox_event_group_private_message_destruct((Tox_Event_Group_Private_Message * _Nonnull)group_private_message, mem); } mem_delete(mem, group_private_message); } diff --git a/toxcore/events/group_self_join.c b/toxcore/events/group_self_join.c index 1fc7b96d9f..7bc37dadb9 100644 --- a/toxcore/events/group_self_join.c +++ b/toxcore/events/group_self_join.c @@ -86,7 +86,7 @@ Tox_Event_Group_Self_Join *tox_event_group_self_join_new(const Memory *mem) void tox_event_group_self_join_free(Tox_Event_Group_Self_Join *group_self_join, const Memory *mem) { if (group_self_join != nullptr) { - tox_event_group_self_join_destruct(group_self_join, mem); + tox_event_group_self_join_destruct((Tox_Event_Group_Self_Join * _Nonnull)group_self_join, mem); } mem_delete(mem, group_self_join); } diff --git a/toxcore/events/group_topic.c b/toxcore/events/group_topic.c index 396a423e94..e5fe463935 100644 --- a/toxcore/events/group_topic.c +++ b/toxcore/events/group_topic.c @@ -148,7 +148,7 @@ Tox_Event_Group_Topic *tox_event_group_topic_new(const Memory *mem) void tox_event_group_topic_free(Tox_Event_Group_Topic *group_topic, const Memory *mem) { if (group_topic != nullptr) { - tox_event_group_topic_destruct(group_topic, mem); + tox_event_group_topic_destruct((Tox_Event_Group_Topic * _Nonnull)group_topic, mem); } mem_delete(mem, group_topic); } diff --git a/toxcore/events/group_topic_lock.c b/toxcore/events/group_topic_lock.c index 4c00031799..b398faec9c 100644 --- a/toxcore/events/group_topic_lock.c +++ b/toxcore/events/group_topic_lock.c @@ -107,7 +107,7 @@ Tox_Event_Group_Topic_Lock *tox_event_group_topic_lock_new(const Memory *mem) void tox_event_group_topic_lock_free(Tox_Event_Group_Topic_Lock *group_topic_lock, const Memory *mem) { if (group_topic_lock != nullptr) { - tox_event_group_topic_lock_destruct(group_topic_lock, mem); + tox_event_group_topic_lock_destruct((Tox_Event_Group_Topic_Lock * _Nonnull)group_topic_lock, mem); } mem_delete(mem, group_topic_lock); } diff --git a/toxcore/events/group_voice_state.c b/toxcore/events/group_voice_state.c index 98602549fa..6a79c62e86 100644 --- a/toxcore/events/group_voice_state.c +++ b/toxcore/events/group_voice_state.c @@ -107,7 +107,7 @@ Tox_Event_Group_Voice_State *tox_event_group_voice_state_new(const Memory *mem) void tox_event_group_voice_state_free(Tox_Event_Group_Voice_State *group_voice_state, const Memory *mem) { if (group_voice_state != nullptr) { - tox_event_group_voice_state_destruct(group_voice_state, mem); + tox_event_group_voice_state_destruct((Tox_Event_Group_Voice_State * _Nonnull)group_voice_state, mem); } mem_delete(mem, group_voice_state); } diff --git a/toxcore/events/self_connection_status.c b/toxcore/events/self_connection_status.c index 223aca58ba..95e8426814 100644 --- a/toxcore/events/self_connection_status.c +++ b/toxcore/events/self_connection_status.c @@ -88,7 +88,7 @@ Tox_Event_Self_Connection_Status *tox_event_self_connection_status_new(const Mem void tox_event_self_connection_status_free(Tox_Event_Self_Connection_Status *self_connection_status, const Memory *mem) { if (self_connection_status != nullptr) { - tox_event_self_connection_status_destruct(self_connection_status, mem); + tox_event_self_connection_status_destruct((Tox_Event_Self_Connection_Status * _Nonnull)self_connection_status, mem); } mem_delete(mem, self_connection_status); } diff --git a/toxcore/forwarding.c b/toxcore/forwarding.c index 4c7cf91172..8a0a53a93b 100644 --- a/toxcore/forwarding.c +++ b/toxcore/forwarding.c @@ -18,12 +18,12 @@ #include "timed_auth.h" struct Forwarding { - const Logger *log; - const Memory *mem; - const Random *rng; - DHT *dht; - const Mono_Time *mono_time; - Networking_Core *net; + const Logger *_Nonnull log; + const Memory *_Nonnull mem; + const Random *_Nonnull rng; + DHT *_Nonnull dht; + const Mono_Time *_Nonnull mono_time; + Networking_Core *_Nonnull net; uint8_t hmac_key[CRYPTO_HMAC_KEY_SIZE]; diff --git a/toxcore/friend_connection.c b/toxcore/friend_connection.c index e3464e5e6d..6c85687a57 100644 --- a/toxcore/friend_connection.c +++ b/toxcore/friend_connection.c @@ -29,11 +29,11 @@ #define PORTS_PER_DISCOVERY 10 typedef struct Friend_Conn_Callbacks { - fc_status_cb *status_callback; - fc_data_cb *data_callback; - fc_lossy_data_cb *lossy_data_callback; + fc_status_cb *_Nullable status_callback; + fc_data_cb *_Nullable data_callback; + fc_lossy_data_cb *_Nullable lossy_data_callback; - void *callback_object; + void *_Nullable callback_object; int callback_id; } Friend_Conn_Callbacks; @@ -68,22 +68,22 @@ struct Friend_Conn { static const Friend_Conn empty_friend_conn = {0}; struct Friend_Connections { - const Mono_Time *mono_time; - const Memory *mem; - const Logger *logger; - Net_Crypto *net_crypto; - DHT *dht; - Broadcast_Info *broadcast; - Onion_Client *onion_c; - - Friend_Conn *conns; + const Mono_Time *_Nonnull mono_time; + const Memory *_Nonnull mem; + const Logger *_Nonnull logger; + Net_Crypto *_Nonnull net_crypto; + DHT *_Nonnull dht; + Broadcast_Info *_Nullable broadcast; + Onion_Client *_Nonnull onion_c; + + Friend_Conn *_Nullable conns; uint32_t num_cons; - fr_request_cb *fr_request_callback; - void *fr_request_object; + fr_request_cb *_Nullable fr_request_callback; + void *_Nullable fr_request_object; - global_status_cb *global_status_callback; - void *global_status_callback_object; + global_status_cb *_Nullable global_status_callback; + void *_Nullable global_status_callback_object; uint64_t last_lan_discovery; uint16_t next_lan_port; @@ -386,7 +386,7 @@ static void change_dht_pk(Friend_Connections *_Nonnull fr_c, int friendcon_id, c memcpy(friend_con->dht_temp_pk, dht_public_key, CRYPTO_PUBLIC_KEY_SIZE); } -static int handle_status(void *_Nonnull object, int id, bool status, void *_Nonnull userdata) +static int handle_status(void *_Nonnull object, int id, bool status, void *_Nullable userdata) { Friend_Connections *const fr_c = (Friend_Connections *)object; Friend_Conn *const friend_con = get_conn(fr_c, id); @@ -433,7 +433,7 @@ static int handle_status(void *_Nonnull object, int id, bool status, void *_Nonn } /** Callback for dht public key changes. */ -static void dht_pk_callback(void *_Nonnull object, int32_t number, const uint8_t *_Nonnull dht_public_key, void *_Nonnull userdata) +static void dht_pk_callback(void *_Nonnull object, int32_t number, const uint8_t *_Nonnull dht_public_key, void *_Nullable userdata) { Friend_Connections *const fr_c = (Friend_Connections *)object; Friend_Conn *const friend_con = get_conn(fr_c, number); @@ -475,7 +475,7 @@ static int handle_packet(void *_Nonnull object, int id, const uint8_t *_Nonnull if (data[0] == PACKET_ID_FRIEND_REQUESTS) { if (fr_c->fr_request_callback != nullptr) { - fr_c->fr_request_callback(fr_c->fr_request_object, friend_con->real_public_key, data, length, userdata); + fr_c->fr_request_callback((void *_Nonnull)fr_c->fr_request_object, friend_con->real_public_key, data, length, userdata); } return 0; @@ -937,17 +937,20 @@ Friend_Connections *new_friend_connections( static void lan_discovery(Friend_Connections *_Nonnull fr_c) { if (fr_c->last_lan_discovery + LAN_DISCOVERY_INTERVAL < mono_time_get(fr_c->mono_time)) { + if (fr_c->broadcast == nullptr) { + return; + } const uint16_t first = fr_c->next_lan_port; uint16_t last = first + PORTS_PER_DISCOVERY; last = last > TOX_PORTRANGE_TO ? TOX_PORTRANGE_TO : last; // Always send to default port - lan_discovery_send(dht_get_net(fr_c->dht), fr_c->broadcast, dht_get_self_public_key(fr_c->dht), + lan_discovery_send(dht_get_net(fr_c->dht), (const Broadcast_Info * _Nonnull)fr_c->broadcast, dht_get_self_public_key(fr_c->dht), net_htons(TOX_PORT_DEFAULT)); // And check some extra ports for (uint16_t port = first; port < last; ++port) { - lan_discovery_send(dht_get_net(fr_c->dht), fr_c->broadcast, dht_get_self_public_key(fr_c->dht), net_htons(port)); + lan_discovery_send(dht_get_net(fr_c->dht), (const Broadcast_Info * _Nonnull)fr_c->broadcast, dht_get_self_public_key(fr_c->dht), net_htons(port)); } // Don't include default port in port range @@ -1016,7 +1019,7 @@ void kill_friend_connections(Friend_Connections *fr_c) } for (uint32_t i = 0; i < fr_c->num_cons; ++i) { - kill_friend_connection(fr_c, i); + kill_friend_connection((Friend_Connections * _Nonnull)fr_c, i); } // there might be allocated NONE connections diff --git a/toxcore/friend_connection.h b/toxcore/friend_connection.h index e4b6baa69c..ae304aa595 100644 --- a/toxcore/friend_connection.h +++ b/toxcore/friend_connection.h @@ -55,7 +55,8 @@ typedef struct Friend_Connections Friend_Connections; Net_Crypto *_Nonnull friendconn_net_crypto(const Friend_Connections *_Nonnull fr_c); -/** @return friendcon_id corresponding to the real public key on success. +/** + * @return friendcon_id corresponding to the real public key on success. * @retval -1 on failure. */ int getfriend_conn_id_pk(const Friend_Connections *_Nonnull fr_c, const uint8_t *_Nonnull real_pk); @@ -81,7 +82,7 @@ unsigned int friend_con_connected(const Friend_Connections *_Nonnull fr_c, int f */ int get_friendcon_public_keys(uint8_t *_Nullable real_pk, uint8_t *_Nullable dht_temp_pk, const Friend_Connections *_Nonnull fr_c, int friendcon_id); /** Set temp dht key for connection. */ -void set_dht_temp_pk(Friend_Connections *_Nonnull fr_c, int friendcon_id, const uint8_t *_Nonnull dht_temp_pk, void *_Nonnull userdata); +void set_dht_temp_pk(Friend_Connections *_Nonnull fr_c, int friendcon_id, const uint8_t *_Nonnull dht_temp_pk, void *_Nullable userdata); typedef int global_status_cb(void *_Nullable object, int friendcon_id, bool status, void *_Nullable userdata); @@ -140,14 +141,14 @@ typedef int fr_request_cb( * * This function will be called every time a friend request packet is received. */ -void set_friend_request_callback(Friend_Connections *_Nonnull fr_c, fr_request_cb *_Nonnull fr_request_callback, void *_Nonnull object); +void set_friend_request_callback(Friend_Connections *_Nonnull fr_c, fr_request_cb *_Nullable fr_request_callback, void *_Nullable object); /** Create new friend_connections instance. */ Friend_Connections *_Nullable new_friend_connections(const Logger *_Nonnull logger, const Memory *_Nonnull mem, const Mono_Time *_Nonnull mono_time, const Network *_Nonnull ns, Onion_Client *_Nonnull onion_c, bool local_discovery_enabled); /** main friend_connections loop. */ -void do_friend_connections(Friend_Connections *_Nonnull fr_c, void *_Nonnull userdata); +void do_friend_connections(Friend_Connections *_Nonnull fr_c, void *_Nullable userdata); /** Free everything related with friend_connections. */ void kill_friend_connections(Friend_Connections *_Nullable fr_c); diff --git a/toxcore/friend_requests.c b/toxcore/friend_requests.c index 00373274cc..aa75d07a21 100644 --- a/toxcore/friend_requests.c +++ b/toxcore/friend_requests.c @@ -36,15 +36,15 @@ struct Received_Requests { }; struct Friend_Requests { - const Memory *mem; + const Memory *_Nonnull mem; uint32_t nospam; - fr_friend_request_cb *handle_friendrequest; + fr_friend_request_cb *_Nullable handle_friendrequest; uint8_t handle_friendrequest_isset; - void *handle_friendrequest_object; + void *_Nullable handle_friendrequest_object; - filter_function_cb *filter_function; - void *filter_function_userdata; + filter_function_cb *_Nullable filter_function; + void *_Nullable filter_function_userdata; struct Received_Requests received; }; @@ -145,7 +145,7 @@ static int friendreq_handlepacket(void *_Nonnull object, const uint8_t *_Nonnull } if (fr->filter_function != nullptr) { - if (fr->filter_function(fr->filter_function_userdata, source_pubkey) != 0) { + if (fr->filter_function((void *_Nonnull)fr->filter_function_userdata, source_pubkey) != 0) { return 1; } } @@ -157,7 +157,7 @@ static int friendreq_handlepacket(void *_Nonnull object, const uint8_t *_Nonnull memcpy(message, data + sizeof(fr->nospam), message_len); message[message_len] = 0; /* Be sure the message is null terminated. TODO(iphydf): But why? */ - fr->handle_friendrequest(fr->handle_friendrequest_object, source_pubkey, message, message_len, userdata); + fr->handle_friendrequest((void *_Nonnull)fr->handle_friendrequest_object, source_pubkey, message, message_len, userdata); return 0; } diff --git a/toxcore/friend_requests.h b/toxcore/friend_requests.h index 0a31367ce0..b97640acea 100644 --- a/toxcore/friend_requests.h +++ b/toxcore/friend_requests.h @@ -35,14 +35,14 @@ typedef void fr_friend_request_cb(void *_Nonnull object, const uint8_t *_Nonnull void *_Nullable user_data); /** Set the function that will be executed when a friend request for us is received. */ -void callback_friendrequest(Friend_Requests *_Nonnull fr, fr_friend_request_cb *_Nonnull function, void *_Nonnull object); +void callback_friendrequest(Friend_Requests *_Nonnull fr, fr_friend_request_cb *_Nullable function, void *_Nullable object); typedef int filter_function_cb(void *_Nonnull object, const uint8_t *_Nonnull public_key); /** @brief Set the function used to check if a friend request should be displayed to the user or not. * It must return 0 if the request is ok (anything else if it is bad). */ -void set_filter_function(Friend_Requests *_Nonnull fr, filter_function_cb *_Nonnull function, void *_Nonnull userdata); +void set_filter_function(Friend_Requests *_Nonnull fr, filter_function_cb *_Nullable function, void *_Nullable userdata); /** Sets up friendreq packet handlers. */ void friendreq_init(Friend_Requests *_Nonnull fr, Friend_Connections *_Nonnull fr_c); diff --git a/toxcore/group.c b/toxcore/group.c index 4eddd9d4b4..e8e05df549 100644 --- a/toxcore/group.c +++ b/toxcore/group.c @@ -87,7 +87,7 @@ typedef struct Group_Peer { uint16_t bottom_lossy_number; uint16_t top_lossy_number; - void *object; + void *_Nullable object; } Group_Peer; typedef struct Groupchat_Connection { @@ -112,10 +112,10 @@ typedef struct Group_c { bool need_send_name; bool title_fresh; - Group_Peer *group; + Group_Peer *_Nullable group; uint32_t numpeers; - Group_Peer *frozen; + Group_Peer *_Nullable frozen; uint32_t numfrozen; uint32_t maxfrozen; @@ -140,31 +140,31 @@ typedef struct Group_c { uint32_t num_introducer_connections; - void *object; + void *_Nullable object; - peer_on_join_cb *peer_on_join; - peer_on_leave_cb *peer_on_leave; - group_on_delete_cb *group_on_delete; + peer_on_join_cb *_Nullable peer_on_join; + peer_on_leave_cb *_Nullable peer_on_leave; + group_on_delete_cb *_Nullable group_on_delete; } Group_c; struct Group_Chats { - const Memory *mem; - const Mono_Time *mono_time; + const Memory *_Nonnull mem; + const Mono_Time *_Nonnull mono_time; - Messenger *m; - Friend_Connections *fr_c; + Messenger *_Nonnull m; + Friend_Connections *_Nonnull fr_c; - Group_c *chats; + Group_c *_Nullable chats; uint16_t num_chats; - g_conference_invite_cb *invite_callback; - g_conference_connected_cb *connected_callback; - g_conference_message_cb *message_callback; - peer_name_cb *peer_name_callback; - peer_list_changed_cb *peer_list_changed_callback; - title_cb *title_callback; + g_conference_invite_cb *_Nullable invite_callback; + g_conference_connected_cb *_Nullable connected_callback; + g_conference_message_cb *_Nullable message_callback; + peer_name_cb *_Nullable peer_name_callback; + peer_list_changed_cb *_Nullable peer_list_changed_callback; + title_cb *_Nullable title_callback; - lossy_packet_cb *lossy_packethandlers[256]; + lossy_packet_cb *_Nullable lossy_packethandlers[256]; }; static const Group_c empty_group_c = {0}; @@ -994,7 +994,7 @@ static bool delete_old_frozen(Group_c *_Nonnull g, const Memory *_Nonnull mem) return true; } - merge_sort(g->frozen, g->numfrozen, mem, &group_peer_cmp_funcs); + merge_sort((Group_Peer * _Nonnull)g->frozen, g->numfrozen, mem, &group_peer_cmp_funcs); Group_Peer *temp = (Group_Peer *)mem_vrealloc(mem, g->frozen, g->maxfrozen, sizeof(Group_Peer)); @@ -3748,7 +3748,7 @@ void kill_groupchats(Group_Chats *g_c) } for (uint16_t i = 0; i < g_c->num_chats; ++i) { - del_groupchat(g_c, i, false); + del_groupchat((Group_Chats * _Nonnull)g_c, i, false); } m_callback_conference_invite(g_c->m, nullptr); diff --git a/toxcore/group_chats.c b/toxcore/group_chats.c index 5ba2c858ae..73a34cde44 100644 --- a/toxcore/group_chats.c +++ b/toxcore/group_chats.c @@ -836,7 +836,7 @@ static void saved_peers_remove_entry(GC_Chat *_Nonnull chat, const uint8_t *_Non static int saved_peers_get_new_index(const GC_Chat *_Nonnull chat, const uint8_t *_Nullable public_key) { if (public_key != nullptr) { - const int idx = saved_peer_index(chat, public_key); + const int idx = saved_peer_index(chat, (const uint8_t *_Nonnull)public_key); if (idx != -1) { return idx; } @@ -1069,7 +1069,11 @@ static bool prune_gc_mod_list(GC_Chat *_Nonnull chat) bool pruned_mod = false; for (uint16_t i = 0; i < chat->moderation.num_mods; ++i) { - if (get_peer_number_of_sig_pk(chat, chat->moderation.mod_list[i]) == -1) { + if (chat->moderation.mod_list[i] == nullptr) { + LOGGER_ERROR(chat->log, "Moderator list contains a null entry at index %u", i); + continue; + } + if (get_peer_number_of_sig_pk(chat, (const uint8_t *_Nonnull)chat->moderation.mod_list[i]) == -1) { memcpy(public_sig_key, chat->moderation.mod_list[i], SIG_PUBLIC_KEY_SIZE); if (!mod_list_remove_index(&chat->moderation, i)) { @@ -1481,7 +1485,7 @@ static int group_packet_unwrap(const Logger *_Nonnull log, const Memory *_Nonnul plain_len -= sizeof(uint8_t); if (message_id != nullptr) { - net_unpack_u64(real_plain + sizeof(uint8_t), message_id); + net_unpack_u64(real_plain + sizeof(uint8_t), (uint64_t *_Nonnull)message_id); plain_len -= GC_MESSAGE_ID_BYTES; header_len += GC_MESSAGE_ID_BYTES; } @@ -1622,7 +1626,10 @@ static bool send_lossless_group_packet(const GC_Chat *_Nonnull chat, GC_Connecti } if (length > MAX_GC_PACKET_CHUNK_SIZE) { - return gcc_send_lossless_packet_fragments(chat, gconn, data, length, packet_type); + if (data == nullptr) { + return false; + } + return gcc_send_lossless_packet_fragments(chat, gconn, (const uint8_t *_Nonnull)data, length, packet_type); } return gcc_send_lossless_packet(chat, gconn, data, length, packet_type) == 0; @@ -1719,7 +1726,10 @@ static bool unpack_gc_sync_announce(GC_Chat *_Nonnull chat, const uint8_t *_Nonn uint32_t added_tcp_relays = 0; for (uint8_t i = 0; i < announce.tcp_relays_count; ++i) { - const int add_tcp_result = add_tcp_relay_connection(chat->tcp_conn, new_gconn->tcp_connection_num, + if (chat->tcp_conn == nullptr) { + return false; + } + const int add_tcp_result = add_tcp_relay_connection((TCP_Connections * _Nonnull)chat->tcp_conn, new_gconn->tcp_connection_num, &announce.tcp_relays[i].ip_port, announce.tcp_relays[i].public_key); @@ -1733,7 +1743,10 @@ static bool unpack_gc_sync_announce(GC_Chat *_Nonnull chat, const uint8_t *_Nonn } if (!announce.ip_port_is_set && added_tcp_relays == 0) { - gcc_mark_for_deletion(new_gconn, chat->tcp_conn, GC_EXIT_TYPE_DISCONNECTED, nullptr, 0); + if (chat->tcp_conn == nullptr) { + return false; + } + gcc_mark_for_deletion(new_gconn, (TCP_Connections * _Nonnull)chat->tcp_conn, GC_EXIT_TYPE_DISCONNECTED, nullptr, 0); LOGGER_ERROR(chat->log, "Sync error: Invalid peer connection info"); return false; } @@ -1765,7 +1778,10 @@ static int handle_gc_sync_response(const GC_Session *_Nonnull c, GC_Chat *_Nonnu } if (length > 0) { - if (!unpack_gc_sync_announce(chat, data, length)) { + if (data == NULL) { + return -1; + } + if (!unpack_gc_sync_announce(chat, (const uint8_t *_Nonnull)data, length)) { return -1; } } @@ -2184,7 +2200,7 @@ static bool send_gc_invite_response_reject(const GC_Chat *_Nonnull chat, GC_Conn * Return -3 if we fail to send an invite response. * Return -4 if peer_number does not designate a valid peer. */ -static int handle_gc_invite_request(GC_Chat *_Nonnull chat, uint32_t peer_number, const uint8_t *_Nullable data, uint16_t length) +static int handle_gc_invite_request(GC_Chat *_Nonnull chat, uint32_t peer_number, const uint8_t *_Nonnull data, uint16_t length) { if (chat->shared_state.version == 0) { // we aren't synced yet; ignore request return 0; @@ -7229,13 +7245,15 @@ static bool init_gc_tcp_connection(const GC_Session *_Nonnull c, GC_Chat *_Nonnu { const Messenger *m = c->messenger; - chat->tcp_conn = new_tcp_connections(chat->log, chat->mem, chat->rng, m->ns, chat->mono_time, chat->self_secret_key.enc, - &m->options.proxy_info, c->tcp_np); + TCP_Connections *tcp_conn = new_tcp_connections(chat->log, chat->mem, chat->rng, m->ns, chat->mono_time, chat->self_secret_key.enc, + &m->options.proxy_info, c->tcp_np); - if (chat->tcp_conn == nullptr) { + if (tcp_conn == nullptr) { return false; } + chat->tcp_conn = tcp_conn; + add_tcp_relays_to_chat(c, chat); set_packet_tcp_connection_callback(chat->tcp_conn, &handle_gc_tcp_packet, c->messenger); @@ -8160,7 +8178,7 @@ void kill_dht_groupchats(GC_Session *c) continue; } - if (kill_group(c, chat) != 0) { + if (kill_group((GC_Session * _Nonnull)c, chat) != 0) { LOGGER_WARNING(c->messenger->log, "Failed to send group exit packet"); } } diff --git a/toxcore/group_chats.h b/toxcore/group_chats.h index ef051e7561..8369293486 100644 --- a/toxcore/group_chats.h +++ b/toxcore/group_chats.h @@ -541,7 +541,7 @@ void do_gc(GC_Session *_Nonnull c, void *_Nullable userdata); * Make sure that DHT is initialized before calling this. * Returns a NULL pointer on failure. */ -GC_Session *_Nullable new_dht_groupchats(Messenger *_Nullable m); +GC_Session *_Nullable new_dht_groupchats(Messenger *_Nonnull m); /** @brief Cleans up groupchat structures and calls `gc_group_exit()` for every group chat */ void kill_dht_groupchats(GC_Session *_Nullable c); /** @brief Loads a previously saved group and attempts to join it. @@ -680,7 +680,7 @@ bool gc_send_message_ack(const GC_Chat *_Nonnull chat, GC_Connection *_Nonnull g * * @retval true if packet is successfully handled. */ -bool handle_gc_lossless_helper(const GC_Session *_Nonnull c, GC_Chat *_Nonnull chat, uint32_t peer_number, const uint8_t *_Nullable data, +bool handle_gc_lossless_helper(const GC_Session *_Nonnull c, GC_Chat *_Nonnull chat, uint32_t peer_number, const uint8_t *_Nonnull data, uint16_t length, uint8_t packet_type, void *_Nullable userdata); /** @brief Handles an invite accept packet. * diff --git a/toxcore/group_common.h b/toxcore/group_common.h index 35dcb702a9..2ba7436898 100644 --- a/toxcore/group_common.h +++ b/toxcore/group_common.h @@ -275,7 +275,7 @@ typedef struct GC_Chat { IP_Port self_ip_port; Networking_Core *_Nonnull net; - TCP_Connections *_Nullable tcp_conn; + TCP_Connections *_Nonnull tcp_conn; uint64_t last_checked_tcp_relays; Group_Handshake_Join_Type join_type; @@ -379,8 +379,8 @@ typedef void gc_rejected_cb(const Messenger *_Nonnull m, uint32_t group_number, typedef struct GC_Session { Messenger *_Nonnull messenger; GC_Chat *_Nullable chats; - Net_Profile *_Nullable tcp_np; - struct GC_Announces_List *_Nullable announces_list; + Net_Profile *_Nonnull tcp_np; + struct GC_Announces_List *_Nonnull announces_list; uint32_t chats_index; diff --git a/toxcore/group_connection.c b/toxcore/group_connection.c index 5b68fe0a5e..ebfaf319a7 100644 --- a/toxcore/group_connection.c +++ b/toxcore/group_connection.c @@ -275,7 +275,7 @@ bool gcc_ip_port_is_set(const GC_Connection *gconn) void gcc_set_ip_port(GC_Connection *gconn, const IP_Port *ipp) { - if (ipp != nullptr && ipport_isset(ipp)) { + if (ipp != nullptr && ipport_isset((const IP_Port * _Nonnull)ipp)) { gconn->addr.ip_port = *ipp; } } @@ -452,13 +452,15 @@ int gcc_handle_packet_fragment(const GC_Session *c, GC_Chat *chat, uint32_t peer /* peer number can change from peer add operations in packet handlers */ peer_number = get_peer_number_of_enc_pk(chat, sender_pk, false); - gconn = get_gc_connection(chat, peer_number); + GC_Connection *new_gconn = get_gc_connection(chat, peer_number); - if (gconn == nullptr) { + if (new_gconn == nullptr) { mem_delete(chat->mem, payload); return 0; } + gconn = (GC_Connection * _Nonnull)new_gconn; + gcc_set_recv_message_id(gconn, gconn->received_message_id + 1); gconn->last_chunk_id = 0; @@ -510,12 +512,12 @@ static bool process_recv_array_entry(const GC_Session *_Nonnull c, GC_Chat *_Non uint8_t sender_pk[ENC_PUBLIC_KEY_SIZE]; memcpy(sender_pk, get_enc_key(&gconn->addr.public_key), ENC_PUBLIC_KEY_SIZE); - const bool ret = handle_gc_lossless_helper(c, chat, peer_number, array_entry->data, array_entry->data_length, + const bool ret = handle_gc_lossless_helper(c, chat, peer_number, (const uint8_t *_Nonnull)array_entry->data, array_entry->data_length, array_entry->packet_type, userdata); /* peer number can change from peer add operations in packet handlers */ peer_number = get_peer_number_of_enc_pk(chat, sender_pk, false); - gconn = get_gc_connection(chat, peer_number); + gconn = (GC_Connection * _Nonnull)get_gc_connection(chat, peer_number); clear_array_entry(chat->mem, array_entry); @@ -524,13 +526,13 @@ static bool process_recv_array_entry(const GC_Session *_Nonnull c, GC_Chat *_Non } if (!ret) { - gc_send_message_ack(chat, gconn, array_entry->message_id, GR_ACK_REQ); + gc_send_message_ack(chat, (GC_Connection * _Nonnull)gconn, array_entry->message_id, GR_ACK_REQ); return false; } - gc_send_message_ack(chat, gconn, array_entry->message_id, GR_ACK_RECV); + gc_send_message_ack(chat, (GC_Connection * _Nonnull)gconn, array_entry->message_id, GR_ACK_RECV); - gcc_set_recv_message_id(gconn, gconn->received_message_id + 1); + gcc_set_recv_message_id((GC_Connection * _Nonnull)gconn, gconn->received_message_id + 1); return true; } @@ -563,7 +565,7 @@ void gcc_resend_packets(const GC_Chat *chat, GC_Connection *gconn) } if (mono_time_is_timeout(chat->mono_time, array_entry->time_added, GC_CONFIRMED_PEER_TIMEOUT)) { - gcc_mark_for_deletion(gconn, chat->tcp_conn, GC_EXIT_TYPE_TIMEOUT, nullptr, 0); + gcc_mark_for_deletion(gconn, (TCP_Connections * _Nonnull)chat->tcp_conn, GC_EXIT_TYPE_TIMEOUT, nullptr, 0); LOGGER_DEBUG(chat->log, "Send array stuck; timing out peer"); return; } @@ -612,7 +614,10 @@ bool gcc_send_packet(const GC_Chat *chat, GC_Connection *gconn, const uint8_t *p } } - const int ret = send_packet_tcp_connection(chat->tcp_conn, gconn->tcp_connection_num, packet, length); + if (chat->tcp_conn == nullptr) { + return false; + } + const int ret = send_packet_tcp_connection((const TCP_Connections * _Nonnull)chat->tcp_conn, gconn->tcp_connection_num, packet, length); return ret == 0 || direct_send_attempt; } @@ -682,7 +687,9 @@ void gcc_mark_for_deletion(GC_Connection *gconn, TCP_Connections *tcp_conn, Grou gconn->pending_delete = true; gconn->exit_info.exit_type = type; - kill_tcp_connection_to(tcp_conn, gconn->tcp_connection_num); + if (tcp_conn != nullptr) { + kill_tcp_connection_to((TCP_Connections * _Nonnull)tcp_conn, gconn->tcp_connection_num); + } if (length > 0 && length <= MAX_GC_PART_MESSAGE_SIZE && part_message != nullptr) { memcpy(gconn->exit_info.part_message, part_message, length); diff --git a/toxcore/group_moderation.c b/toxcore/group_moderation.c index b25eb440e2..6add88b350 100644 --- a/toxcore/group_moderation.c +++ b/toxcore/group_moderation.c @@ -304,7 +304,7 @@ int sanctions_list_pack(uint8_t *_Nonnull data, uint16_t length, const Mod_Sanct return -1; } - const uint16_t cred_len = sanctions_creds_pack(creds, &data[packed_len]); + const uint16_t cred_len = sanctions_creds_pack((const Mod_Sanction_Creds * _Nonnull)creds, &data[packed_len]); if (cred_len != MOD_SANCTIONS_CREDS_SIZE) { return -1; @@ -586,7 +586,7 @@ static bool sanctions_apply_new(Moderation *_Nonnull moderation, Mod_Sanction *_ uint16_t num_sanctions) { if (new_creds != nullptr) { - if (!sanctions_creds_validate(moderation, new_sanctions, new_creds, num_sanctions)) { + if (!sanctions_creds_validate(moderation, new_sanctions, (const Mod_Sanction_Creds * _Nonnull) new_creds, num_sanctions)) { LOGGER_WARNING(moderation->log, "Failed to validate credentials"); return false; } @@ -633,7 +633,7 @@ static bool sanctions_list_remove_index(Moderation *_Nonnull moderation, uint16_ if (new_num == 0) { if (creds != nullptr) { - if (!sanctions_creds_validate(moderation, nullptr, creds, 0)) { + if (!sanctions_creds_validate(moderation, nullptr, (const Mod_Sanction_Creds * _Nonnull) creds, 0)) { return false; } @@ -646,7 +646,10 @@ static bool sanctions_list_remove_index(Moderation *_Nonnull moderation, uint16_ } /* Operate on a copy of the list in case something goes wrong. */ - Mod_Sanction *sanctions_copy = sanctions_list_copy(moderation->mem, moderation->sanctions, moderation->num_sanctions); + if (moderation->sanctions == NULL) { + return false; + } + Mod_Sanction *sanctions_copy = sanctions_list_copy(moderation->mem, (const Mod_Sanction * _Nonnull)moderation->sanctions, moderation->num_sanctions); if (sanctions_copy == nullptr) { return false; @@ -744,7 +747,10 @@ bool sanctions_list_add_entry(Moderation *_Nonnull moderation, const Mod_Sanctio Mod_Sanction *sanctions_copy = nullptr; if (moderation->num_sanctions > 0) { - sanctions_copy = sanctions_list_copy(moderation->mem, moderation->sanctions, moderation->num_sanctions); + if (moderation->sanctions == NULL) { + return false; + } + sanctions_copy = sanctions_list_copy(moderation->mem, (const Mod_Sanction * _Nonnull)moderation->sanctions, moderation->num_sanctions); if (sanctions_copy == nullptr) { return false; diff --git a/toxcore/logger.c b/toxcore/logger.c index b97ef8e184..89d4af6bee 100644 --- a/toxcore/logger.c +++ b/toxcore/logger.c @@ -18,11 +18,11 @@ #include "mem.h" struct Logger { - const Memory *mem; + const Memory *_Nonnull mem; - logger_cb *callback; - void *context; - void *userdata; + logger_cb *_Nullable callback; + void *_Nullable context; + void *_Nullable userdata; }; /* diff --git a/toxcore/mono_time.c b/toxcore/mono_time.c index 6e1ea375c0..379725b6a2 100644 --- a/toxcore/mono_time.c +++ b/toxcore/mono_time.c @@ -43,12 +43,12 @@ struct Mono_Time { uint64_t base_time; #ifndef ESP_PLATFORM - /* protect `time` from concurrent access */ - pthread_rwlock_t *time_update_lock; + /** protect @ref cur_time from concurrent access */ + pthread_rwlock_t *_Nonnull time_update_lock; #endif /* ESP_PLATFORM */ - mono_time_current_time_cb *current_time_callback; - void *user_data; + mono_time_current_time_cb *_Nonnull current_time_callback; + void *_Nullable user_data; }; static uint64_t timespec_to_u64(struct timespec clock_mono) @@ -116,7 +116,7 @@ Mono_Time *mono_time_new(const Memory *mem, mono_time_current_time_cb *current_t } #ifndef ESP_PLATFORM - pthread_rwlock_t *rwlock = (pthread_rwlock_t *)mem_alloc(mem, sizeof(pthread_rwlock_t)); + pthread_rwlock_t *const rwlock = (pthread_rwlock_t *)mem_alloc(mem, sizeof(pthread_rwlock_t)); if (rwlock == nullptr) { mem_delete(mem, mono_time); @@ -205,7 +205,7 @@ void mono_time_set_current_time_callback(Mono_Time *mono_time, mono_time->current_time_callback = current_time_monotonic_default; mono_time->user_data = mono_time; } else { - mono_time->current_time_callback = current_time_callback; + mono_time->current_time_callback = (mono_time_current_time_cb * _Nonnull)current_time_callback; mono_time->user_data = user_data; } } diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 6815a13ab6..bd37e16951 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -35,7 +35,7 @@ typedef struct Packet_Data { } Packet_Data; typedef struct Packets_Array { - Packet_Data *buffer[CRYPTO_PACKET_BUFFER_SIZE]; + Packet_Data *_Nullable buffer[CRYPTO_PACKET_BUFFER_SIZE]; uint32_t buffer_start; uint32_t buffer_end; /* packet numbers in array: `{buffer_start, buffer_end)` */ } Packets_Array; @@ -66,7 +66,7 @@ typedef struct Crypto_Connection { uint64_t cookie_request_number; /* number used in the cookie request packets for this connection */ uint8_t dht_public_key[CRYPTO_PUBLIC_KEY_SIZE]; /* The dht public key of the peer */ - uint8_t *temp_packet; /* Where the cookie request/handshake packet is stored while it is being sent. */ + uint8_t *_Nullable temp_packet; /* Where the cookie request/handshake packet is stored while it is being sent. */ uint16_t temp_packet_length; uint64_t temp_packet_sent_time; /* The time at which the last temp_packet was sent in ms. */ uint32_t temp_packet_num_sent; @@ -81,16 +81,16 @@ typedef struct Crypto_Connection { Packets_Array send_array; Packets_Array recv_array; - connection_status_cb *connection_status_callback; - void *connection_status_callback_object; + connection_status_cb *_Nullable connection_status_callback; + void *_Nullable connection_status_callback_object; int connection_status_callback_id; - connection_data_cb *connection_data_callback; - void *connection_data_callback_object; + connection_data_cb *_Nullable connection_data_callback; + void *_Nullable connection_data_callback_object; int connection_data_callback_id; - connection_lossy_data_cb *connection_lossy_data_callback; - void *connection_lossy_data_callback_object; + connection_lossy_data_cb *_Nullable connection_lossy_data_callback; + void *_Nullable connection_lossy_data_callback_object; int connection_lossy_data_callback_id; uint64_t last_request_packet_sent; @@ -124,24 +124,24 @@ typedef struct Crypto_Connection { bool maximum_speed_reached; - dht_pk_cb *dht_pk_callback; - void *dht_pk_callback_object; + dht_pk_cb *_Nullable dht_pk_callback; + void *_Nullable dht_pk_callback_object; uint32_t dht_pk_callback_number; } Crypto_Connection; static const Crypto_Connection empty_crypto_connection = {{0}}; struct Net_Crypto { - const Logger *log; - const Memory *mem; - const Random *rng; - Mono_Time *mono_time; - const Network *ns; + const Logger *_Nonnull log; + const Memory *_Nonnull mem; + const Random *_Nonnull rng; + Mono_Time *_Nonnull mono_time; + const Network *_Nonnull ns; - DHT *dht; - TCP_Connections *tcp_c; + DHT *_Nonnull dht; + TCP_Connections *_Nonnull tcp_c; - Crypto_Connection *crypto_connections; + Crypto_Connection *_Nullable crypto_connections; uint32_t crypto_connections_length; /* Length of connections array. */ @@ -152,8 +152,8 @@ struct Net_Crypto { /* The secret key used for cookies */ uint8_t secret_symmetric_key[CRYPTO_SYMMETRIC_KEY_SIZE]; - new_connection_cb *new_connection_callback; - void *new_connection_callback_object; + new_connection_cb *_Nullable new_connection_callback; + void *_Nullable new_connection_callback_object; /* The current optimal sleep time */ uint32_t current_sleep_time; @@ -518,7 +518,7 @@ static bool handle_crypto_handshake(const Net_Crypto *_Nonnull c, uint8_t *_Nonn return false; } - if (expected_real_pk != nullptr && !pk_equal(cookie_plain, expected_real_pk)) { + if (expected_real_pk != nullptr && !pk_equal(cookie_plain, (const uint8_t *_Nonnull)expected_real_pk)) { return false; } @@ -772,7 +772,7 @@ static int get_data_pointer(const Packets_Array *_Nonnull array, Packet_Data *_N return 0; } - *data = array->buffer[num]; + *data = (Packet_Data * _Nonnull)array->buffer[num]; return 1; } @@ -1383,7 +1383,7 @@ static int send_temp_packet(const Net_Crypto *_Nonnull c, int crypt_connection_i return -1; } - if (send_packet_to(c, crypt_connection_id, conn->temp_packet, conn->temp_packet_length) != 0) { + if (send_packet_to(c, crypt_connection_id, (const uint8_t *_Nonnull)conn->temp_packet, conn->temp_packet_length) != 0) { return -1; } @@ -1447,7 +1447,7 @@ static void connection_kill(Net_Crypto *_Nonnull c, int crypt_connection_id, voi } if (conn->connection_status_callback != nullptr) { - conn->connection_status_callback(conn->connection_status_callback_object, conn->connection_status_callback_id, + conn->connection_status_callback((void *_Nonnull)conn->connection_status_callback_object, conn->connection_status_callback_id, false, userdata); } @@ -1522,7 +1522,7 @@ static int handle_data_packet_core(Net_Crypto *_Nonnull c, int crypt_connection_ conn->status = CRYPTO_CONN_ESTABLISHED; if (conn->connection_status_callback != nullptr) { - conn->connection_status_callback(conn->connection_status_callback_object, conn->connection_status_callback_id, + conn->connection_status_callback((void *_Nonnull)conn->connection_status_callback_object, conn->connection_status_callback_id, true, userdata); } } @@ -1560,7 +1560,7 @@ static int handle_data_packet_core(Net_Crypto *_Nonnull c, int crypt_connection_ } if (conn->connection_data_callback != nullptr) { - conn->connection_data_callback(conn->connection_data_callback_object, conn->connection_data_callback_id, dt.data, + conn->connection_data_callback((void *_Nonnull)conn->connection_data_callback_object, conn->connection_data_callback_id, dt.data, dt.length, userdata); } @@ -1579,7 +1579,7 @@ static int handle_data_packet_core(Net_Crypto *_Nonnull c, int crypt_connection_ set_buffer_end(&conn->recv_array, num); if (conn->connection_lossy_data_callback != nullptr) { - conn->connection_lossy_data_callback(conn->connection_lossy_data_callback_object, + conn->connection_lossy_data_callback((void *_Nonnull)conn->connection_lossy_data_callback_object, conn->connection_lossy_data_callback_id, real_data, real_length, userdata); } } else { @@ -1663,7 +1663,7 @@ static int handle_packet_crypto_hs(const Net_Crypto *_Nonnull c, int crypt_conne conn->status = CRYPTO_CONN_NOT_CONFIRMED; } else { if (conn->dht_pk_callback != nullptr) { - conn->dht_pk_callback(conn->dht_pk_callback_object, conn->dht_pk_callback_number, dht_public_key, userdata); + conn->dht_pk_callback((void *_Nonnull)conn->dht_pk_callback_object, conn->dht_pk_callback_number, dht_public_key, userdata); } } @@ -1946,7 +1946,7 @@ static int handle_new_connection_handshake(Net_Crypto *_Nonnull c, const IP_Port } } - const int ret = c->new_connection_callback(c->new_connection_callback_object, &n_c); + const int ret = c->new_connection_callback((void *_Nonnull)c->new_connection_callback_object, &n_c); mem_delete(c->mem, n_c.cookie); return ret; } @@ -2244,7 +2244,7 @@ uint32_t copy_connected_tcp_relays_index(const Net_Crypto *c, Node_format *tcp_r return tcp_copy_connected_relays_index(c->tcp_c, tcp_relays, num, idx); } -static void do_tcp(Net_Crypto *_Nonnull c, void *_Nonnull userdata) +static void do_tcp(Net_Crypto *_Nonnull c, void *_Nullable userdata) { do_tcp_connections(c->log, c->tcp_c, userdata); @@ -2933,13 +2933,15 @@ Net_Crypto *new_net_crypto(const Logger *log, const Memory *mem, const Random *r temp->mono_time = mono_time; temp->ns = ns; - temp->tcp_c = new_tcp_connections(log, mem, rng, ns, mono_time, dht_get_self_secret_key(dht), proxy_info, tcp_np); + TCP_Connections *const tcp_c = new_tcp_connections(log, mem, rng, ns, mono_time, dht_get_self_secret_key(dht), proxy_info, tcp_np); - if (temp->tcp_c == nullptr) { + if (tcp_c == nullptr) { mem_delete(mem, temp); return nullptr; } + temp->tcp_c = tcp_c; + set_packet_tcp_connection_callback(temp->tcp_c, &tcp_data_callback, temp); set_oob_packet_tcp_connection_callback(temp->tcp_c, &tcp_oob_callback, temp); @@ -3011,7 +3013,7 @@ void kill_net_crypto(Net_Crypto *c) const Memory *mem = c->mem; for (uint32_t i = 0; i < c->crypto_connections_length; ++i) { - crypto_kill(c, i); + crypto_kill((Net_Crypto * _Nonnull)c, i); } kill_tcp_connections(c->tcp_c); @@ -3020,6 +3022,6 @@ void kill_net_crypto(Net_Crypto *c) networking_registerhandler(dht_get_net(c->dht), NET_PACKET_COOKIE_RESPONSE, nullptr, nullptr); networking_registerhandler(dht_get_net(c->dht), NET_PACKET_CRYPTO_HS, nullptr, nullptr); networking_registerhandler(dht_get_net(c->dht), NET_PACKET_CRYPTO_DATA, nullptr, nullptr); - crypto_memzero(c, sizeof(Net_Crypto)); + crypto_memzero((Net_Crypto * _Nonnull)c, sizeof(Net_Crypto)); mem_delete(mem, c); } diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h index dc93d6585a..14c977877f 100644 --- a/toxcore/net_crypto.h +++ b/toxcore/net_crypto.h @@ -134,7 +134,7 @@ typedef struct New_Connection { uint8_t dht_public_key[CRYPTO_PUBLIC_KEY_SIZE]; /* The dht public key of the peer. */ uint8_t recv_nonce[CRYPTO_NONCE_SIZE]; /* Nonce of received packets. */ uint8_t peersessionpublic_key[CRYPTO_PUBLIC_KEY_SIZE]; /* The public key of the peer. */ - uint8_t *_Nullable cookie; + uint8_t *_Nonnull cookie; uint8_t cookie_length; } New_Connection; diff --git a/toxcore/network.c b/toxcore/network.c index 00e8c87b4f..e19f0fd269 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -970,22 +970,22 @@ bool set_socket_dualstack(const Network *ns, Socket sock) } typedef struct Packet_Handler { - packet_handler_cb *function; - void *object; + packet_handler_cb *_Nullable function; + void *_Nullable object; } Packet_Handler; struct Networking_Core { - const Logger *log; - const Memory *mem; + const Logger *_Nonnull log; + const Memory *_Nonnull mem; Packet_Handler packethandlers[256]; - const Network *ns; + const Network *_Nonnull ns; Family family; uint16_t port; /* Our UDP socket. */ Socket sock; - Net_Profile *udp_net_profile; + Net_Profile *_Nullable udp_net_profile; }; Family net_family(const Networking_Core *net) @@ -1971,7 +1971,7 @@ static bool addr_resolve(const Network *_Nonnull ns, const Memory *_Nonnull mem, ip_copy(to, &ip6); if ((result & TOX_ADDR_RESOLVE_INET) != 0 && (extra != nullptr)) { - ip_copy(extra, &ip4); + ip_copy((IP * _Nonnull)extra, &ip4); } } else if ((result & TOX_ADDR_RESOLVE_INET) != 0) { ip_copy(to, &ip4); diff --git a/toxcore/onion.c b/toxcore/onion.c index f5738dfb89..dc950e4613 100644 --- a/toxcore/onion.c +++ b/toxcore/onion.c @@ -724,18 +724,23 @@ Onion *new_onion(const Logger *log, const Memory *mem, const Mono_Time *mono_tim onion->timestamp = mono_time_get(onion->mono_time); const uint8_t *secret_key = dht_get_self_secret_key(dht); - onion->shared_keys_1 = shared_key_cache_new(log, mono_time, mem, secret_key, KEYS_TIMEOUT, MAX_KEYS_PER_SLOT); - onion->shared_keys_2 = shared_key_cache_new(log, mono_time, mem, secret_key, KEYS_TIMEOUT, MAX_KEYS_PER_SLOT); - onion->shared_keys_3 = shared_key_cache_new(log, mono_time, mem, secret_key, KEYS_TIMEOUT, MAX_KEYS_PER_SLOT); - - if (onion->shared_keys_1 == nullptr || - onion->shared_keys_2 == nullptr || - onion->shared_keys_3 == nullptr) { + Shared_Key_Cache *const temp_shared_keys_1 = shared_key_cache_new(log, mono_time, mem, secret_key, KEYS_TIMEOUT, MAX_KEYS_PER_SLOT); + Shared_Key_Cache *const temp_shared_keys_2 = shared_key_cache_new(log, mono_time, mem, secret_key, KEYS_TIMEOUT, MAX_KEYS_PER_SLOT); + Shared_Key_Cache *const temp_shared_keys_3 = shared_key_cache_new(log, mono_time, mem, secret_key, KEYS_TIMEOUT, MAX_KEYS_PER_SLOT); + + if (temp_shared_keys_1 == nullptr || temp_shared_keys_2 == nullptr || temp_shared_keys_3 == nullptr) { + shared_key_cache_free(temp_shared_keys_3); + shared_key_cache_free(temp_shared_keys_2); + shared_key_cache_free(temp_shared_keys_1); // cppcheck-suppress mismatchAllocDealloc kill_onion(onion); return nullptr; } + onion->shared_keys_1 = temp_shared_keys_1; + onion->shared_keys_2 = temp_shared_keys_2; + onion->shared_keys_3 = temp_shared_keys_3; + networking_registerhandler(onion->net, NET_PACKET_ONION_SEND_INITIAL, &handle_send_initial, onion); networking_registerhandler(onion->net, NET_PACKET_ONION_SEND_1, &handle_send_1, onion); networking_registerhandler(onion->net, NET_PACKET_ONION_SEND_2, &handle_send_2, onion); diff --git a/toxcore/onion_announce.c b/toxcore/onion_announce.c index 6c80aa96de..0d72966952 100644 --- a/toxcore/onion_announce.c +++ b/toxcore/onion_announce.c @@ -55,20 +55,20 @@ typedef struct Onion_Announce_Entry { } Onion_Announce_Entry; struct Onion_Announce { - const Logger *log; - const Mono_Time *mono_time; - const Random *rng; - const Memory *mem; - DHT *dht; - Networking_Core *net; + const Logger *_Nonnull log; + const Mono_Time *_Nonnull mono_time; + const Random *_Nonnull rng; + const Memory *_Nonnull mem; + DHT *_Nonnull dht; + Networking_Core *_Nonnull net; Onion_Announce_Entry entries[ONION_ANNOUNCE_MAX_ENTRIES]; uint8_t hmac_key[CRYPTO_HMAC_KEY_SIZE]; - Shared_Key_Cache *shared_keys_recv; + Shared_Key_Cache *_Nonnull shared_keys_recv; uint16_t extra_data_max_size; - pack_extra_data_cb *extra_data_callback; - void *extra_data_object; + pack_extra_data_cb *_Nullable extra_data_callback; + void *_Nullable extra_data_object; }; void onion_announce_extra_data_callback(Onion_Announce *onion_a, uint16_t extra_data_max_size, @@ -565,7 +565,7 @@ static int handle_announce_request_common( } const int extra_size = pack_extra_data_callback == nullptr ? 0 - : pack_extra_data_callback(onion_a->extra_data_object, + : pack_extra_data_callback((void *_Nonnull)onion_a->extra_data_object, onion_a->log, onion_a->mem, onion_a->mono_time, num_nodes, plain + ONION_MINIMAL_SIZE, length - ANNOUNCE_REQUEST_MIN_SIZE_RECV, response, response_size, offset); @@ -692,6 +692,13 @@ Onion_Announce *new_onion_announce(const Logger *log, const Memory *mem, const R return nullptr; } + Shared_Key_Cache *const shared_keys_recv = shared_key_cache_new(log, mono_time, mem, dht_get_self_secret_key(dht), KEYS_TIMEOUT, MAX_KEYS_PER_SLOT); + if (shared_keys_recv == nullptr) { + mem_delete(mem, onion_a); + return nullptr; + } + onion_a->shared_keys_recv = shared_keys_recv; + onion_a->log = log; onion_a->rng = rng; onion_a->mem = mem; @@ -703,13 +710,6 @@ Onion_Announce *new_onion_announce(const Logger *log, const Memory *mem, const R onion_a->extra_data_object = nullptr; new_hmac_key(rng, onion_a->hmac_key); - onion_a->shared_keys_recv = shared_key_cache_new(log, mono_time, mem, dht_get_self_secret_key(dht), KEYS_TIMEOUT, MAX_KEYS_PER_SLOT); - if (onion_a->shared_keys_recv == nullptr) { - // cppcheck-suppress mismatchAllocDealloc - kill_onion_announce(onion_a); - return nullptr; - } - networking_registerhandler(onion_a->net, NET_PACKET_ANNOUNCE_REQUEST, &handle_announce_request, onion_a); networking_registerhandler(onion_a->net, NET_PACKET_ANNOUNCE_REQUEST_OLD, &handle_announce_request_old, onion_a); networking_registerhandler(onion_a->net, NET_PACKET_ONION_DATA_REQUEST, &handle_data_request, onion_a); diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c index 4c50925b99..ba45ccc1ea 100644 --- a/toxcore/onion_client.c +++ b/toxcore/onion_client.c @@ -94,12 +94,12 @@ struct Onion_Friend { Last_Pinged last_pinged[MAX_STORED_PINGED_NODES]; uint8_t last_pinged_index; - recv_tcp_relay_cb *tcp_relay_node_callback; - void *tcp_relay_node_callback_object; + recv_tcp_relay_cb *_Nullable tcp_relay_node_callback; + void *_Nullable tcp_relay_node_callback_object; uint32_t tcp_relay_node_callback_number; - onion_dht_pk_cb *dht_pk_callback; - void *dht_pk_callback_object; + onion_dht_pk_cb *_Nullable dht_pk_callback; + void *_Nullable dht_pk_callback_object; uint32_t dht_pk_callback_number; uint8_t gc_data[GCA_MAX_DATA_LENGTH]; @@ -111,20 +111,20 @@ struct Onion_Friend { static const Onion_Friend empty_onion_friend = {false}; typedef struct Onion_Data_Handler { - oniondata_handler_cb *function; - void *object; + oniondata_handler_cb *_Nullable function; + void *_Nullable object; } Onion_Data_Handler; struct Onion_Client { - const Mono_Time *mono_time; - const Logger *logger; - const Random *rng; - const Memory *mem; - - DHT *dht; - Net_Crypto *c; - Networking_Core *net; - Onion_Friend *friends_list; + const Mono_Time *_Nonnull mono_time; + const Logger *_Nonnull logger; + const Random *_Nonnull rng; + const Memory *_Nonnull mem; + + DHT *_Nonnull dht; + Net_Crypto *_Nonnull c; + Networking_Core *_Nonnull net; + Onion_Friend *_Nullable friends_list; uint16_t num_friends; Onion_Node clients_announce_list[MAX_ONION_CLIENTS_ANNOUNCE]; @@ -149,7 +149,7 @@ struct Onion_Client { Node_format path_nodes_bs[MAX_PATH_NODES]; uint16_t path_nodes_index_bs; - Ping_Array *announce_ping_array; + Ping_Array *_Nonnull announce_ping_array; uint8_t last_pinged_index; Onion_Data_Handler onion_data_handlers[256]; @@ -159,8 +159,8 @@ struct Onion_Client { unsigned int onion_connected; bool udp_connected; - onion_group_announce_cb *group_announce_response; - void *group_announce_response_user_data; + onion_group_announce_cb *_Nullable group_announce_response; + void *_Nullable group_announce_response_user_data; }; uint16_t onion_get_friend_count(const Onion_Client *const onion_c) @@ -655,7 +655,7 @@ static int client_send_announce_request(Onion_Client *_Nonnull onion_c, uint32_t if (num == 0) { len = create_announce_request( onion_c->mem, onion_c->rng, request, sizeof(request), dest_pubkey, nc_get_self_public_key(onion_c->c), - nc_get_self_secret_key(onion_c->c), ping_id, nc_get_self_public_key(onion_c->c), + nc_get_self_secret_key(onion_c->c), (const uint8_t *_Nonnull)ping_id, nc_get_self_public_key(onion_c->c), onion_c->temp_public_key, sendback); } else { Onion_Friend *onion_friend = &onion_c->friends_list[num - 1]; @@ -663,14 +663,14 @@ static int client_send_announce_request(Onion_Client *_Nonnull onion_c, uint32_t if (onion_friend->gc_data_length == 0) { // contact is a friend len = create_announce_request( onion_c->mem, onion_c->rng, request, sizeof(request), dest_pubkey, onion_friend->temp_public_key, - onion_friend->temp_secret_key, ping_id, onion_friend->real_public_key, + onion_friend->temp_secret_key, (const uint8_t *_Nonnull)ping_id, onion_friend->real_public_key, zero_ping_id, sendback); } else { // contact is a gc onion_friend->is_groupchat = true; len = create_gca_announce_request( onion_c->mem, onion_c->rng, request, sizeof(request), dest_pubkey, onion_friend->temp_public_key, - onion_friend->temp_secret_key, ping_id, onion_friend->real_public_key, + onion_friend->temp_secret_key, (const uint8_t *_Nonnull)ping_id, onion_friend->real_public_key, zero_ping_id, sendback, onion_friend->gc_data, onion_friend->gc_data_length); } @@ -1207,7 +1207,7 @@ static int handle_dhtpk_announce(void *_Nonnull object, const uint8_t *_Nonnull onion_c->friends_list[friend_num].last_noreplay = no_replay; if (onion_c->friends_list[friend_num].dht_pk_callback != nullptr) { - onion_c->friends_list[friend_num].dht_pk_callback(onion_c->friends_list[friend_num].dht_pk_callback_object, + onion_c->friends_list[friend_num].dht_pk_callback((void *_Nonnull)onion_c->friends_list[friend_num].dht_pk_callback_object, onion_c->friends_list[friend_num].dht_pk_callback_number, data + 1 + sizeof(uint64_t), userdata); } @@ -2181,12 +2181,13 @@ Onion_Client *new_onion_client(const Logger *logger, const Memory *mem, const Ra return nullptr; } - onion_c->announce_ping_array = ping_array_new(mem, ANNOUNCE_ARRAY_SIZE, ANNOUNCE_TIMEOUT); + Ping_Array *const temp_ping_array = ping_array_new(mem, ANNOUNCE_ARRAY_SIZE, ANNOUNCE_TIMEOUT); - if (onion_c->announce_ping_array == nullptr) { + if (temp_ping_array == nullptr) { mem_delete(mem, onion_c); return nullptr; } + onion_c->announce_ping_array = temp_ping_array; onion_c->mono_time = mono_time; onion_c->logger = logger; @@ -2216,13 +2217,13 @@ void kill_onion_client(Onion_Client *onion_c) const Memory *mem = onion_c->mem; ping_array_kill(onion_c->announce_ping_array); - realloc_onion_friends(onion_c, 0); + realloc_onion_friends((Onion_Client * _Nonnull)onion_c, 0); networking_registerhandler(onion_c->net, NET_PACKET_ANNOUNCE_RESPONSE, nullptr, nullptr); networking_registerhandler(onion_c->net, NET_PACKET_ANNOUNCE_RESPONSE_OLD, nullptr, nullptr); networking_registerhandler(onion_c->net, NET_PACKET_ONION_DATA_RESPONSE, nullptr, nullptr); - oniondata_registerhandler(onion_c, ONION_DATA_DHTPK, nullptr, nullptr); + oniondata_registerhandler((Onion_Client * _Nonnull)onion_c, ONION_DATA_DHTPK, nullptr, nullptr); cryptopacket_registerhandler(onion_c->dht, CRYPTO_PACKET_DHTPK, nullptr, nullptr); set_onion_packet_tcp_connection_callback(nc_get_tcp_c(onion_c->c), nullptr, nullptr); - crypto_memzero(onion_c, sizeof(Onion_Client)); + crypto_memzero((void *_Nonnull)onion_c, sizeof(Onion_Client)); mem_delete(mem, onion_c); } diff --git a/toxcore/onion_client.h b/toxcore/onion_client.h index bdcaf3553c..a12c9733c9 100644 --- a/toxcore/onion_client.h +++ b/toxcore/onion_client.h @@ -212,9 +212,9 @@ Onion_Connection_Status onion_connection_status(const Onion_Client *_Nonnull oni typedef struct Onion_Friend Onion_Friend; uint16_t onion_get_friend_count(const Onion_Client *_Nonnull onion_c); -Onion_Friend *_Nullable onion_get_friend(const Onion_Client *_Nonnull onion_c, uint16_t friend_num); -const uint8_t *_Nullable onion_friend_get_gc_public_key(const Onion_Friend *_Nonnull onion_friend); -const uint8_t *_Nullable onion_friend_get_gc_public_key_num(const Onion_Client *_Nonnull onion_c, uint32_t num); +Onion_Friend *_Nonnull onion_get_friend(const Onion_Client *_Nonnull onion_c, uint16_t friend_num); +const uint8_t *_Nonnull onion_friend_get_gc_public_key(const Onion_Friend *_Nonnull onion_friend); +const uint8_t *_Nonnull onion_friend_get_gc_public_key_num(const Onion_Client *_Nonnull onion_c, uint32_t num); void onion_friend_set_gc_public_key(Onion_Friend *_Nonnull onion_friend, const uint8_t *_Nonnull public_key); void onion_friend_set_gc_data(Onion_Friend *_Nonnull onion_friend, const uint8_t *_Nullable gc_data, uint16_t gc_data_length); bool onion_friend_is_groupchat(const Onion_Friend *_Nonnull onion_friend); diff --git a/toxcore/ping.c b/toxcore/ping.c index 13e4433534..edfd172276 100644 --- a/toxcore/ping.c +++ b/toxcore/ping.c @@ -29,12 +29,12 @@ #define TIME_TO_PING 2 struct Ping { - const Mono_Time *mono_time; - const Random *rng; - const Memory *mem; - DHT *dht; + const Mono_Time *_Nonnull mono_time; + const Random *_Nonnull rng; + const Memory *_Nonnull mem; + DHT *_Nonnull dht; - Ping_Array *ping_array; + Ping_Array *_Nonnull ping_array; Node_format to_ping[MAX_TO_PING]; uint64_t last_to_ping; }; @@ -332,12 +332,13 @@ Ping *ping_new(const Memory *mem, const Mono_Time *mono_time, const Random *rng, return nullptr; } - ping->ping_array = ping_array_new(mem, PING_NUM_MAX, PING_TIMEOUT); + Ping_Array *const ping_array = ping_array_new(mem, PING_NUM_MAX, PING_TIMEOUT); - if (ping->ping_array == nullptr) { + if (ping_array == nullptr) { mem_delete(mem, ping); return nullptr; } + ping->ping_array = ping_array; ping->mono_time = mono_time; ping->rng = rng; diff --git a/toxcore/ping_array.c b/toxcore/ping_array.c index 61e607ccdf..578ca21616 100644 --- a/toxcore/ping_array.c +++ b/toxcore/ping_array.c @@ -17,15 +17,15 @@ #include "mono_time.h" typedef struct Ping_Array_Entry { - uint8_t *data; + uint8_t *_Nullable data; uint32_t length; uint64_t ping_time; uint64_t ping_id; } Ping_Array_Entry; struct Ping_Array { - const Memory *mem; - Ping_Array_Entry *entries; + const Memory *_Nonnull mem; + Ping_Array_Entry *_Nonnull entries; uint32_t last_deleted; /* number representing the next entry to be deleted. */ uint32_t last_added; /* number representing the last entry to be added. */ @@ -50,15 +50,15 @@ Ping_Array *ping_array_new(const Memory *mem, uint32_t size, uint32_t timeout) return nullptr; } - Ping_Array_Entry *entries = (Ping_Array_Entry *)mem_valloc(mem, size, sizeof(Ping_Array_Entry)); + Ping_Array_Entry *const entries = (Ping_Array_Entry *)mem_valloc(mem, size, sizeof(Ping_Array_Entry)); if (entries == nullptr) { mem_delete(mem, empty_array); return nullptr; } + empty_array->entries = entries; empty_array->mem = mem; - empty_array->entries = entries; empty_array->last_deleted = 0; empty_array->last_added = 0; empty_array->total_size = size; @@ -81,7 +81,7 @@ void ping_array_kill(Ping_Array *array) while (array->last_deleted != array->last_added) { const uint32_t index = array->last_deleted % array->total_size; - clear_entry(array, index); + clear_entry((Ping_Array * _Nonnull)array, index); ++array->last_deleted; } @@ -112,7 +112,7 @@ uint64_t ping_array_add(Ping_Array *array, const Mono_Time *mono_time, const Ran if (array->entries[index].data != nullptr) { array->last_deleted = array->last_added - array->total_size; - clear_entry(array, index); + clear_entry((Ping_Array * _Nonnull)array, index); } uint8_t *entry_data = (uint8_t *)mem_balloc(array->mem, length); @@ -169,6 +169,6 @@ int32_t ping_array_check(Ping_Array *array, const Mono_Time *mono_time, uint8_t memcpy(data, array->entries[index].data, array->entries[index].length); const uint32_t len = array->entries[index].length; - clear_entry(array, index); + clear_entry((Ping_Array * _Nonnull)array, index); return len; } diff --git a/toxcore/shared_key_cache.c b/toxcore/shared_key_cache.c index 1e0a82b191..dc442fff5e 100644 --- a/toxcore/shared_key_cache.c +++ b/toxcore/shared_key_cache.c @@ -21,12 +21,12 @@ typedef struct Shared_Key { } Shared_Key; struct Shared_Key_Cache { - Shared_Key *keys; - const uint8_t *self_secret_key; + Shared_Key *_Nonnull keys; + const uint8_t *_Nonnull self_secret_key; uint64_t timeout; /** After this time (in seconds), a key is erased on the next housekeeping cycle */ - const Mono_Time *mono_time; - const Memory *mem; - const Logger *log; + const Mono_Time *_Nonnull mono_time; + const Memory *_Nonnull mem; + const Logger *_Nonnull log; uint8_t keys_per_slot; }; diff --git a/toxcore/tox.c b/toxcore/tox.c index c81741a6a1..b64060959c 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -594,7 +594,7 @@ static State_Load_Status state_load_callback(void *_Nonnull outer, const uint8_t State_Load_Status status = STATE_LOAD_STATUS_CONTINUE; if (messenger_load_state_section(tox->m, data, length, type, &status) - || conferences_load_state_section(tox->m->conferences_object, data, length, type, &status)) { + || conferences_load_state_section((Group_Chats * _Nonnull)tox->m->conferences_object, data, length, type, &status)) { return status; } @@ -669,6 +669,10 @@ static Tox *tox_new_system(const struct Tox_Options *_Nullable options, Tox_Err_ return nullptr; } + const Random *const rng = (const Random * _Nonnull)sys->rng; + const Network *const ns = (const Network * _Nonnull)sys->ns; + const Memory *const mem = (const Memory * _Nonnull)sys->mem; + Messenger_Options m_options = {false}; m_options.dns_enabled = !tox_options_get_experimental_disable_dns(opts); @@ -722,7 +726,7 @@ static Tox *tox_new_system(const struct Tox_Options *_Nullable options, Tox_Err_ m_options.local_discovery_enabled = false; } - Tox *tox = (Tox *)mem_alloc(sys->mem, sizeof(Tox)); + Tox *tox = (Tox *)mem_alloc(mem, sizeof(Tox)); if (tox == nullptr) { SET_ERROR_PARAMETER(error, TOX_ERR_NEW_MALLOC); @@ -753,7 +757,7 @@ static Tox *tox_new_system(const struct Tox_Options *_Nullable options, Tox_Err_ default: { SET_ERROR_PARAMETER(error, TOX_ERR_NEW_PROXY_BAD_TYPE); - mem_delete(sys->mem, tox); + mem_delete(mem, tox); tox_options_free(default_options); return nullptr; } @@ -764,7 +768,7 @@ static Tox *tox_new_system(const struct Tox_Options *_Nullable options, Tox_Err_ if (m_options.proxy_info.proxy_type != TCP_PROXY_NONE) { if (tox_options_get_proxy_port(opts) == 0) { SET_ERROR_PARAMETER(error, TOX_ERR_NEW_PROXY_BAD_PORT); - mem_delete(sys->mem, tox); + mem_delete(mem, tox); tox_options_free(default_options); return nullptr; } @@ -779,10 +783,10 @@ static Tox *tox_new_system(const struct Tox_Options *_Nullable options, Tox_Err_ const bool dns_enabled = !tox_options_get_experimental_disable_dns(opts); if (proxy_host == nullptr - || !addr_resolve_or_parse_ip(tox->sys.ns, tox->sys.mem, proxy_host, &m_options.proxy_info.ip_port.ip, nullptr, dns_enabled)) { + || !addr_resolve_or_parse_ip(ns, mem, proxy_host, &m_options.proxy_info.ip_port.ip, nullptr, dns_enabled)) { SET_ERROR_PARAMETER(error, TOX_ERR_NEW_PROXY_BAD_HOST); // TODO(irungentoo): TOX_ERR_NEW_PROXY_NOT_FOUND if domain. - mem_delete(sys->mem, tox); + mem_delete(mem, tox); tox_options_free(default_options); return nullptr; } @@ -790,21 +794,22 @@ static Tox *tox_new_system(const struct Tox_Options *_Nullable options, Tox_Err_ m_options.proxy_info.ip_port.port = net_htons(tox_options_get_proxy_port(opts)); } - tox->mono_time = mono_time_new(tox->sys.mem, sys->mono_time_callback, sys->mono_time_user_data); + Mono_Time *temp_mono_time = mono_time_new(mem, sys->mono_time_callback, sys->mono_time_user_data); - if (tox->mono_time == nullptr) { + if (temp_mono_time == nullptr) { SET_ERROR_PARAMETER(error, TOX_ERR_NEW_MALLOC); - mem_delete(sys->mem, tox); + mem_delete(mem, tox); tox_options_free(default_options); return nullptr; } + tox->mono_time = temp_mono_time; if (tox_options_get_experimental_thread_safety(opts)) { - pthread_mutex_t *mutex = (pthread_mutex_t *)mem_alloc(sys->mem, sizeof(pthread_mutex_t)); + pthread_mutex_t *mutex = (pthread_mutex_t *)mem_alloc(mem, sizeof(pthread_mutex_t)); if (mutex == nullptr) { SET_ERROR_PARAMETER(error, TOX_ERR_NEW_MALLOC); - mem_delete(sys->mem, tox); + mem_delete(mem, tox); tox_options_free(default_options); return nullptr; } @@ -819,9 +824,9 @@ static Tox *tox_new_system(const struct Tox_Options *_Nullable options, Tox_Err_ tox_lock(tox); Messenger_Error m_error; - tox->m = new_messenger(tox->mono_time, tox->sys.mem, tox->sys.rng, tox->sys.ns, &m_options, &m_error); + Messenger *temp_m = new_messenger(tox->mono_time, mem, rng, ns, &m_options, &m_error); - if (tox->m == nullptr) { + if (temp_m == nullptr) { switch (m_error) { case MESSENGER_ERROR_PORT: case MESSENGER_ERROR_TCP_SERVER: { @@ -835,33 +840,34 @@ static Tox *tox_new_system(const struct Tox_Options *_Nullable options, Tox_Err_ } } - mono_time_free(tox->sys.mem, tox->mono_time); + mono_time_free(mem, tox->mono_time); tox_unlock(tox); if (tox->mutex != nullptr) { pthread_mutex_destroy(tox->mutex); } - mem_delete(sys->mem, tox->mutex); - mem_delete(sys->mem, tox); + mem_delete(mem, tox->mutex); + mem_delete(mem, tox); tox_options_free(default_options); return nullptr; } + tox->m = temp_m; - tox->m->conferences_object = new_groupchats(tox->mono_time, sys->mem, tox->m); + tox->m->conferences_object = new_groupchats(tox->mono_time, mem, tox->m); if (tox->m->conferences_object == nullptr) { kill_messenger(tox->m); - mono_time_free(tox->sys.mem, tox->mono_time); + mono_time_free(mem, tox->mono_time); tox_unlock(tox); if (tox->mutex != nullptr) { pthread_mutex_destroy(tox->mutex); } - mem_delete(sys->mem, tox->mutex); - mem_delete(sys->mem, tox); + mem_delete(mem, tox->mutex); + mem_delete(mem, tox); SET_ERROR_PARAMETER(error, TOX_ERR_NEW_MALLOC); tox_options_free(default_options); @@ -873,15 +879,15 @@ static Tox *tox_new_system(const struct Tox_Options *_Nullable options, Tox_Err_ kill_groupchats(tox->m->conferences_object); kill_messenger(tox->m); - mono_time_free(tox->sys.mem, tox->mono_time); + mono_time_free(mem, tox->mono_time); tox_unlock(tox); if (tox->mutex != nullptr) { pthread_mutex_destroy(tox->mutex); } - mem_delete(sys->mem, tox->mutex); - mem_delete(sys->mem, tox); + mem_delete(mem, tox->mutex); + mem_delete(mem, tox); SET_ERROR_PARAMETER(error, TOX_ERR_NEW_LOAD_BAD_FORMAT); tox_options_free(default_options); @@ -906,12 +912,12 @@ static Tox *tox_new_system(const struct Tox_Options *_Nullable options, Tox_Err_ callback_file_sendrequest(tox->m, tox_file_recv_handler); callback_file_data(tox->m, tox_file_recv_chunk_handler); dht_callback_nodes_response(tox->m->dht, tox_dht_nodes_response_handler); - g_callback_group_invite(tox->m->conferences_object, tox_conference_invite_handler); - g_callback_group_connected(tox->m->conferences_object, tox_conference_connected_handler); - g_callback_group_message(tox->m->conferences_object, tox_conference_message_handler); - g_callback_group_title(tox->m->conferences_object, tox_conference_title_handler); - g_callback_peer_name(tox->m->conferences_object, tox_conference_peer_name_handler); - g_callback_peer_list_changed(tox->m->conferences_object, tox_conference_peer_list_changed_handler); + g_callback_group_invite((Group_Chats * _Nonnull)tox->m->conferences_object, tox_conference_invite_handler); + g_callback_group_connected((Group_Chats * _Nonnull)tox->m->conferences_object, tox_conference_connected_handler); + g_callback_group_message((Group_Chats * _Nonnull)tox->m->conferences_object, tox_conference_message_handler); + g_callback_group_title((Group_Chats * _Nonnull)tox->m->conferences_object, tox_conference_title_handler); + g_callback_peer_name((Group_Chats * _Nonnull)tox->m->conferences_object, tox_conference_peer_name_handler); + g_callback_peer_list_changed((Group_Chats * _Nonnull)tox->m->conferences_object, tox_conference_peer_list_changed_handler); custom_lossy_packet_registerhandler(tox->m, tox_friend_lossy_packet_handler); custom_lossless_packet_registerhandler(tox->m, tox_friend_lossless_packet_handler); @@ -983,15 +989,15 @@ void tox_kill(Tox *tox) LOGGER_ASSERT(tox->m->log, tox->toxav_object == nullptr, "Attempted to kill tox while toxav is still alive"); kill_groupchats(tox->m->conferences_object); kill_messenger(tox->m); - mono_time_free(tox->sys.mem, tox->mono_time); + mono_time_free((const Memory * _Nonnull)tox->sys.mem, tox->mono_time); tox_unlock(tox); if (tox->mutex != nullptr) { pthread_mutex_destroy(tox->mutex); - mem_delete(tox->sys.mem, tox->mutex); + mem_delete((const Memory * _Nonnull)tox->sys.mem, tox->mutex); } - mem_delete(tox->sys.mem, tox); + mem_delete((const Memory * _Nonnull)tox->sys.mem, tox); } static uint32_t end_size(void) @@ -1010,7 +1016,7 @@ size_t tox_get_savedata_size(const Tox *tox) tox_lock(tox); const size_t ret = 2 * sizeof(uint32_t) + messenger_size(tox->m) - + conferences_size(tox->m->conferences_object) + + conferences_size((const Group_Chats * _Nonnull)tox->m->conferences_object) + end_size(); tox_unlock(tox); return ret; @@ -1037,7 +1043,7 @@ void tox_get_savedata(const Tox *tox, uint8_t *savedata) savedata += size32; savedata = messenger_save(tox->m, savedata); - savedata = conferences_save(tox->m->conferences_object, savedata); + savedata = conferences_save((const Group_Chats * _Nonnull)tox->m->conferences_object, savedata); end_save(savedata); tox_unlock(tox); @@ -1059,11 +1065,11 @@ static int32_t resolve_bootstrap_node(Tox *_Nullable tox, const char *_Nullable return -1; } - const int32_t count = net_getipport(tox->sys.ns, tox->sys.mem, host, root, TOX_SOCK_DGRAM, tox->m->options.dns_enabled); + const int32_t count = net_getipport((const Network * _Nonnull)tox->sys.ns, (const Memory * _Nonnull)tox->sys.mem, (const char *_Nonnull)host, root, TOX_SOCK_DGRAM, tox->m->options.dns_enabled); if (count < 1) { - LOGGER_DEBUG(tox->m->log, "could not resolve bootstrap node '%s'", host); - net_freeipport(tox->sys.mem, *root); + LOGGER_DEBUG(tox->m->log, "could not resolve bootstrap node '%s'", (const char *_Nonnull)host); + net_freeipport((const Memory * _Nonnull)tox->sys.mem, *root); SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST); return -1; } @@ -1112,7 +1118,7 @@ bool tox_bootstrap(Tox *tox, const char *host, uint16_t port, const uint8_t publ tox_unlock(tox); - net_freeipport(tox->sys.mem, root); + net_freeipport((const Memory * _Nonnull)tox->sys.mem, root); if (count == 0 || !onion_success || !udp_success) { LOGGER_DEBUG(tox->m->log, "bootstrap node '%s' resolved to %d IP_Ports%s (onion: %s, UDP: %s)", @@ -1149,7 +1155,7 @@ bool tox_add_tcp_relay(Tox *tox, const char *host, uint16_t port, const uint8_t tox_unlock(tox); - net_freeipport(tox->sys.mem, root); + net_freeipport((const Memory * _Nonnull)tox->sys.mem, root); if (count == 0) { SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST); @@ -1211,7 +1217,7 @@ void tox_iterate(Tox *tox, void *user_data) struct Tox_Userdata tox_data = { tox, user_data }; do_messenger(tox->m, &tox_data); - do_groupchats(tox->m->conferences_object, &tox_data); + do_groupchats((Group_Chats * _Nonnull)tox->m->conferences_object, &tox_data); tox_unlock(tox); } @@ -1279,7 +1285,7 @@ bool tox_self_set_name(Tox *tox, const uint8_t *name, size_t length, Tox_Err_Set if (setname(tox->m, name, length) == 0) { // TODO(irungentoo): function to set different per group names? - send_name_all_groups(tox->m->conferences_object); + send_name_all_groups((const Group_Chats * _Nonnull)tox->m->conferences_object); SET_ERROR_PARAMETER(error, TOX_ERR_SET_INFO_OK); tox_unlock(tox); return true; @@ -1999,7 +2005,7 @@ uint32_t tox_file_send(Tox *tox, uint32_t friend_number, uint32_t kind, uint64_t if (file_id == nullptr) { /* Tox keys are 32 bytes like FILE_ID_LENGTH. */ - new_symmetric_key(tox->sys.rng, f_id); + new_symmetric_key((const Random * _Nonnull)tox->sys.rng, f_id); file_id = f_id; } @@ -2154,7 +2160,7 @@ uint32_t tox_conference_new(Tox *tox, Tox_Err_Conference_New *error) { assert(tox != nullptr); tox_lock(tox); - const int ret = add_groupchat(tox->m->conferences_object, tox->sys.rng, GROUPCHAT_TYPE_TEXT); + const int ret = add_groupchat((Group_Chats * _Nonnull)tox->m->conferences_object, (const Random * _Nonnull)tox->sys.rng, GROUPCHAT_TYPE_TEXT); tox_unlock(tox); if (ret == -1) { @@ -2170,7 +2176,7 @@ bool tox_conference_delete(Tox *tox, uint32_t conference_number, Tox_Err_Confere { assert(tox != nullptr); tox_lock(tox); - const bool ret = del_groupchat(tox->m->conferences_object, conference_number, true); + const bool ret = del_groupchat((Group_Chats * _Nonnull)tox->m->conferences_object, conference_number, true); tox_unlock(tox); if (!ret) { @@ -2186,7 +2192,7 @@ uint32_t tox_conference_peer_count(const Tox *tox, uint32_t conference_number, T { assert(tox != nullptr); tox_lock(tox); - const int ret = group_number_peers(tox->m->conferences_object, conference_number, false); + const int ret = group_number_peers((const Group_Chats * _Nonnull)tox->m->conferences_object, conference_number, false); tox_unlock(tox); if (ret == -1) { @@ -2203,7 +2209,7 @@ size_t tox_conference_peer_get_name_size(const Tox *tox, uint32_t conference_num { assert(tox != nullptr); tox_lock(tox); - const int ret = group_peername_size(tox->m->conferences_object, conference_number, peer_number, false); + const int ret = group_peername_size((const Group_Chats * _Nonnull)tox->m->conferences_object, conference_number, peer_number, false); tox_unlock(tox); switch (ret) { @@ -2227,7 +2233,7 @@ bool tox_conference_peer_get_name(const Tox *tox, uint32_t conference_number, ui { assert(tox != nullptr); tox_lock(tox); - const int ret = group_peername(tox->m->conferences_object, conference_number, peer_number, name, false); + const int ret = group_peername((const Group_Chats * _Nonnull)tox->m->conferences_object, conference_number, peer_number, name, false); tox_unlock(tox); switch (ret) { @@ -2251,7 +2257,7 @@ bool tox_conference_peer_get_public_key(const Tox *tox, uint32_t conference_numb { assert(tox != nullptr); tox_lock(tox); - const int ret = group_peer_pubkey(tox->m->conferences_object, conference_number, peer_number, public_key, false); + const int ret = group_peer_pubkey((const Group_Chats * _Nonnull)tox->m->conferences_object, conference_number, peer_number, public_key, false); tox_unlock(tox); switch (ret) { @@ -2275,7 +2281,7 @@ bool tox_conference_peer_number_is_ours(const Tox *tox, uint32_t conference_numb { assert(tox != nullptr); tox_lock(tox); - const int ret = group_peernumber_is_ours(tox->m->conferences_object, conference_number, peer_number); + const int ret = group_peernumber_is_ours((const Group_Chats * _Nonnull)tox->m->conferences_object, conference_number, peer_number); tox_unlock(tox); switch (ret) { @@ -2304,7 +2310,7 @@ uint32_t tox_conference_offline_peer_count(const Tox *tox, uint32_t conference_n { assert(tox != nullptr); tox_lock(tox); - const int ret = group_number_peers(tox->m->conferences_object, conference_number, true); + const int ret = group_number_peers((const Group_Chats * _Nonnull)tox->m->conferences_object, conference_number, true); tox_unlock(tox); if (ret == -1) { @@ -2322,7 +2328,7 @@ size_t tox_conference_offline_peer_get_name_size(const Tox *tox, uint32_t confer { assert(tox != nullptr); tox_lock(tox); - const int ret = group_peername_size(tox->m->conferences_object, conference_number, offline_peer_number, true); + const int ret = group_peername_size((const Group_Chats * _Nonnull)tox->m->conferences_object, conference_number, offline_peer_number, true); tox_unlock(tox); switch (ret) { @@ -2347,7 +2353,7 @@ bool tox_conference_offline_peer_get_name(const Tox *tox, uint32_t conference_nu { assert(tox != nullptr); tox_lock(tox); - const int ret = group_peername(tox->m->conferences_object, conference_number, offline_peer_number, name, true); + const int ret = group_peername((const Group_Chats * _Nonnull)tox->m->conferences_object, conference_number, offline_peer_number, name, true); tox_unlock(tox); switch (ret) { @@ -2372,7 +2378,7 @@ bool tox_conference_offline_peer_get_public_key(const Tox *tox, uint32_t confere { assert(tox != nullptr); tox_lock(tox); - const int ret = group_peer_pubkey(tox->m->conferences_object, conference_number, offline_peer_number, public_key, true); + const int ret = group_peer_pubkey((const Group_Chats * _Nonnull)tox->m->conferences_object, conference_number, offline_peer_number, public_key, true); tox_unlock(tox); switch (ret) { @@ -2398,7 +2404,7 @@ uint64_t tox_conference_offline_peer_get_last_active(const Tox *tox, uint32_t co assert(tox != nullptr); uint64_t last_active = UINT64_MAX; tox_lock(tox); - const int ret = group_frozen_last_active(tox->m->conferences_object, conference_number, offline_peer_number, + const int ret = group_frozen_last_active((const Group_Chats * _Nonnull)tox->m->conferences_object, conference_number, offline_peer_number, &last_active); tox_unlock(tox); @@ -2424,7 +2430,7 @@ bool tox_conference_set_max_offline(Tox *tox, uint32_t conference_number, { assert(tox != nullptr); tox_lock(tox); - const int ret = group_set_max_frozen(tox->m->conferences_object, conference_number, max_offline); + const int ret = group_set_max_frozen((const Group_Chats * _Nonnull)tox->m->conferences_object, conference_number, max_offline); tox_unlock(tox); if (ret == -1) { @@ -2441,7 +2447,7 @@ bool tox_conference_invite(Tox *tox, uint32_t friend_number, uint32_t conference { assert(tox != nullptr); tox_lock(tox); - const int ret = invite_friend(tox->m->conferences_object, friend_number, conference_number); + const int ret = invite_friend((const Group_Chats * _Nonnull)tox->m->conferences_object, friend_number, conference_number); tox_unlock(tox); switch (ret) { @@ -2476,7 +2482,7 @@ uint32_t tox_conference_join(Tox *tox, uint32_t friend_number, const uint8_t *co } tox_lock(tox); - const int ret = join_groupchat(tox->m->conferences_object, friend_number, GROUPCHAT_TYPE_TEXT, cookie, length); + const int ret = join_groupchat((Group_Chats * _Nonnull)tox->m->conferences_object, friend_number, GROUPCHAT_TYPE_TEXT, cookie, length); tox_unlock(tox); switch (ret) { @@ -2523,9 +2529,9 @@ bool tox_conference_send_message(Tox *tox, uint32_t conference_number, Tox_Messa int ret = 0; if (type == TOX_MESSAGE_TYPE_NORMAL) { - ret = group_message_send(tox->m->conferences_object, conference_number, message, length); + ret = group_message_send((const Group_Chats * _Nonnull)tox->m->conferences_object, conference_number, message, length); } else { - ret = group_action_send(tox->m->conferences_object, conference_number, message, length); + ret = group_action_send((const Group_Chats * _Nonnull)tox->m->conferences_object, conference_number, message, length); } tox_unlock(tox); @@ -2560,7 +2566,7 @@ size_t tox_conference_get_title_size(const Tox *tox, uint32_t conference_number, { assert(tox != nullptr); tox_lock(tox); - const int ret = group_title_get_size(tox->m->conferences_object, conference_number); + const int ret = group_title_get_size((const Group_Chats * _Nonnull)tox->m->conferences_object, conference_number); tox_unlock(tox); switch (ret) { @@ -2584,7 +2590,7 @@ bool tox_conference_get_title(const Tox *tox, uint32_t conference_number, uint8_ { assert(tox != nullptr); tox_lock(tox); - const int ret = group_title_get(tox->m->conferences_object, conference_number, title); + const int ret = group_title_get((const Group_Chats * _Nonnull)tox->m->conferences_object, conference_number, title); tox_unlock(tox); switch (ret) { @@ -2608,7 +2614,7 @@ bool tox_conference_set_title(Tox *tox, uint32_t conference_number, const uint8_ { assert(tox != nullptr); tox_lock(tox); - const int ret = group_title_send(tox->m->conferences_object, conference_number, title, length); + const int ret = group_title_send((const Group_Chats * _Nonnull)tox->m->conferences_object, conference_number, title, length); tox_unlock(tox); switch (ret) { @@ -2636,7 +2642,7 @@ size_t tox_conference_get_chatlist_size(const Tox *tox) { assert(tox != nullptr); tox_lock(tox); - const size_t ret = count_chatlist(tox->m->conferences_object); + const size_t ret = count_chatlist((const Group_Chats * _Nonnull)tox->m->conferences_object); tox_unlock(tox); return ret; } @@ -2645,8 +2651,8 @@ void tox_conference_get_chatlist(const Tox *tox, uint32_t *chatlist) { assert(tox != nullptr); tox_lock(tox); - const size_t list_size = count_chatlist(tox->m->conferences_object); - copy_chatlist(tox->m->conferences_object, chatlist, list_size); + const size_t list_size = count_chatlist((const Group_Chats * _Nonnull)tox->m->conferences_object); + copy_chatlist((const Group_Chats * _Nonnull)tox->m->conferences_object, chatlist, list_size); tox_unlock(tox); } @@ -2655,7 +2661,7 @@ Tox_Conference_Type tox_conference_get_type(const Tox *tox, uint32_t conference_ { assert(tox != nullptr); tox_lock(tox); - const int ret = group_get_type(tox->m->conferences_object, conference_number); + const int ret = group_get_type((const Group_Chats * _Nonnull)tox->m->conferences_object, conference_number); tox_unlock(tox); if (ret == -1) { @@ -2671,7 +2677,7 @@ bool tox_conference_get_id(const Tox *tox, uint32_t conference_number, uint8_t i { assert(tox != nullptr); tox_lock(tox); - const bool ret = conference_get_id(tox->m->conferences_object, conference_number, id); + const bool ret = conference_get_id((const Group_Chats * _Nonnull)tox->m->conferences_object, conference_number, id); tox_unlock(tox); return ret; } @@ -2693,7 +2699,7 @@ uint32_t tox_conference_by_id(const Tox *tox, const uint8_t id[TOX_CONFERENCE_ID } tox_lock(tox); - const int32_t ret = conference_by_id(tox->m->conferences_object, id); + const int32_t ret = conference_by_id((const Group_Chats * _Nonnull)tox->m->conferences_object, id); tox_unlock(tox); if (ret == -1) { @@ -4641,7 +4647,7 @@ bool tox_group_kick_peer(const Tox *tox, uint32_t group_number, uint32_t peer_id return false; } -const Tox_System *tox_get_system(Tox *tox) +const Tox_System *tox_get_system(const Tox *tox) { assert(tox != nullptr); return &tox->sys; diff --git a/toxcore/tox_dispatch.c b/toxcore/tox_dispatch.c index 04e0d9345e..0015bfcfcc 100644 --- a/toxcore/tox_dispatch.c +++ b/toxcore/tox_dispatch.c @@ -14,46 +14,46 @@ #include "tox_events.h" struct Tox_Dispatch { - tox_events_conference_connected_cb *conference_connected_callback; - tox_events_conference_invite_cb *conference_invite_callback; - tox_events_conference_message_cb *conference_message_callback; - tox_events_conference_peer_list_changed_cb *conference_peer_list_changed_callback; - tox_events_conference_peer_name_cb *conference_peer_name_callback; - tox_events_conference_title_cb *conference_title_callback; - tox_events_file_chunk_request_cb *file_chunk_request_callback; - tox_events_file_recv_cb *file_recv_callback; - tox_events_file_recv_chunk_cb *file_recv_chunk_callback; - tox_events_file_recv_control_cb *file_recv_control_callback; - tox_events_friend_connection_status_cb *friend_connection_status_callback; - tox_events_friend_lossless_packet_cb *friend_lossless_packet_callback; - tox_events_friend_lossy_packet_cb *friend_lossy_packet_callback; - tox_events_friend_message_cb *friend_message_callback; - tox_events_friend_name_cb *friend_name_callback; - tox_events_friend_read_receipt_cb *friend_read_receipt_callback; - tox_events_friend_request_cb *friend_request_callback; - tox_events_friend_status_cb *friend_status_callback; - tox_events_friend_status_message_cb *friend_status_message_callback; - tox_events_friend_typing_cb *friend_typing_callback; - tox_events_self_connection_status_cb *self_connection_status_callback; - tox_events_group_peer_name_cb *group_peer_name_callback; - tox_events_group_peer_status_cb *group_peer_status_callback; - tox_events_group_topic_cb *group_topic_callback; - tox_events_group_privacy_state_cb *group_privacy_state_callback; - tox_events_group_voice_state_cb *group_voice_state_callback; - tox_events_group_topic_lock_cb *group_topic_lock_callback; - tox_events_group_peer_limit_cb *group_peer_limit_callback; - tox_events_group_password_cb *group_password_callback; - tox_events_group_message_cb *group_message_callback; - tox_events_group_private_message_cb *group_private_message_callback; - tox_events_group_custom_packet_cb *group_custom_packet_callback; - tox_events_group_custom_private_packet_cb *group_custom_private_packet_callback; - tox_events_group_invite_cb *group_invite_callback; - tox_events_group_peer_join_cb *group_peer_join_callback; - tox_events_group_peer_exit_cb *group_peer_exit_callback; - tox_events_group_self_join_cb *group_self_join_callback; - tox_events_group_join_fail_cb *group_join_fail_callback; - tox_events_group_moderation_cb *group_moderation_callback; - tox_events_dht_nodes_response_cb *dht_nodes_response_callback; + tox_events_conference_connected_cb *_Nullable conference_connected_callback; + tox_events_conference_invite_cb *_Nullable conference_invite_callback; + tox_events_conference_message_cb *_Nullable conference_message_callback; + tox_events_conference_peer_list_changed_cb *_Nullable conference_peer_list_changed_callback; + tox_events_conference_peer_name_cb *_Nullable conference_peer_name_callback; + tox_events_conference_title_cb *_Nullable conference_title_callback; + tox_events_file_chunk_request_cb *_Nullable file_chunk_request_callback; + tox_events_file_recv_cb *_Nullable file_recv_callback; + tox_events_file_recv_chunk_cb *_Nullable file_recv_chunk_callback; + tox_events_file_recv_control_cb *_Nullable file_recv_control_callback; + tox_events_friend_connection_status_cb *_Nullable friend_connection_status_callback; + tox_events_friend_lossless_packet_cb *_Nullable friend_lossless_packet_callback; + tox_events_friend_lossy_packet_cb *_Nullable friend_lossy_packet_callback; + tox_events_friend_message_cb *_Nullable friend_message_callback; + tox_events_friend_name_cb *_Nullable friend_name_callback; + tox_events_friend_read_receipt_cb *_Nullable friend_read_receipt_callback; + tox_events_friend_request_cb *_Nullable friend_request_callback; + tox_events_friend_status_cb *_Nullable friend_status_callback; + tox_events_friend_status_message_cb *_Nullable friend_status_message_callback; + tox_events_friend_typing_cb *_Nullable friend_typing_callback; + tox_events_self_connection_status_cb *_Nullable self_connection_status_callback; + tox_events_group_peer_name_cb *_Nullable group_peer_name_callback; + tox_events_group_peer_status_cb *_Nullable group_peer_status_callback; + tox_events_group_topic_cb *_Nullable group_topic_callback; + tox_events_group_privacy_state_cb *_Nullable group_privacy_state_callback; + tox_events_group_voice_state_cb *_Nullable group_voice_state_callback; + tox_events_group_topic_lock_cb *_Nullable group_topic_lock_callback; + tox_events_group_peer_limit_cb *_Nullable group_peer_limit_callback; + tox_events_group_password_cb *_Nullable group_password_callback; + tox_events_group_message_cb *_Nullable group_message_callback; + tox_events_group_private_message_cb *_Nullable group_private_message_callback; + tox_events_group_custom_packet_cb *_Nullable group_custom_packet_callback; + tox_events_group_custom_private_packet_cb *_Nullable group_custom_private_packet_callback; + tox_events_group_invite_cb *_Nullable group_invite_callback; + tox_events_group_peer_join_cb *_Nullable group_peer_join_callback; + tox_events_group_peer_exit_cb *_Nullable group_peer_exit_callback; + tox_events_group_self_join_cb *_Nullable group_self_join_callback; + tox_events_group_join_fail_cb *_Nullable group_join_fail_callback; + tox_events_group_moderation_cb *_Nullable group_moderation_callback; + tox_events_dht_nodes_response_cb *_Nullable dht_nodes_response_callback; }; Tox_Dispatch *tox_dispatch_new(Tox_Err_Dispatch_New *error) diff --git a/toxcore/tox_event.c b/toxcore/tox_event.c index faa7be4ee2..f405f7a453 100644 --- a/toxcore/tox_event.c +++ b/toxcore/tox_event.c @@ -587,124 +587,124 @@ static bool tox_event_data_pack(Tox_Event_Type type, const Tox_Event_Data *_Nonn { switch (type) { case TOX_EVENT_CONFERENCE_CONNECTED: - return tox_event_conference_connected_pack(data->conference_connected, bp); + return tox_event_conference_connected_pack((const Tox_Event_Conference_Connected * _Nonnull)data->conference_connected, bp); case TOX_EVENT_CONFERENCE_INVITE: - return tox_event_conference_invite_pack(data->conference_invite, bp); + return tox_event_conference_invite_pack((const Tox_Event_Conference_Invite * _Nonnull)data->conference_invite, bp); case TOX_EVENT_CONFERENCE_MESSAGE: - return tox_event_conference_message_pack(data->conference_message, bp); + return tox_event_conference_message_pack((const Tox_Event_Conference_Message * _Nonnull)data->conference_message, bp); case TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED: - return tox_event_conference_peer_list_changed_pack(data->conference_peer_list_changed, bp); + return tox_event_conference_peer_list_changed_pack((const Tox_Event_Conference_Peer_List_Changed * _Nonnull)data->conference_peer_list_changed, bp); case TOX_EVENT_CONFERENCE_PEER_NAME: - return tox_event_conference_peer_name_pack(data->conference_peer_name, bp); + return tox_event_conference_peer_name_pack((const Tox_Event_Conference_Peer_Name * _Nonnull)data->conference_peer_name, bp); case TOX_EVENT_CONFERENCE_TITLE: - return tox_event_conference_title_pack(data->conference_title, bp); + return tox_event_conference_title_pack((const Tox_Event_Conference_Title * _Nonnull)data->conference_title, bp); case TOX_EVENT_FILE_CHUNK_REQUEST: - return tox_event_file_chunk_request_pack(data->file_chunk_request, bp); + return tox_event_file_chunk_request_pack((const Tox_Event_File_Chunk_Request * _Nonnull)data->file_chunk_request, bp); case TOX_EVENT_FILE_RECV_CHUNK: - return tox_event_file_recv_chunk_pack(data->file_recv_chunk, bp); + return tox_event_file_recv_chunk_pack((const Tox_Event_File_Recv_Chunk * _Nonnull)data->file_recv_chunk, bp); case TOX_EVENT_FILE_RECV_CONTROL: - return tox_event_file_recv_control_pack(data->file_recv_control, bp); + return tox_event_file_recv_control_pack((const Tox_Event_File_Recv_Control * _Nonnull)data->file_recv_control, bp); case TOX_EVENT_FILE_RECV: - return tox_event_file_recv_pack(data->file_recv, bp); + return tox_event_file_recv_pack((const Tox_Event_File_Recv * _Nonnull)data->file_recv, bp); case TOX_EVENT_FRIEND_CONNECTION_STATUS: - return tox_event_friend_connection_status_pack(data->friend_connection_status, bp); + return tox_event_friend_connection_status_pack((const Tox_Event_Friend_Connection_Status * _Nonnull)data->friend_connection_status, bp); case TOX_EVENT_FRIEND_LOSSLESS_PACKET: - return tox_event_friend_lossless_packet_pack(data->friend_lossless_packet, bp); + return tox_event_friend_lossless_packet_pack((const Tox_Event_Friend_Lossless_Packet * _Nonnull)data->friend_lossless_packet, bp); case TOX_EVENT_FRIEND_LOSSY_PACKET: - return tox_event_friend_lossy_packet_pack(data->friend_lossy_packet, bp); + return tox_event_friend_lossy_packet_pack((const Tox_Event_Friend_Lossy_Packet * _Nonnull)data->friend_lossy_packet, bp); case TOX_EVENT_FRIEND_MESSAGE: - return tox_event_friend_message_pack(data->friend_message, bp); + return tox_event_friend_message_pack((const Tox_Event_Friend_Message * _Nonnull)data->friend_message, bp); case TOX_EVENT_FRIEND_NAME: - return tox_event_friend_name_pack(data->friend_name, bp); + return tox_event_friend_name_pack((const Tox_Event_Friend_Name * _Nonnull)data->friend_name, bp); case TOX_EVENT_FRIEND_READ_RECEIPT: - return tox_event_friend_read_receipt_pack(data->friend_read_receipt, bp); + return tox_event_friend_read_receipt_pack((const Tox_Event_Friend_Read_Receipt * _Nonnull)data->friend_read_receipt, bp); case TOX_EVENT_FRIEND_REQUEST: - return tox_event_friend_request_pack(data->friend_request, bp); + return tox_event_friend_request_pack((const Tox_Event_Friend_Request * _Nonnull)data->friend_request, bp); case TOX_EVENT_FRIEND_STATUS: - return tox_event_friend_status_pack(data->friend_status, bp); + return tox_event_friend_status_pack((const Tox_Event_Friend_Status * _Nonnull)data->friend_status, bp); case TOX_EVENT_FRIEND_STATUS_MESSAGE: - return tox_event_friend_status_message_pack(data->friend_status_message, bp); + return tox_event_friend_status_message_pack((const Tox_Event_Friend_Status_Message * _Nonnull)data->friend_status_message, bp); case TOX_EVENT_FRIEND_TYPING: - return tox_event_friend_typing_pack(data->friend_typing, bp); + return tox_event_friend_typing_pack((const Tox_Event_Friend_Typing * _Nonnull)data->friend_typing, bp); case TOX_EVENT_SELF_CONNECTION_STATUS: - return tox_event_self_connection_status_pack(data->self_connection_status, bp); + return tox_event_self_connection_status_pack((const Tox_Event_Self_Connection_Status * _Nonnull)data->self_connection_status, bp); case TOX_EVENT_GROUP_PEER_NAME: - return tox_event_group_peer_name_pack(data->group_peer_name, bp); + return tox_event_group_peer_name_pack((const Tox_Event_Group_Peer_Name * _Nonnull)data->group_peer_name, bp); case TOX_EVENT_GROUP_PEER_STATUS: - return tox_event_group_peer_status_pack(data->group_peer_status, bp); + return tox_event_group_peer_status_pack((const Tox_Event_Group_Peer_Status * _Nonnull)data->group_peer_status, bp); case TOX_EVENT_GROUP_TOPIC: - return tox_event_group_topic_pack(data->group_topic, bp); + return tox_event_group_topic_pack((const Tox_Event_Group_Topic * _Nonnull)data->group_topic, bp); case TOX_EVENT_GROUP_PRIVACY_STATE: - return tox_event_group_privacy_state_pack(data->group_privacy_state, bp); + return tox_event_group_privacy_state_pack((const Tox_Event_Group_Privacy_State * _Nonnull)data->group_privacy_state, bp); case TOX_EVENT_GROUP_VOICE_STATE: - return tox_event_group_voice_state_pack(data->group_voice_state, bp); + return tox_event_group_voice_state_pack((const Tox_Event_Group_Voice_State * _Nonnull)data->group_voice_state, bp); case TOX_EVENT_GROUP_TOPIC_LOCK: - return tox_event_group_topic_lock_pack(data->group_topic_lock, bp); + return tox_event_group_topic_lock_pack((const Tox_Event_Group_Topic_Lock * _Nonnull)data->group_topic_lock, bp); case TOX_EVENT_GROUP_PEER_LIMIT: - return tox_event_group_peer_limit_pack(data->group_peer_limit, bp); + return tox_event_group_peer_limit_pack((const Tox_Event_Group_Peer_Limit * _Nonnull)data->group_peer_limit, bp); case TOX_EVENT_GROUP_PASSWORD: - return tox_event_group_password_pack(data->group_password, bp); + return tox_event_group_password_pack((const Tox_Event_Group_Password * _Nonnull)data->group_password, bp); case TOX_EVENT_GROUP_MESSAGE: - return tox_event_group_message_pack(data->group_message, bp); + return tox_event_group_message_pack((const Tox_Event_Group_Message * _Nonnull)data->group_message, bp); case TOX_EVENT_GROUP_PRIVATE_MESSAGE: - return tox_event_group_private_message_pack(data->group_private_message, bp); + return tox_event_group_private_message_pack((const Tox_Event_Group_Private_Message * _Nonnull)data->group_private_message, bp); case TOX_EVENT_GROUP_CUSTOM_PACKET: - return tox_event_group_custom_packet_pack(data->group_custom_packet, bp); + return tox_event_group_custom_packet_pack((const Tox_Event_Group_Custom_Packet * _Nonnull)data->group_custom_packet, bp); case TOX_EVENT_GROUP_CUSTOM_PRIVATE_PACKET: - return tox_event_group_custom_private_packet_pack(data->group_custom_private_packet, bp); + return tox_event_group_custom_private_packet_pack((const Tox_Event_Group_Custom_Private_Packet * _Nonnull)data->group_custom_private_packet, bp); case TOX_EVENT_GROUP_INVITE: - return tox_event_group_invite_pack(data->group_invite, bp); + return tox_event_group_invite_pack((const Tox_Event_Group_Invite * _Nonnull)data->group_invite, bp); case TOX_EVENT_GROUP_PEER_JOIN: - return tox_event_group_peer_join_pack(data->group_peer_join, bp); + return tox_event_group_peer_join_pack((const Tox_Event_Group_Peer_Join * _Nonnull)data->group_peer_join, bp); case TOX_EVENT_GROUP_PEER_EXIT: - return tox_event_group_peer_exit_pack(data->group_peer_exit, bp); + return tox_event_group_peer_exit_pack((const Tox_Event_Group_Peer_Exit * _Nonnull)data->group_peer_exit, bp); case TOX_EVENT_GROUP_SELF_JOIN: - return tox_event_group_self_join_pack(data->group_self_join, bp); + return tox_event_group_self_join_pack((const Tox_Event_Group_Self_Join * _Nonnull)data->group_self_join, bp); case TOX_EVENT_GROUP_JOIN_FAIL: - return tox_event_group_join_fail_pack(data->group_join_fail, bp); + return tox_event_group_join_fail_pack((const Tox_Event_Group_Join_Fail * _Nonnull)data->group_join_fail, bp); case TOX_EVENT_GROUP_MODERATION: - return tox_event_group_moderation_pack(data->group_moderation, bp); + return tox_event_group_moderation_pack((const Tox_Event_Group_Moderation * _Nonnull)data->group_moderation, bp); case TOX_EVENT_DHT_NODES_RESPONSE: - return tox_event_dht_nodes_response_pack(data->dht_nodes_response, bp); + return tox_event_dht_nodes_response_pack((const Tox_Event_Dht_Nodes_Response * _Nonnull)data->dht_nodes_response, bp); case TOX_EVENT_INVALID: return false; diff --git a/toxcore/tox_events.c b/toxcore/tox_events.c index 05aae5a54f..5d5f8821c6 100644 --- a/toxcore/tox_events.c +++ b/toxcore/tox_events.c @@ -91,7 +91,7 @@ const Tox_Event *tox_events_get(const Tox_Events *events, uint32_t index) Tox_Events *tox_events_iterate(Tox *tox, bool fail_hard, Tox_Err_Events_Iterate *error) { const Tox_System *sys = tox_get_system(tox); - Tox_Events_State state = {TOX_ERR_EVENTS_ITERATE_OK, sys->mem}; + Tox_Events_State state = {TOX_ERR_EVENTS_ITERATE_OK, (const Memory * _Nonnull)sys->mem}; tox_iterate(tox, &state); if (error != nullptr) { @@ -158,7 +158,7 @@ static bool tox_events_unpack_handler(void *_Nonnull obj, Bin_Unpack *_Nonnull b Tox_Events *tox_events_load(const Tox_System *sys, const uint8_t *bytes, uint32_t bytes_size) { - Tox_Events *events = (Tox_Events *)mem_alloc(sys->mem, sizeof(Tox_Events)); + Tox_Events *events = (Tox_Events *)mem_alloc((const Memory * _Nonnull)sys->mem, sizeof(Tox_Events)); if (events == nullptr) { return nullptr; @@ -167,9 +167,9 @@ Tox_Events *tox_events_load(const Tox_System *sys, const uint8_t *bytes, uint32_ *events = (Tox_Events) { nullptr }; - events->mem = sys->mem; + events->mem = (const Memory * _Nonnull)sys->mem; - if (!bin_unpack_obj(sys->mem, tox_events_unpack_handler, events, bytes, bytes_size)) { + if (!bin_unpack_obj((const Memory * _Nonnull)sys->mem, tox_events_unpack_handler, events, bytes, bytes_size)) { tox_events_free(events); return nullptr; } @@ -189,12 +189,12 @@ bool tox_events_equal(const Tox_System *sys, const Tox_Events *a, const Tox_Even return false; } - uint8_t *a_bytes = (uint8_t *)mem_balloc(sys->mem, a_size); - uint8_t *b_bytes = (uint8_t *)mem_balloc(sys->mem, b_size); + uint8_t *a_bytes = (uint8_t *)mem_balloc((const Memory * _Nonnull)sys->mem, a_size); + uint8_t *b_bytes = (uint8_t *)mem_balloc((const Memory * _Nonnull)sys->mem, b_size); if (a_bytes == nullptr || b_bytes == nullptr) { - mem_delete(sys->mem, b_bytes); - mem_delete(sys->mem, a_bytes); + mem_delete((const Memory * _Nonnull)sys->mem, b_bytes); + mem_delete((const Memory * _Nonnull)sys->mem, a_bytes); return false; } @@ -203,8 +203,8 @@ bool tox_events_equal(const Tox_System *sys, const Tox_Events *a, const Tox_Even const bool ret = memcmp(a_bytes, b_bytes, a_size) == 0; - mem_delete(sys->mem, b_bytes); - mem_delete(sys->mem, a_bytes); + mem_delete((const Memory * _Nonnull)sys->mem, b_bytes); + mem_delete((const Memory * _Nonnull)sys->mem, a_bytes); return ret; } diff --git a/toxcore/tox_events_test.cc b/toxcore/tox_events_test.cc index 749d6768f3..9d9fced7e5 100644 --- a/toxcore/tox_events_test.cc +++ b/toxcore/tox_events_test.cc @@ -15,7 +15,7 @@ TEST(ToxEvents, UnpackRandomDataDoesntCrash) const Tox_System sys = tox_default_system(); ASSERT_NE(sys.rng, nullptr); std::array data; - random_bytes(sys.rng, data.data(), data.size()); + random_bytes(static_cast(sys.rng), data.data(), data.size()); tox_events_free(tox_events_load(&sys, data.data(), data.size())); } diff --git a/toxcore/tox_options.h b/toxcore/tox_options.h index 7d8b7aafaa..f90e186f36 100644 --- a/toxcore/tox_options.h +++ b/toxcore/tox_options.h @@ -239,12 +239,13 @@ struct Tox_Options { */ void *log_user_data; - /** + /* * These options are experimental, so avoid writing code that depends on * them. Options marked "experimental" may change their behaviour or go away * entirely in the future, or may be renamed to something non-experimental * if they become part of the supported API. */ + /** * Make public API functions thread-safe using a per-instance lock. * diff --git a/toxcore/tox_private.c b/toxcore/tox_private.c index e87072e5cf..d8c02d98f6 100644 --- a/toxcore/tox_private.c +++ b/toxcore/tox_private.c @@ -127,11 +127,11 @@ bool tox_dht_send_nodes_request(const Tox *tox, const uint8_t *public_key, const IP_Port *root; - const int32_t count = net_getipport(tox->sys.ns, tox->sys.mem, ip, &root, TOX_SOCK_DGRAM, tox->m->options.dns_enabled); + const int32_t count = net_getipport((const Network * _Nonnull)tox->sys.ns, (const Memory * _Nonnull)tox->sys.mem, ip, &root, TOX_SOCK_DGRAM, tox->m->options.dns_enabled); if (count < 1) { SET_ERROR_PARAMETER(error, TOX_ERR_DHT_SEND_NODES_REQUEST_BAD_IP); - net_freeipport(tox->sys.mem, root); + net_freeipport((const Memory * _Nonnull)tox->sys.mem, root); tox_unlock(tox); return false; } @@ -148,7 +148,7 @@ bool tox_dht_send_nodes_request(const Tox *tox, const uint8_t *public_key, const tox_unlock(tox); - net_freeipport(tox->sys.mem, root); + net_freeipport((const Memory * _Nonnull)tox->sys.mem, root); if (!success) { SET_ERROR_PARAMETER(error, TOX_ERR_DHT_SEND_NODES_REQUEST_FAIL); diff --git a/toxcore/tox_private.h b/toxcore/tox_private.h index 4cc3edd00c..e0d4a81c97 100644 --- a/toxcore/tox_private.h +++ b/toxcore/tox_private.h @@ -10,6 +10,7 @@ #include #include +#include "attributes.h" #include "tox.h" #include "tox_options.h" @@ -17,22 +18,22 @@ extern "C" { #endif -typedef uint64_t tox_mono_time_cb(void *user_data); +typedef uint64_t tox_mono_time_cb(void *_Nullable user_data); typedef struct Tox_System { - tox_mono_time_cb *mono_time_callback; - void *mono_time_user_data; - const struct Random *rng; - const struct Network *ns; - const struct Memory *mem; + tox_mono_time_cb *_Nullable mono_time_callback; + void *_Nullable mono_time_user_data; + const struct Random *_Nullable rng; + const struct Network *_Nullable ns; + const struct Memory *_Nullable mem; } Tox_System; Tox_System tox_default_system(void); -const Tox_System *tox_get_system(Tox *tox); +const Tox_System *_Nonnull tox_get_system(const Tox *_Nonnull tox); typedef struct Tox_Options_Testing { - const struct Tox_System *operating_system; + const struct Tox_System *_Nullable operating_system; } Tox_Options_Testing; typedef enum Tox_Err_New_Testing { @@ -40,10 +41,10 @@ typedef enum Tox_Err_New_Testing { TOX_ERR_NEW_TESTING_NULL, } Tox_Err_New_Testing; -Tox *tox_new_testing(const Tox_Options *options, Tox_Err_New *error, const Tox_Options_Testing *testing, Tox_Err_New_Testing *testing_error); +Tox *_Nullable tox_new_testing(const Tox_Options *_Nonnull options, Tox_Err_New *_Nullable error, const Tox_Options_Testing *_Nonnull testing, Tox_Err_New_Testing *_Nullable testing_error); -void tox_lock(const Tox *tox); -void tox_unlock(const Tox *tox); +void tox_lock(const Tox *_Nonnull tox); +void tox_unlock(const Tox *_Nonnull tox); /** * Set the callback for the `friend_lossy_packet` event for a specific packet @@ -53,7 +54,7 @@ void tox_unlock(const Tox *tox); * from `PACKET_ID_RANGE_LOSSY_START` to `PACKET_ID_RANGE_LOSSY_END` (both * inclusive) */ -void tox_callback_friend_lossy_packet_per_pktid(Tox *tox, tox_friend_lossy_packet_cb *callback, uint8_t pktid); +void tox_callback_friend_lossy_packet_per_pktid(Tox *_Nonnull tox, tox_friend_lossy_packet_cb *_Nullable callback, uint8_t pktid); /** * Set the callback for the `friend_lossless_packet` event for a specific packet @@ -63,10 +64,10 @@ void tox_callback_friend_lossy_packet_per_pktid(Tox *tox, tox_friend_lossy_packe * from `PACKET_ID_RANGE_LOSSLESS_CUSTOM_START` to * `PACKET_ID_RANGE_LOSSLESS_CUSTOM_END` (both inclusive) and `PACKET_ID_MSI` */ -void tox_callback_friend_lossless_packet_per_pktid(Tox *tox, tox_friend_lossless_packet_cb *callback, uint8_t pktid); +void tox_callback_friend_lossless_packet_per_pktid(Tox *_Nonnull tox, tox_friend_lossless_packet_cb *_Nullable callback, uint8_t pktid); -void tox_set_av_object(Tox *tox, void *object); -void *tox_get_av_object(const Tox *tox); +void tox_set_av_object(Tox *_Nonnull tox, void *_Nullable object); +void *_Nullable tox_get_av_object(const Tox *_Nonnull tox); /******************************************************************************* * @@ -94,15 +95,15 @@ uint32_t tox_dht_node_public_key_size(void); * @param port The node's port. */ typedef void tox_dht_nodes_response_cb( - Tox *tox, const uint8_t *public_key, const char *ip, uint32_t ip_length, - uint16_t port, void *user_data); + Tox *_Nonnull tox, const uint8_t *_Nonnull public_key, const char *_Nonnull ip, uint32_t ip_length, + uint16_t port, void *_Nullable user_data); /** * Set the callback for the `dht_nodes_response` event. Pass NULL to unset. * * This event is triggered when a nodes response is received from a DHT peer. */ -void tox_callback_dht_nodes_response(Tox *tox, tox_dht_nodes_response_cb *callback); +void tox_callback_dht_nodes_response(Tox *_Nonnull tox, tox_dht_nodes_response_cb *_Nullable callback); typedef enum Tox_Err_Dht_Send_Nodes_Request { /** @@ -153,15 +154,15 @@ typedef enum Tox_Err_Dht_Send_Nodes_Request { * * @return true on success. */ -bool tox_dht_send_nodes_request(const Tox *tox, const uint8_t *public_key, const char *ip, uint16_t port, - const uint8_t *target_public_key, Tox_Err_Dht_Send_Nodes_Request *error); +bool tox_dht_send_nodes_request(const Tox *_Nonnull tox, const uint8_t *_Nonnull public_key, const char *_Nonnull ip, uint16_t port, + const uint8_t *_Nonnull target_public_key, Tox_Err_Dht_Send_Nodes_Request *_Nullable error); /** * This function returns the number of DHT nodes in the closelist. * * @return number */ -uint16_t tox_dht_get_num_closelist(const Tox *tox); +uint16_t tox_dht_get_num_closelist(const Tox *_Nonnull tox); /** * This function returns the number of DHT nodes in the closelist @@ -169,7 +170,7 @@ uint16_t tox_dht_get_num_closelist(const Tox *tox); * * @return number */ -uint16_t tox_dht_get_num_closelist_announce_capable(const Tox *tox); +uint16_t tox_dht_get_num_closelist_announce_capable(const Tox *_Nonnull tox); /******************************************************************************* * @@ -347,7 +348,7 @@ typedef enum Tox_Netprof_Packet_Id { TOX_NETPROF_PACKET_ID_BOOTSTRAP_INFO = 0xf0, } Tox_Netprof_Packet_Id; -const char *tox_netprof_packet_id_to_string(Tox_Netprof_Packet_Id value); +const char *_Nonnull tox_netprof_packet_id_to_string(Tox_Netprof_Packet_Id value); /** * Specifies the packet type for a given query. @@ -374,7 +375,7 @@ typedef enum Tox_Netprof_Packet_Type { TOX_NETPROF_PACKET_TYPE_UDP, } Tox_Netprof_Packet_Type; -const char *tox_netprof_packet_type_to_string(Tox_Netprof_Packet_Type value); +const char *_Nonnull tox_netprof_packet_type_to_string(Tox_Netprof_Packet_Type value); /** * Specifies the packet direction for a given query. @@ -391,7 +392,7 @@ typedef enum Tox_Netprof_Direction { TOX_NETPROF_DIRECTION_RECV, } Tox_Netprof_Direction; -const char *tox_netprof_direction_to_string(Tox_Netprof_Direction value); +const char *_Nonnull tox_netprof_direction_to_string(Tox_Netprof_Direction value); /** * Return the number of packets sent or received for a specific packet ID. @@ -400,7 +401,7 @@ const char *tox_netprof_direction_to_string(Tox_Netprof_Direction value); * @param id The packet ID being queried. * @param direction The packet direction. */ -uint64_t tox_netprof_get_packet_id_count(const Tox *tox, Tox_Netprof_Packet_Type type, uint8_t id, +uint64_t tox_netprof_get_packet_id_count(const Tox *_Nonnull tox, Tox_Netprof_Packet_Type type, uint8_t id, Tox_Netprof_Direction direction); /** @@ -409,7 +410,7 @@ uint64_t tox_netprof_get_packet_id_count(const Tox *tox, Tox_Netprof_Packet_Type * @param type The types of packets being queried. * @param direction The packet direction. */ -uint64_t tox_netprof_get_packet_total_count(const Tox *tox, Tox_Netprof_Packet_Type type, +uint64_t tox_netprof_get_packet_total_count(const Tox *_Nonnull tox, Tox_Netprof_Packet_Type type, Tox_Netprof_Direction direction); /** @@ -419,7 +420,7 @@ uint64_t tox_netprof_get_packet_total_count(const Tox *tox, Tox_Netprof_Packet_T * @param id The packet ID being queried. * @param direction The packet direction. */ -uint64_t tox_netprof_get_packet_id_bytes(const Tox *tox, Tox_Netprof_Packet_Type type, uint8_t id, +uint64_t tox_netprof_get_packet_id_bytes(const Tox *_Nonnull tox, Tox_Netprof_Packet_Type type, uint8_t id, Tox_Netprof_Direction direction); /** @@ -428,7 +429,7 @@ uint64_t tox_netprof_get_packet_id_bytes(const Tox *tox, Tox_Netprof_Packet_Type * @param type The types of packets being queried. * @param direction The packet direction. */ -uint64_t tox_netprof_get_packet_total_bytes(const Tox *tox, Tox_Netprof_Packet_Type type, +uint64_t tox_netprof_get_packet_total_bytes(const Tox *_Nonnull tox, Tox_Netprof_Packet_Type type, Tox_Netprof_Direction direction); @@ -453,8 +454,8 @@ uint32_t tox_group_peer_ip_string_max_length(void); * @param peer_id The ID of the peer whose IP address length we want to * retrieve. */ -size_t tox_group_peer_get_ip_address_size(const Tox *tox, uint32_t group_number, uint32_t peer_id, - Tox_Err_Group_Peer_Query *error); +size_t tox_group_peer_get_ip_address_size(const Tox *_Nonnull tox, uint32_t group_number, uint32_t peer_id, + Tox_Err_Group_Peer_Query *_Nullable error); /** * Write the IP address associated with the designated peer_id for the * designated group number to ip_addr. @@ -475,8 +476,8 @@ size_t tox_group_peer_get_ip_address_size(const Tox *tox, uint32_t group_number, * * @return true on success. */ -bool tox_group_peer_get_ip_address(const Tox *tox, uint32_t group_number, uint32_t peer_id, uint8_t *ip_addr, - Tox_Err_Group_Peer_Query *error); +bool tox_group_peer_get_ip_address(const Tox *_Nonnull tox, uint32_t group_number, uint32_t peer_id, uint8_t *_Nonnull ip_addr, + Tox_Err_Group_Peer_Query *_Nullable error); #ifdef __cplusplus } /* extern "C" */ diff --git a/toxcore/tox_struct.h b/toxcore/tox_struct.h index 9383c2a509..078a9b3769 100644 --- a/toxcore/tox_struct.h +++ b/toxcore/tox_struct.h @@ -18,54 +18,54 @@ extern "C" { #endif struct Tox { - struct Messenger *m; - Mono_Time *mono_time; + struct Messenger *_Nonnull m; + Mono_Time *_Nonnull mono_time; Tox_System sys; - pthread_mutex_t *mutex; + pthread_mutex_t *_Nullable mutex; - tox_log_cb *log_callback; - tox_self_connection_status_cb *self_connection_status_callback; - tox_friend_name_cb *friend_name_callback; - tox_friend_status_message_cb *friend_status_message_callback; - tox_friend_status_cb *friend_status_callback; - tox_friend_connection_status_cb *friend_connection_status_callback; - tox_friend_typing_cb *friend_typing_callback; - tox_friend_read_receipt_cb *friend_read_receipt_callback; - tox_friend_request_cb *friend_request_callback; - tox_friend_message_cb *friend_message_callback; - tox_file_recv_control_cb *file_recv_control_callback; - tox_file_chunk_request_cb *file_chunk_request_callback; - tox_file_recv_cb *file_recv_callback; - tox_file_recv_chunk_cb *file_recv_chunk_callback; - tox_conference_invite_cb *conference_invite_callback; - tox_conference_connected_cb *conference_connected_callback; - tox_conference_message_cb *conference_message_callback; - tox_conference_title_cb *conference_title_callback; - tox_conference_peer_name_cb *conference_peer_name_callback; - tox_conference_peer_list_changed_cb *conference_peer_list_changed_callback; - tox_dht_nodes_response_cb *dht_nodes_response_callback; - tox_friend_lossy_packet_cb *friend_lossy_packet_callback_per_pktid[UINT8_MAX + 1]; - tox_friend_lossless_packet_cb *friend_lossless_packet_callback_per_pktid[UINT8_MAX + 1]; - tox_group_peer_name_cb *group_peer_name_callback; - tox_group_peer_status_cb *group_peer_status_callback; - tox_group_topic_cb *group_topic_callback; - tox_group_privacy_state_cb *group_privacy_state_callback; - tox_group_topic_lock_cb *group_topic_lock_callback; - tox_group_voice_state_cb *group_voice_state_callback; - tox_group_peer_limit_cb *group_peer_limit_callback; - tox_group_password_cb *group_password_callback; - tox_group_message_cb *group_message_callback; - tox_group_private_message_cb *group_private_message_callback; - tox_group_custom_packet_cb *group_custom_packet_callback; - tox_group_custom_private_packet_cb *group_custom_private_packet_callback; - tox_group_invite_cb *group_invite_callback; - tox_group_peer_join_cb *group_peer_join_callback; - tox_group_peer_exit_cb *group_peer_exit_callback; - tox_group_self_join_cb *group_self_join_callback; - tox_group_join_fail_cb *group_join_fail_callback; - tox_group_moderation_cb *group_moderation_callback; + tox_log_cb *_Nullable log_callback; + tox_self_connection_status_cb *_Nullable self_connection_status_callback; + tox_friend_name_cb *_Nullable friend_name_callback; + tox_friend_status_message_cb *_Nullable friend_status_message_callback; + tox_friend_status_cb *_Nullable friend_status_callback; + tox_friend_connection_status_cb *_Nullable friend_connection_status_callback; + tox_friend_typing_cb *_Nullable friend_typing_callback; + tox_friend_read_receipt_cb *_Nullable friend_read_receipt_callback; + tox_friend_request_cb *_Nullable friend_request_callback; + tox_friend_message_cb *_Nullable friend_message_callback; + tox_file_recv_control_cb *_Nullable file_recv_control_callback; + tox_file_chunk_request_cb *_Nullable file_chunk_request_callback; + tox_file_recv_cb *_Nullable file_recv_callback; + tox_file_recv_chunk_cb *_Nullable file_recv_chunk_callback; + tox_conference_invite_cb *_Nullable conference_invite_callback; + tox_conference_connected_cb *_Nullable conference_connected_callback; + tox_conference_message_cb *_Nullable conference_message_callback; + tox_conference_title_cb *_Nullable conference_title_callback; + tox_conference_peer_name_cb *_Nullable conference_peer_name_callback; + tox_conference_peer_list_changed_cb *_Nullable conference_peer_list_changed_callback; + tox_dht_nodes_response_cb *_Nullable dht_nodes_response_callback; + tox_friend_lossy_packet_cb *_Nullable friend_lossy_packet_callback_per_pktid[UINT8_MAX + 1]; + tox_friend_lossless_packet_cb *_Nullable friend_lossless_packet_callback_per_pktid[UINT8_MAX + 1]; + tox_group_peer_name_cb *_Nullable group_peer_name_callback; + tox_group_peer_status_cb *_Nullable group_peer_status_callback; + tox_group_topic_cb *_Nullable group_topic_callback; + tox_group_privacy_state_cb *_Nullable group_privacy_state_callback; + tox_group_topic_lock_cb *_Nullable group_topic_lock_callback; + tox_group_voice_state_cb *_Nullable group_voice_state_callback; + tox_group_peer_limit_cb *_Nullable group_peer_limit_callback; + tox_group_password_cb *_Nullable group_password_callback; + tox_group_message_cb *_Nullable group_message_callback; + tox_group_private_message_cb *_Nullable group_private_message_callback; + tox_group_custom_packet_cb *_Nullable group_custom_packet_callback; + tox_group_custom_private_packet_cb *_Nullable group_custom_private_packet_callback; + tox_group_invite_cb *_Nullable group_invite_callback; + tox_group_peer_join_cb *_Nullable group_peer_join_callback; + tox_group_peer_exit_cb *_Nullable group_peer_exit_callback; + tox_group_self_join_cb *_Nullable group_self_join_callback; + tox_group_join_fail_cb *_Nullable group_join_fail_callback; + tox_group_moderation_cb *_Nullable group_moderation_callback; - void *toxav_object; // workaround to store a ToxAV object (setter and getter functions are available) + void *_Nullable toxav_object; // workaround to store a ToxAV object (setter and getter functions are available) }; #ifdef __cplusplus diff --git a/toxencryptsave/toxencryptsave.c b/toxencryptsave/toxencryptsave.c index 63bda86058..b0e7309df7 100644 --- a/toxencryptsave/toxencryptsave.c +++ b/toxencryptsave/toxencryptsave.c @@ -231,7 +231,7 @@ bool tox_pass_key_encrypt(const Tox_Pass_Key *key, const uint8_t plaintext[], si ciphertext += crypto_box_NONCEBYTES; /* now encrypt */ - const int32_t encrypted_len = encrypt_data_symmetric(os_memory(), key->key, nonce, plaintext, plaintext_len, ciphertext); + const int32_t encrypted_len = encrypt_data_symmetric((const Memory * _Nonnull)os_memory(), key->key, nonce, plaintext, plaintext_len, ciphertext); if (encrypted_len < 0 || (size_t)encrypted_len != plaintext_len + crypto_box_MACBYTES) { SET_ERROR_PARAMETER(error, TOX_ERR_ENCRYPTION_FAILED); return false; @@ -316,7 +316,7 @@ bool tox_pass_key_decrypt(const Tox_Pass_Key *key, const uint8_t ciphertext[], s ciphertext += crypto_box_NONCEBYTES; /* decrypt the ciphertext */ - const int32_t decrypted_len = decrypt_data_symmetric(os_memory(), key->key, nonce, ciphertext, decrypt_length + crypto_box_MACBYTES, plaintext); + const int32_t decrypted_len = decrypt_data_symmetric((const Memory * _Nonnull)os_memory(), key->key, nonce, ciphertext, decrypt_length + crypto_box_MACBYTES, plaintext); if (decrypted_len < 0 || (size_t)decrypted_len != decrypt_length) { SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_FAILED); return false;