diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0b832d3778..65a051ae01 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -17,7 +17,7 @@ jobs: strategy: fail-fast: false matrix: - tool: [autotools, clang-tidy, compcert, cppcheck, doxygen, goblint, infer, freebsd, misra, modules, pkgsrc, rpm, slimcc, sparse, tcc, tokstyle] + tool: [autotools, cake, clang-tidy, compcert, cppcheck, doxygen, goblint, infer, freebsd, misra, modules, pkgsrc, rpm, slimcc, sparse, tcc, tokstyle] runs-on: ubuntu-22.04 steps: - name: Set up Docker Buildx diff --git a/other/docker/cake/cake.Dockerfile b/other/docker/cake/cake.Dockerfile new file mode 100644 index 0000000000..b01687e27e --- /dev/null +++ b/other/docker/cake/cake.Dockerfile @@ -0,0 +1,75 @@ +FROM alpine:3.19.0 AS cake + +RUN ["apk", "add", "--no-cache", \ + "clang", \ + "compiler-rt", \ + "gdb", \ + "git", \ + "libsodium-dev", \ + "libvpx-dev", \ + "linux-headers", \ + "llvm", \ + "musl-dev", \ + "opus-dev", \ + "util-linux-dev"] + +WORKDIR /src/workspace/cake +ARG CAKE_COMMIT="aee9bdec15f62db185f6f35ff3879f2d6d00321d" +RUN ["git", "clone", "https://github.com/thradams/cake", "/src/workspace/cake"] +RUN git checkout "$CAKE_COMMIT" + +WORKDIR /src/workspace/cake/src +RUN sed -i \ + -e 's/ -Wall / -DNDEBUG -std=gnu2x -Wall -Wno-pointer-sign -Wno-multichar -Wno-int-conversion -Wno-unused-but-set-variable -Wno-incompatible-pointer-types-discards-qualifiers -Wno-unused-function -Wno-unused-variable -Wno-unused-label -Wno-return-type -Werror -static -ggdb3 /' \ + -e 's/RUN "amalgamator.exe/"echo amalgamator.exe/' \ + build.c \ + && clang -DDEBUG build.c -o build \ + && ./build + +# ENV CAKEFLAGS="-D__x86_64__ -I/src/workspace/cake/src/include -I/src/workspace/cake/src -I/usr/include" +ENV CAKEFLAGS="-D__x86_64__ -I/src/workspace/cake/src/include -I/src/workspace/cake/src -I/usr/include -fanalyzer" + +WORKDIR /src/workspace/c-toxcore +COPY . /src/workspace/c-toxcore/ + +RUN for i in toxcore/*.c; do \ + echo "$i"; \ + OUT="$(/src/workspace/cake/src/cake $CAKEFLAGS "$i")"; \ + echo "$OUT"; \ + if echo "$OUT" | grep "error:" >/dev/null; then exit 1; fi; \ + if echo "$OUT" | grep "warning:" >/dev/null; then exit 1; fi; \ + if echo "$OUT" | grep " 0 files" >/dev/null; then exit 1; fi; \ + done + +# For creduce: +#FROM ubuntu:22.04 +# +#ENV DEBIAN_FRONTEND="noninteractive" +#RUN apt-get update \ +# && apt-get install -y --no-install-recommends \ +# creduce \ +# gdb \ +# libopus-dev \ +# libsodium-dev \ +# libvpx-dev \ +# linux-libc-dev \ +# musl-dev \ +# && apt-get clean \ +# && rm -rf /var/lib/apt/lists/* +# +#SHELL ["/bin/bash", "-o", "pipefail", "-c"] +# +#COPY --from=cake /src/workspace/cake/src/ /src/workspace/cake/src/ +# +#WORKDIR /src/workspace/c-toxcore +#COPY . /src/workspace/c-toxcore/ +# +#ENV CAKEFLAGS="-Wno-conditional-constant -D__x86_64__ -I/src/workspace/cake/src/include -I/src/workspace/cake/src -I/usr/include/x86_64-linux-musl -I/usr/include" +# +##RUN /src/workspace/cake/src/cake $CAKEFLAGS -E toxcore/DHT.c | grep -Ev '^(Cake|/| [01])' >crash.c +#RUN cp toxcore/test.c crash.c +#RUN other/docker/cake/creduce.sh +#RUN creduce other/docker/cake/creduce.sh crash.c \ +# || creduce other/docker/cake/creduce.sh crash.c +# +#RUN gdb -ex r -ex bt --args /src/workspace/cake/src/cake -fanalyzer crash.c diff --git a/other/docker/cake/cake.Dockerfile.dockerignore b/other/docker/cake/cake.Dockerfile.dockerignore new file mode 100644 index 0000000000..71cc148807 --- /dev/null +++ b/other/docker/cake/cake.Dockerfile.dockerignore @@ -0,0 +1,25 @@ +# ===== common ===== +# Ignore everything ... +**/* +# ... except sources +!**/*.[ch] +!**/*.cc +!**/*.hh +!CHANGELOG.md +!LICENSE +!README.md +!auto_tests/data/* +!other/bootstrap_daemon/bash-completion/** +!other/bootstrap_daemon/tox-bootstrapd.* +!other/proxy/*.mod +!other/proxy/*.sum +!other/proxy/*.go +# ... and CMake build files (used by most builds). +!**/CMakeLists.txt +!.github/scripts/flags*.sh +!cmake/*.cmake +!other/pkgconfig/* +!other/rpm/* +!so.version +# ===== custom ===== +!other/docker/cake/creduce.sh diff --git a/other/docker/cake/creduce.sh b/other/docker/cake/creduce.sh new file mode 100755 index 0000000000..ea132355ab --- /dev/null +++ b/other/docker/cake/creduce.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +WARNINGS=(-std=c99 -Wall -Wno-return-type -Wno-unused -Werror) + +if ! gcc "${WARNINGS[@]}" -I/src/workspace/c-toxcore/toxcore -I/usr/include/x86_64-linux-musl -fsyntax-only crash.c; then + exit 1 +fi +/src/workspace/cake/src/cake -I/src/workspace/c-toxcore/toxcore -I/src/workspace/cake/src/include -I/src/workspace/cake/src -fanalyzer crash.c +if [ $? != 139 ]; then + exit 1 +fi diff --git a/other/docker/cake/dockerignore b/other/docker/cake/dockerignore new file mode 100644 index 0000000000..690efd47e5 --- /dev/null +++ b/other/docker/cake/dockerignore @@ -0,0 +1,2 @@ +# ===== custom ===== +!other/docker/cake/creduce.sh diff --git a/other/docker/cake/run b/other/docker/cake/run new file mode 100755 index 0000000000..5a0f34bc84 --- /dev/null +++ b/other/docker/cake/run @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +. "$(cd "$(dirname "${BASH_SOURCE[0]}")/../sources" && pwd)/run.sh" diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 630b6c4899..5c1be8bcd8 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -111,10 +111,10 @@ 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 *owner friends_list; uint16_t num_friends; - Node_format *loaded_nodes_list; + Node_format *owner loaded_nodes_list; uint32_t loaded_num_nodes; unsigned int loaded_nodes_index; @@ -371,7 +371,7 @@ int dht_create_packet(const Memory *mem, const Random *rng, uint8_t *packet, size_t length) { uint8_t nonce[CRYPTO_NONCE_SIZE]; - uint8_t *encrypted = (uint8_t *)mem_balloc(mem, plain_length + CRYPTO_MAC_SIZE); + uint8_t *owner encrypted = (uint8_t *owner)mem_balloc(mem, plain_length + CRYPTO_MAC_SIZE); if (encrypted == nullptr) { return -1; @@ -1637,13 +1637,14 @@ int dht_addfriend(DHT *dht, const uint8_t *public_key, dht_ip_cb *ip_callback, return 0; } - DHT_Friend *const temp = (DHT_Friend *)mem_vrealloc(dht->mem, dht->friends_list, dht->num_friends + 1, sizeof(DHT_Friend)); + bool ok; + dht->friends_list = + (DHT_Friend *owner)mem_vresize(dht->mem, dht->friends_list, dht->num_friends + 1, sizeof(DHT_Friend), &ok); - if (temp == nullptr) { + if (!ok) { return -1; } - dht->friends_list = temp; DHT_Friend *const dht_friend = &dht->friends_list[dht->num_friends]; *dht_friend = empty_dht_friend; memcpy(dht_friend->public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE); @@ -1687,13 +1688,14 @@ int dht_delfriend(DHT *dht, const uint8_t *public_key, uint32_t lock_token) return 0; } - DHT_Friend *const temp = (DHT_Friend *)mem_vrealloc(dht->mem, dht->friends_list, dht->num_friends, sizeof(DHT_Friend)); + bool ok; + dht->friends_list = + (DHT_Friend *owner)mem_vresize(dht->mem, dht->friends_list, dht->num_friends, sizeof(DHT_Friend), &ok); - if (temp == nullptr) { + if (!ok) { return -1; } - dht->friends_list = temp; return 0; } @@ -1740,8 +1742,10 @@ static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, co const uint64_t temp_time = mono_time_get(dht->mono_time); uint32_t num_nodes = 0; - Client_data **client_list = (Client_data **)mem_valloc(dht->mem, list_count * 2, sizeof(Client_data *)); - IPPTsPng **assoc_list = (IPPTsPng **)mem_valloc(dht->mem, list_count * 2, sizeof(IPPTsPng *)); + Client_data **owner client_list = + (Client_data **owner)mem_valloc(dht->mem, list_count * 2, sizeof(Client_data *)); + IPPTsPng **owner assoc_list = + (IPPTsPng **owner)mem_valloc(dht->mem, list_count * 2, sizeof(IPPTsPng *)); unsigned int sort = 0; bool sort_ok = false; @@ -2559,15 +2563,16 @@ static int handle_lan_discovery(void *object, const IP_Port *source, const uint8 /*----------------------------------------------------------------------------------*/ -DHT *new_dht(const Logger *log, const Memory *mem, const Random *rng, const Network *ns, - Mono_Time *mono_time, Networking_Core *net, - bool hole_punching_enabled, bool lan_discovery_enabled) +DHT *owner new_dht( + const Logger *log, const Memory *mem, const Random *rng, const Network *ns, + Mono_Time *mono_time, Networking_Core *net, + bool hole_punching_enabled, bool lan_discovery_enabled) { if (net == nullptr) { return nullptr; } - DHT *const dht = (DHT *)mem_alloc(mem, sizeof(DHT)); + DHT *const owner dht = (DHT *owner)mem_alloc(mem, sizeof(DHT)); if (dht == nullptr) { LOGGER_ERROR(log, "failed to allocate DHT struct (%ld bytes)", (unsigned long)sizeof(DHT)); @@ -2666,7 +2671,7 @@ void do_dht(DHT *dht) ping_iterate(dht->ping); } -void kill_dht(DHT *dht) +void kill_dht(DHT *owner dht) { if (dht == nullptr) { return; @@ -2739,7 +2744,8 @@ void dht_save(const DHT *dht, uint8_t *data) /* get right offset. we write the actual header later. */ data = state_write_section_header(data, DHT_STATE_COOKIE_TYPE, 0, 0); - Node_format *clients = (Node_format *)mem_valloc(dht->mem, MAX_SAVED_DHT_NODES, sizeof(Node_format)); + Node_format *owner clients = + (Node_format *owner)mem_valloc(dht->mem, MAX_SAVED_DHT_NODES, sizeof(Node_format)); if (clients == nullptr) { LOGGER_ERROR(dht->log, "could not allocate %u nodes", MAX_SAVED_DHT_NODES); @@ -2836,7 +2842,8 @@ static State_Load_Status dht_load_state_callback(void *outer, const uint8_t *dat mem_delete(dht->mem, dht->loaded_nodes_list); // Copy to loaded_clients_list - Node_format *nodes = (Node_format *)mem_valloc(dht->mem, MAX_SAVED_DHT_NODES, sizeof(Node_format)); + Node_format *owner nodes = + (Node_format *owner)mem_valloc(dht->mem, MAX_SAVED_DHT_NODES, sizeof(Node_format)); if (nodes == nullptr) { LOGGER_ERROR(dht->log, "could not allocate %u nodes", MAX_SAVED_DHT_NODES); diff --git a/toxcore/DHT.h b/toxcore/DHT.h index 8e0e5e703c..b1bc830692 100644 --- a/toxcore/DHT.h +++ b/toxcore/DHT.h @@ -464,11 +464,12 @@ int dht_load(DHT *dht, const uint8_t *data, uint32_t length); /** Initialize DHT. */ non_null() -DHT *new_dht(const Logger *log, const Memory *mem, const Random *rng, const Network *ns, - Mono_Time *mono_time, Networking_Core *net, bool hole_punching_enabled, bool lan_discovery_enabled); +DHT *owner new_dht( + const Logger *log, const Memory *mem, const Random *rng, const Network *ns, + Mono_Time *mono_time, Networking_Core *net, bool hole_punching_enabled, bool lan_discovery_enabled); nullable(1) -void kill_dht(DHT *dht); +void kill_dht(DHT *owner dht); /** * @retval false if we are not connected to the DHT. diff --git a/toxcore/LAN_discovery.c b/toxcore/LAN_discovery.c index fcc3d4793f..4dbf31bbaf 100644 --- a/toxcore/LAN_discovery.c +++ b/toxcore/LAN_discovery.c @@ -54,9 +54,9 @@ struct Broadcast_Info { #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) non_null() -static Broadcast_Info *fetch_broadcast_info(const Memory *mem, const Network *ns) +static Broadcast_Info *owner fetch_broadcast_info(const Memory *mem, const Network *ns) { - Broadcast_Info *broadcast = (Broadcast_Info *)mem_alloc(mem, sizeof(Broadcast_Info)); + Broadcast_Info *owner broadcast = (Broadcast_Info *owner)mem_alloc(mem, sizeof(Broadcast_Info)); if (broadcast == nullptr) { return nullptr; @@ -64,7 +64,7 @@ static Broadcast_Info *fetch_broadcast_info(const Memory *mem, const Network *ns broadcast->mem = mem; - IP_ADAPTER_INFO *adapter_info = (IP_ADAPTER_INFO *)mem_balloc(mem, sizeof(IP_ADAPTER_INFO)); + IP_ADAPTER_INFO *owner adapter_info = (IP_ADAPTER_INFO *owner)mem_balloc(mem, sizeof(IP_ADAPTER_INFO)); if (adapter_info == nullptr) { mem_delete(mem, broadcast); @@ -75,7 +75,7 @@ static Broadcast_Info *fetch_broadcast_info(const Memory *mem, const Network *ns if (GetAdaptersInfo(adapter_info, &out_buf_len) == ERROR_BUFFER_OVERFLOW) { mem_delete(mem, adapter_info); - IP_ADAPTER_INFO *new_adapter_info = (IP_ADAPTER_INFO *)mem_balloc(mem, out_buf_len); + IP_ADAPTER_INFO *owner new_adapter_info = (IP_ADAPTER_INFO *owner)mem_balloc(mem, out_buf_len); if (new_adapter_info == nullptr) { mem_delete(mem, broadcast); @@ -128,9 +128,9 @@ non_null() static bool ip4_is_local(const IP4 *ip4); non_null() -static Broadcast_Info *fetch_broadcast_info(const Memory *mem, const Network *ns) +static Broadcast_Info *owner fetch_broadcast_info(const Memory *mem, const Network *ns) { - Broadcast_Info *broadcast = (Broadcast_Info *)mem_alloc(mem, sizeof(Broadcast_Info)); + Broadcast_Info *owner broadcast = (Broadcast_Info *owner)mem_alloc(mem, sizeof(Broadcast_Info)); if (broadcast == nullptr) { return nullptr; @@ -223,9 +223,9 @@ static Broadcast_Info *fetch_broadcast_info(const Memory *mem, const Network *ns #else // TODO(irungentoo): Other platforms? non_null() -static Broadcast_Info *fetch_broadcast_info(const Memory *mem, const Network *ns) +static Broadcast_Info *owner fetch_broadcast_info(const Memory *mem, const Network *ns) { - Broadcast_Info *broadcast = (Broadcast_Info *)mem_alloc(mem, sizeof(Broadcast_Info)); + Broadcast_Info *broadcast = (Broadcast_Info *owner)mem_alloc(mem, sizeof(Broadcast_Info)); if (broadcast == nullptr) { return nullptr; @@ -409,12 +409,12 @@ bool lan_discovery_send(const Networking_Core *net, const Broadcast_Info *broadc return res; } -Broadcast_Info *lan_discovery_init(const Memory *mem, const Network *ns) +Broadcast_Info *owner lan_discovery_init(const Memory *mem, const Network *ns) { return fetch_broadcast_info(mem, ns); } -void lan_discovery_kill(Broadcast_Info *broadcast) +void lan_discovery_kill(Broadcast_Info *owner broadcast) { if (broadcast == nullptr) { return; diff --git a/toxcore/LAN_discovery.h b/toxcore/LAN_discovery.h index c18ce9e562..699527a64f 100644 --- a/toxcore/LAN_discovery.h +++ b/toxcore/LAN_discovery.h @@ -33,13 +33,13 @@ bool lan_discovery_send(const Networking_Core *net, const Broadcast_Info *broadc * Discovers broadcast devices and IP addresses. */ non_null() -Broadcast_Info *lan_discovery_init(const Memory *mem, const Network *ns); +Broadcast_Info *owner lan_discovery_init(const Memory *mem, const Network *ns); /** * Free all resources associated with the broadcast info. */ nullable(1) -void lan_discovery_kill(Broadcast_Info *broadcast); +void lan_discovery_kill(Broadcast_Info *owner broadcast); /** * Is IP a local ip or not. diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index e7f8ee9cee..88cfc86083 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -58,7 +58,7 @@ bool friend_is_valid(const Messenger *m, int32_t friendnumber) /** @brief Set the size of the friend list to numfriends. * - * @retval -1 if mem_vrealloc fails. + * @retval -1 if mem_vresize fails. */ non_null() static int realloc_friendlist(Messenger *m, uint32_t num) @@ -69,13 +69,13 @@ static int realloc_friendlist(Messenger *m, uint32_t num) return 0; } - Friend *newfriendlist = (Friend *)mem_vrealloc(m->mem, m->friendlist, num, sizeof(Friend)); + bool ok; + m->friendlist = (Friend *owner)mem_vresize(m->mem, m->friendlist, num, sizeof(Friend), &ok); - if (newfriendlist == nullptr) { + if (!ok) { return -1; } - m->friendlist = newfriendlist; return 0; } @@ -326,10 +326,10 @@ static int clear_receipts(Messenger *m, int32_t friendnumber) return -1; } - struct Receipts *receipts = m->friendlist[friendnumber].receipts_start; + struct Receipts *owner receipts = m->friendlist[friendnumber].receipts_start; while (receipts != nullptr) { - struct Receipts *temp_r = receipts->next; + struct Receipts *owner temp_r = receipts->next; mem_delete(m->mem, receipts); receipts = temp_r; } @@ -346,7 +346,7 @@ static int add_receipt(Messenger *m, int32_t friendnumber, uint32_t packet_num, return -1; } - struct Receipts *new_receipts = (struct Receipts *)mem_alloc(m->mem, sizeof(struct Receipts)); + struct Receipts *owner new_receipts = (struct Receipts *owner)mem_alloc(m->mem, sizeof(struct Receipts)); if (new_receipts == nullptr) { return -1; @@ -434,7 +434,7 @@ static int do_receipts(Messenger *m, int32_t friendnumber, void *userdata) return -1; } - struct Receipts *receipts = m->friendlist[friendnumber].receipts_start; + struct Receipts *owner receipts = m->friendlist[friendnumber].receipts_start; while (receipts != nullptr) { if (friend_received_packet(m, friendnumber, receipts->packet_num) == -1) { @@ -445,7 +445,7 @@ static int do_receipts(Messenger *m, int32_t friendnumber, void *userdata) m->read_receipt(m, friendnumber, receipts->msg_id, userdata); } - struct Receipts *r_next = receipts->next; + struct Receipts *owner r_next = receipts->next; mem_delete(m->mem, receipts); @@ -2859,14 +2859,14 @@ bool m_register_state_plugin(Messenger *m, State_Type type, m_state_size_cb *siz m_state_save_cb *save_callback) { const uint32_t new_length = m->options.state_plugins_length + 1; - Messenger_State_Plugin *temp = (Messenger_State_Plugin *)mem_vrealloc( - m->mem, m->options.state_plugins, new_length, sizeof(Messenger_State_Plugin)); + bool ok; + m->options.state_plugins = (Messenger_State_Plugin *owner)mem_vresize( + m->mem, m->options.state_plugins, new_length, sizeof(Messenger_State_Plugin), &ok); - if (temp == nullptr) { + if (!ok) { return false; } - m->options.state_plugins = temp; m->options.state_plugins_length = new_length; const uint8_t index = m->options.state_plugins_length - 1; @@ -3453,8 +3453,9 @@ static void m_handle_friend_request( * * if error is not NULL it will be set to one of the values in the enum above. */ -Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random *rng, const Network *ns, - Messenger_Options *options, Messenger_Error *error) +Messenger *owner new_messenger( + Mono_Time *mono_time, const Memory *mem, const Random *rng, const Network *ns, + Messenger_Options *options, Messenger_Error *error) { if (options == nullptr) { return nullptr; @@ -3464,7 +3465,7 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random * *error = MESSENGER_ERROR_OTHER; } - Messenger *m = (Messenger *)mem_alloc(mem, sizeof(Messenger)); + Messenger *owner m = (Messenger *owner)mem_alloc(mem, sizeof(Messenger)); if (m == nullptr) { return nullptr; @@ -3673,7 +3674,7 @@ Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random * * * Free all datastructures. */ -void kill_messenger(Messenger *m) +void kill_messenger(Messenger *owner m) { if (m == nullptr) { return; diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index d9bdaf3540..f371b048ea 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -84,7 +84,7 @@ typedef struct Messenger_Options { void *log_context; void *log_user_data; - Messenger_State_Plugin *state_plugins; + Messenger_State_Plugin *owner state_plugins; uint8_t state_plugins_length; bool dns_enabled; @@ -93,7 +93,7 @@ typedef struct Messenger_Options { struct Receipts { uint32_t packet_num; uint32_t msg_id; - struct Receipts *next; + struct Receipts *owner next; }; /** Status definitions. */ @@ -235,7 +235,7 @@ typedef struct Friend { uint32_t num_sending_files; struct File_Transfers file_receiving[MAX_CONCURRENT_FILE_PIPES]; - struct Receipts *receipts_start; + struct Receipts *owner receipts_start; struct Receipts *receipts_end; } Friend; @@ -248,10 +248,10 @@ struct Messenger { Networking_Core *net; Net_Crypto *net_crypto; - DHT *dht; + DHT *owner dht; - Forwarding *forwarding; - Announcements *announce; + Forwarding *owner forwarding; + Announcements *owner announce; Onion *onion; Onion_Announce *onion_a; @@ -259,7 +259,7 @@ struct Messenger { Friend_Connections *fr_c; - TCP_Server *tcp_server; + TCP_Server *owner tcp_server; Friend_Requests *fr; uint8_t name[MAX_NAME_LENGTH]; uint16_t name_length; @@ -269,7 +269,7 @@ struct Messenger { Userstatus userstatus; - Friend *friendlist; + Friend *owner friendlist; uint32_t numfriends; uint64_t lastdump; @@ -762,15 +762,16 @@ typedef enum Messenger_Error { * if error is not NULL it will be set to one of the values in the enum above. */ non_null() -Messenger *new_messenger(Mono_Time *mono_time, const Memory *mem, const Random *rng, const Network *ns, - Messenger_Options *options, Messenger_Error *error); +Messenger *owner new_messenger( + Mono_Time *mono_time, const Memory *mem, const Random *rng, const Network *ns, + Messenger_Options *options, Messenger_Error *error); /** @brief Run this before closing shop. * * Free all datastructures. */ nullable(1) -void kill_messenger(Messenger *m); +void kill_messenger(Messenger *owner m); /** @brief The main loop that needs to be run at least 20 times per second. */ non_null(1) nullable(2) diff --git a/toxcore/TCP_client.c b/toxcore/TCP_client.c index 1982412cda..3e6a7ea1a2 100644 --- a/toxcore/TCP_client.c +++ b/toxcore/TCP_client.c @@ -592,7 +592,7 @@ void forwarding_handler(TCP_Client_Connection *con, forwarded_response_cb *forwa } /** Create new TCP connection to ip_port/public_key */ -TCP_Client_Connection *new_tcp_connection( +TCP_Client_Connection *owner new_tcp_connection( const Logger *logger, const Memory *mem, const Mono_Time *mono_time, const Random *rng, const Network *ns, const IP_Port *ip_port, const uint8_t *public_key, const uint8_t *self_public_key, const uint8_t *self_secret_key, const TCP_Proxy_Info *proxy_info, Net_Profile *net_profile) @@ -647,7 +647,7 @@ TCP_Client_Connection *new_tcp_connection( return nullptr; } - TCP_Client_Connection *temp = (TCP_Client_Connection *)mem_alloc(mem, sizeof(TCP_Client_Connection)); + TCP_Client_Connection *owner temp = (TCP_Client_Connection *owner)mem_alloc(mem, sizeof(TCP_Client_Connection)); if (temp == nullptr) { LOGGER_ERROR(logger, "Failed to allocate memory for TCP_Client_Connection"); @@ -1044,7 +1044,7 @@ void do_tcp_connection(const Logger *logger, const Mono_Time *mono_time, } /** Kill the TCP connection */ -void kill_tcp_connection(TCP_Client_Connection *tcp_connection) +void kill_tcp_connection(TCP_Client_Connection *owner tcp_connection) { if (tcp_connection == nullptr) { return; diff --git a/toxcore/TCP_client.h b/toxcore/TCP_client.h index 3ed6e8c683..101f69765e 100644 --- a/toxcore/TCP_client.h +++ b/toxcore/TCP_client.h @@ -62,7 +62,7 @@ void tcp_con_set_custom_uint(TCP_Client_Connection *con, uint32_t value); /** Create new TCP connection to ip_port/public_key */ non_null(1, 2, 3, 4, 5, 6, 7, 8, 9) nullable(10, 11) -TCP_Client_Connection *new_tcp_connection( +TCP_Client_Connection *owner new_tcp_connection( const Logger *logger, const Memory *mem, const Mono_Time *mono_time, const Random *rng, const Network *ns, const IP_Port *ip_port, const uint8_t *public_key, const uint8_t *self_public_key, const uint8_t *self_secret_key, const TCP_Proxy_Info *proxy_info, Net_Profile *net_profile); @@ -74,7 +74,7 @@ void do_tcp_connection(const Logger *logger, const Mono_Time *mono_time, /** Kill the TCP connection */ nullable(1) -void kill_tcp_connection(TCP_Client_Connection *tcp_connection); +void kill_tcp_connection(TCP_Client_Connection *owner tcp_connection); typedef int tcp_onion_response_cb(void *object, const uint8_t *data, uint16_t length, void *userdata); diff --git a/toxcore/TCP_common.c b/toxcore/TCP_common.c index e9f67dcffb..9eb63a7988 100644 --- a/toxcore/TCP_common.c +++ b/toxcore/TCP_common.c @@ -14,10 +14,10 @@ #include "mem.h" #include "network.h" -void wipe_priority_list(const Memory *mem, TCP_Priority_List *p) +void wipe_priority_list(const Memory *mem, TCP_Priority_List *owner p) { while (p != nullptr) { - TCP_Priority_List *pp = p; + TCP_Priority_List *owner pp = p; p = p->next; mem_delete(mem, pp->data); mem_delete(mem, pp); @@ -63,7 +63,7 @@ int send_pending_data(const Logger *logger, TCP_Connection *con) return -1; } - TCP_Priority_List *p = con->priority_queue_start; + TCP_Priority_List *owner p = con->priority_queue_start; while (p != nullptr) { const uint16_t left = p->size - p->sent; @@ -77,7 +77,7 @@ int send_pending_data(const Logger *logger, TCP_Connection *con) break; } - TCP_Priority_List *pp = p; + TCP_Priority_List *owner pp = p; p = p->next; mem_delete(con->mem, pp->data); mem_delete(con->mem, pp); @@ -101,13 +101,14 @@ non_null() static bool add_priority(TCP_Connection *con, const uint8_t *packet, uint16_t size, uint16_t sent) { TCP_Priority_List *p = con->priority_queue_end; - TCP_Priority_List *new_list = (TCP_Priority_List *)mem_alloc(con->mem, sizeof(TCP_Priority_List)); + TCP_Priority_List *owner new_list = + (TCP_Priority_List *owner)mem_alloc(con->mem, sizeof(TCP_Priority_List)); if (new_list == nullptr) { return false; } - uint8_t *data = (uint8_t *)mem_balloc(con->mem, size); + uint8_t *owner data = (uint8_t *owner)mem_balloc(con->mem, size); if (data == nullptr) { mem_delete(con->mem, new_list); diff --git a/toxcore/TCP_common.h b/toxcore/TCP_common.h index dd07fd1ec5..c9e3561cc8 100644 --- a/toxcore/TCP_common.h +++ b/toxcore/TCP_common.h @@ -15,14 +15,14 @@ typedef struct TCP_Priority_List TCP_Priority_List; struct TCP_Priority_List { - TCP_Priority_List *next; + TCP_Priority_List *owner next; uint16_t size; uint16_t sent; - uint8_t *data; + uint8_t *owner data; }; non_null(1) nullable(2) -void wipe_priority_list(const Memory *mem, TCP_Priority_List *p); +void wipe_priority_list(const Memory *mem, TCP_Priority_List *owner p); #define NUM_RESERVED_PORTS 16 #define NUM_CLIENT_CONNECTIONS (256 - NUM_RESERVED_PORTS) @@ -65,7 +65,7 @@ typedef struct TCP_Connection { uint16_t last_packet_length; uint16_t last_packet_sent; - TCP_Priority_List *priority_queue_start; + TCP_Priority_List *owner priority_queue_start; TCP_Priority_List *priority_queue_end; // This is a shared pointer to the parent's respective Net_Profile object diff --git a/toxcore/TCP_connection.c b/toxcore/TCP_connection.c index 20ac3ce061..a2d1e844cc 100644 --- a/toxcore/TCP_connection.c +++ b/toxcore/TCP_connection.c @@ -35,10 +35,10 @@ struct TCP_Connections { 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 *owner connections; uint32_t connections_length; /* Length of connections array. */ - TCP_con *tcp_connections; + TCP_con *owner tcp_connections; uint32_t tcp_connections_length; /* Length of tcp_connections array. */ tcp_data_cb *tcp_data_callback; @@ -81,7 +81,7 @@ uint32_t tcp_connections_count(const TCP_Connections *tcp_c) * @retval 0 if it succeeds. */ non_null() -static int realloc_tcp_connection_to(const Memory *mem, TCP_Connection_to **array, size_t num) +static int realloc_tcp_connection_to(const Memory *mem, TCP_Connection_to *owner *array, size_t num) { if (num == 0) { mem_delete(mem, *array); @@ -89,8 +89,8 @@ static int realloc_tcp_connection_to(const Memory *mem, TCP_Connection_to **arra return 0; } - TCP_Connection_to *temp_pointer = - (TCP_Connection_to *)mem_vrealloc(mem, *array, num, sizeof(TCP_Connection_to)); + TCP_Connection_to *owner temp_pointer = + (TCP_Connection_to *owner)mem_vrealloc(mem, *array, num, sizeof(TCP_Connection_to)); if (temp_pointer == nullptr) { return -1; @@ -102,7 +102,7 @@ static int realloc_tcp_connection_to(const Memory *mem, TCP_Connection_to **arra } non_null() -static int realloc_tcp_con(const Memory *mem, TCP_con **array, size_t num) +static int realloc_tcp_con(const Memory *mem, TCP_con *owner *array, size_t num) { if (num == 0) { mem_delete(mem, *array); @@ -110,7 +110,8 @@ static int realloc_tcp_con(const Memory *mem, TCP_con **array, size_t num) return 0; } - TCP_con *temp_pointer = (TCP_con *)mem_vrealloc(mem, *array, num, sizeof(TCP_con)); + TCP_con *owner temp_pointer = + (TCP_con *owner)mem_vrealloc(mem, *array, num, sizeof(TCP_con)); if (temp_pointer == nullptr) { return -1; @@ -1608,7 +1609,7 @@ TCP_Connections *new_tcp_connections(const Logger *logger, const Memory *mem, co return nullptr; } - TCP_Connections *temp = (TCP_Connections *)mem_alloc(mem, sizeof(TCP_Connections)); + TCP_Connections *owner temp = (TCP_Connections *owner)mem_alloc(mem, sizeof(TCP_Connections)); if (temp == nullptr) { return nullptr; @@ -1724,7 +1725,7 @@ void do_tcp_connections(const Logger *logger, TCP_Connections *tcp_c, void *user kill_nonused_tcp(tcp_c); } -void kill_tcp_connections(TCP_Connections *tcp_c) +void kill_tcp_connections(TCP_Connections *owner tcp_c) { if (tcp_c == nullptr) { return; diff --git a/toxcore/TCP_connection.h b/toxcore/TCP_connection.h index 33388d5f8b..2d6a5e6ca6 100644 --- a/toxcore/TCP_connection.h +++ b/toxcore/TCP_connection.h @@ -67,7 +67,7 @@ typedef struct TCP_Connection_to { typedef struct TCP_con { uint8_t status; - TCP_Client_Connection *connection; + TCP_Client_Connection *owner connection; uint64_t connected_time; uint32_t lock_count; uint32_t sleep_count; @@ -306,8 +306,9 @@ uint32_t tcp_copy_connected_relays_index(const TCP_Connections *tcp_c, Node_form * Returns NULL on failure. */ non_null() -TCP_Connections *new_tcp_connections(const Logger *logger, const Memory *mem, const Random *rng, const Network *ns, - Mono_Time *mono_time, const uint8_t *secret_key, const TCP_Proxy_Info *proxy_info); +TCP_Connections *owner new_tcp_connections( + const Logger *logger, const Memory *mem, const Random *rng, const Network *ns, + Mono_Time *mono_time, const uint8_t *secret_key, const TCP_Proxy_Info *proxy_info); non_null() int kill_tcp_relay_connection(TCP_Connections *tcp_c, int tcp_connections_number); @@ -316,7 +317,7 @@ non_null(1, 2) nullable(3) void do_tcp_connections(const Logger *logger, TCP_Connections *tcp_c, void *userdata); nullable(1) -void kill_tcp_connections(TCP_Connections *tcp_c); +void kill_tcp_connections(TCP_Connections *owner tcp_c); /** @brief a pointer to the tcp client net profile associated with tcp_c. * diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c index f5b32d9c5f..e0a59f01c7 100644 --- a/toxcore/TCP_server.c +++ b/toxcore/TCP_server.c @@ -75,7 +75,7 @@ struct TCP_Server { int efd; uint64_t last_run_pinged; #endif /* TCP_SERVER_USE_EPOLL */ - Socket *socks_listening; + Socket *owner 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 *owner accepted_connection_array; uint32_t size_accepted_connections; uint32_t num_accepted_connections; @@ -131,7 +131,7 @@ static int alloc_new_connections(TCP_Server *tcp_server, uint32_t num) return -1; } - TCP_Secure_Connection *new_connections = (TCP_Secure_Connection *)mem_vrealloc( + TCP_Secure_Connection *owner new_connections = (TCP_Secure_Connection *owner)mem_vrealloc( tcp_server->mem, tcp_server->accepted_connection_array, new_size, sizeof(TCP_Secure_Connection)); @@ -968,7 +968,7 @@ TCP_Server *new_tcp_server(const Logger *logger, const Memory *mem, const Random return nullptr; } - TCP_Server *temp = (TCP_Server *)mem_alloc(mem, sizeof(TCP_Server)); + TCP_Server *owner temp = (TCP_Server *owner)mem_alloc(mem, sizeof(TCP_Server)); if (temp == nullptr) { LOGGER_ERROR(logger, "TCP server allocation failed"); @@ -988,7 +988,7 @@ TCP_Server *new_tcp_server(const Logger *logger, const Memory *mem, const Random temp->ns = ns; temp->rng = rng; - Socket *socks_listening = (Socket *)mem_valloc(mem, num_sockets, sizeof(Socket)); + Socket *owner socks_listening = (Socket *owner)mem_valloc(mem, num_sockets, sizeof(Socket)); if (socks_listening == nullptr) { LOGGER_ERROR(logger, "socket allocation failed"); @@ -1406,7 +1406,7 @@ void do_tcp_server(TCP_Server *tcp_server, const Mono_Time *mono_time) do_tcp_confirmed(tcp_server, mono_time); } -void kill_tcp_server(TCP_Server *tcp_server) +void kill_tcp_server(TCP_Server *owner tcp_server) { if (tcp_server == nullptr) { return; diff --git a/toxcore/TCP_server.h b/toxcore/TCP_server.h index bd4c98b0cd..a58f7bf725 100644 --- a/toxcore/TCP_server.h +++ b/toxcore/TCP_server.h @@ -41,9 +41,10 @@ size_t tcp_server_listen_count(const TCP_Server *tcp_server); /** Create new TCP server instance. */ non_null(1, 2, 3, 4, 7, 8) nullable(9, 10) -TCP_Server *new_tcp_server(const Logger *logger, const Memory *mem, const Random *rng, const Network *ns, - bool ipv6_enabled, uint16_t num_sockets, const uint16_t *ports, - const uint8_t *secret_key, Onion *onion, Forwarding *forwarding); +TCP_Server *owner new_tcp_server( + const Logger *logger, const Memory *mem, const Random *rng, const Network *ns, + bool ipv6_enabled, uint16_t num_sockets, const uint16_t *ports, + const uint8_t *secret_key, Onion *onion, Forwarding *forwarding); /** Run the TCP_server */ non_null() @@ -51,7 +52,7 @@ void do_tcp_server(TCP_Server *tcp_server, const Mono_Time *mono_time); /** Kill the TCP server */ nullable(1) -void kill_tcp_server(TCP_Server *tcp_server); +void kill_tcp_server(TCP_Server *owner tcp_server); /** @brief Returns a pointer to the net profile associated with `tcp_server`. * diff --git a/toxcore/announce.c b/toxcore/announce.c index dc34ca05e3..80beb6337f 100644 --- a/toxcore/announce.c +++ b/toxcore/announce.c @@ -51,7 +51,7 @@ 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 *owner data; uint32_t length; } Announce_Entry; @@ -240,7 +240,7 @@ bool announce_store_data(Announcements *announce, const uint8_t *data_public_key mem_delete(announce->mem, entry->data); - uint8_t *entry_data = (uint8_t *)mem_balloc(announce->mem, length); + uint8_t *owner entry_data = (uint8_t *owner)mem_balloc(announce->mem, length); if (entry_data == nullptr) { entry->data = nullptr; // TODO(iphydf): Is this necessary? @@ -643,14 +643,15 @@ static int handle_dht_announce_request( return sendpacket(announce->net, source, reply, len) == len ? 0 : -1; } -Announcements *new_announcements(const Logger *log, const Memory *mem, const Random *rng, const Mono_Time *mono_time, - Forwarding *forwarding) +Announcements *owner new_announcements( + const Logger *log, const Memory *mem, const Random *rng, const Mono_Time *mono_time, + Forwarding *forwarding) { if (log == nullptr || mono_time == nullptr || forwarding == nullptr) { return nullptr; } - Announcements *announce = (Announcements *)mem_alloc(mem, sizeof(Announcements)); + Announcements *owner announce = (Announcements *owner)mem_alloc(mem, sizeof(Announcements)); if (announce == nullptr) { return nullptr; @@ -683,7 +684,7 @@ Announcements *new_announcements(const Logger *log, const Memory *mem, const Ran return announce; } -void kill_announcements(Announcements *announce) +void kill_announcements(Announcements *owner announce) { if (announce == nullptr) { return; diff --git a/toxcore/announce.h b/toxcore/announce.h index c1397454ac..4e792eb806 100644 --- a/toxcore/announce.h +++ b/toxcore/announce.h @@ -23,8 +23,9 @@ uint8_t announce_response_of_request_type(uint8_t request_type); typedef struct Announcements Announcements; non_null() -Announcements *new_announcements(const Logger *log, const Memory *mem, const Random *rng, const Mono_Time *mono_time, - Forwarding *forwarding); +Announcements *owner new_announcements( + const Logger *log, const Memory *mem, const Random *rng, const Mono_Time *mono_time, + Forwarding *forwarding); /** * @brief If data is stored, run `on_retrieve_callback` on it. @@ -39,7 +40,7 @@ non_null() void announce_set_synch_offset(Announcements *announce, int32_t synch_offset); nullable(1) -void kill_announcements(Announcements *announce); +void kill_announcements(Announcements *owner announce); /* The declarations below are not public, they are exposed only for tests. */ diff --git a/toxcore/attributes.h b/toxcore/attributes.h index 009dfa359e..683075167d 100644 --- a/toxcore/attributes.h +++ b/toxcore/attributes.h @@ -12,6 +12,19 @@ //!TOKSTYLE- +#ifdef __STDC_OWNERSHIP__ +#include +#else +//#define out +//#define opt +#define owner +#define obj_owner +//#define view +#define static_debug(x) +#define static_set(x, s) +#define unchecked +#endif + #ifdef __GNUC__ #define GNU_PRINTF(f, a) __attribute__((__format__(__printf__, f, a))) #else diff --git a/toxcore/bin_unpack.c b/toxcore/bin_unpack.c index 82b7f8d9d5..9e8ecf07aa 100644 --- a/toxcore/bin_unpack.c +++ b/toxcore/bin_unpack.c @@ -120,7 +120,7 @@ bool bin_unpack_bin(Bin_Unpack *bu, uint8_t **data_ptr, uint32_t *data_length_pt // There aren't as many bytes as this bin claims to want to allocate. return false; } - uint8_t *const data = (uint8_t *)malloc(bin_size); + uint8_t *owner data = (uint8_t *owner)malloc(bin_size); if (!bin_unpack_bin_b(bu, data, bin_size)) { free(data); diff --git a/toxcore/ccompat.h b/toxcore/ccompat.h index df68413741..c5b7ee8dc4 100644 --- a/toxcore/ccompat.h +++ b/toxcore/ccompat.h @@ -23,7 +23,10 @@ // "function") is used. Note the semantic difference: alloca'd memory does not // get freed at the end of the declaration's scope. Do not use VLA() in loops or // you may run out of stack space. -#if !defined(DISABLE_VLA) && !defined(_MSC_VER) && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L +#ifdef __STDC_OWNERSHIP__ +// HACK: some large array to silence cake until it supports VLAs. +#define ALLOC_VLA(type, name, size) type name[2048] +#elif !defined(DISABLE_VLA) && !defined(_MSC_VER) && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L // C99 VLAs. #define ALLOC_VLA(type, name, size) type name[size] #else diff --git a/toxcore/crypto_core.c b/toxcore/crypto_core.c index 8a6aa38fba..59a34e2093 100644 --- a/toxcore/crypto_core.c +++ b/toxcore/crypto_core.c @@ -89,9 +89,9 @@ const uint8_t *get_chat_id(const Extended_Public_Key *key) #if !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) non_null() -static uint8_t *crypto_malloc(const Memory *mem, size_t bytes) +static uint8_t *owner crypto_malloc(const Memory *mem, size_t bytes) { - uint8_t *ptr = (uint8_t *)mem_balloc(mem, bytes); + uint8_t *owner ptr = (uint8_t *owner)mem_balloc(mem, bytes); if (ptr != nullptr) { crypto_memlock(ptr, bytes); @@ -101,7 +101,7 @@ static uint8_t *crypto_malloc(const Memory *mem, size_t bytes) } non_null(1) nullable(2) -static void crypto_free(const Memory *mem, uint8_t *ptr, size_t bytes) +static void crypto_free(const Memory *mem, uint8_t *owner ptr, size_t bytes) { if (ptr != nullptr) { crypto_memzero(ptr, bytes); @@ -260,8 +260,8 @@ int32_t encrypt_data_symmetric(const Memory *mem, const size_t size_temp_plain = length + crypto_box_ZEROBYTES; const size_t size_temp_encrypted = length + crypto_box_MACBYTES + crypto_box_BOXZEROBYTES; - uint8_t *temp_plain = crypto_malloc(mem, size_temp_plain); - uint8_t *temp_encrypted = crypto_malloc(mem, size_temp_encrypted); + uint8_t *owner temp_plain = crypto_malloc(mem, size_temp_plain); + uint8_t *owner temp_encrypted = crypto_malloc(mem, size_temp_encrypted); if (temp_plain == nullptr || temp_encrypted == nullptr) { crypto_free(mem, temp_plain, size_temp_plain); @@ -313,8 +313,8 @@ int32_t decrypt_data_symmetric(const Memory *mem, const size_t size_temp_plain = length + crypto_box_ZEROBYTES; const size_t size_temp_encrypted = length + crypto_box_BOXZEROBYTES; - uint8_t *temp_plain = crypto_malloc(mem, size_temp_plain); - uint8_t *temp_encrypted = crypto_malloc(mem, size_temp_encrypted); + uint8_t *owner temp_plain = crypto_malloc(mem, size_temp_plain); + uint8_t *owner temp_encrypted = crypto_malloc(mem, size_temp_encrypted); if (temp_plain == nullptr || temp_encrypted == nullptr) { crypto_free(mem, temp_plain, size_temp_plain); diff --git a/toxcore/forwarding.c b/toxcore/forwarding.c index 6d608b5e7b..0984b7efc6 100644 --- a/toxcore/forwarding.c +++ b/toxcore/forwarding.c @@ -358,13 +358,14 @@ void set_callback_forward_reply(Forwarding *forwarding, forward_reply_cb *functi forwarding->forward_reply_callback_object = object; } -Forwarding *new_forwarding(const Logger *log, const Memory *mem, const Random *rng, const Mono_Time *mono_time, DHT *dht) +Forwarding *owner new_forwarding( + const Logger *log, const Memory *mem, const Random *rng, const Mono_Time *mono_time, DHT *dht) { if (log == nullptr || mono_time == nullptr || dht == nullptr) { return nullptr; } - Forwarding *forwarding = (Forwarding *)mem_alloc(mem, sizeof(Forwarding)); + Forwarding *owner forwarding = (Forwarding *owner)mem_alloc(mem, sizeof(Forwarding)); if (forwarding == nullptr) { return nullptr; @@ -386,7 +387,7 @@ Forwarding *new_forwarding(const Logger *log, const Memory *mem, const Random *r return forwarding; } -void kill_forwarding(Forwarding *forwarding) +void kill_forwarding(Forwarding *owner forwarding) { if (forwarding == nullptr) { return; diff --git a/toxcore/forwarding.h b/toxcore/forwarding.h index b4431f57af..5c1960eb27 100644 --- a/toxcore/forwarding.h +++ b/toxcore/forwarding.h @@ -117,10 +117,11 @@ non_null(1) nullable(2, 3) void set_callback_forward_reply(Forwarding *forwarding, forward_reply_cb *function, void *object); non_null() -Forwarding *new_forwarding(const Logger *log, const Memory *mem, const Random *rng, const Mono_Time *mono_time, DHT *dht); +Forwarding *owner new_forwarding( + const Logger *log, const Memory *mem, const Random *rng, const Mono_Time *mono_time, DHT *dht); nullable(1) -void kill_forwarding(Forwarding *forwarding); +void kill_forwarding(Forwarding *owner forwarding); #ifdef __cplusplus } /* extern "C" */ diff --git a/toxcore/friend_connection.c b/toxcore/friend_connection.c index 78c6d4acdc..aab1f9eea1 100644 --- a/toxcore/friend_connection.c +++ b/toxcore/friend_connection.c @@ -73,10 +73,10 @@ struct Friend_Connections { const Logger *logger; Net_Crypto *net_crypto; DHT *dht; - Broadcast_Info *broadcast; + Broadcast_Info *owner broadcast; Onion_Client *onion_c; - Friend_Conn *conns; + Friend_Conn *owner conns; uint32_t num_cons; fr_request_cb *fr_request_callback; @@ -132,7 +132,8 @@ static bool realloc_friendconns(Friend_Connections *fr_c, uint32_t num) return true; } - Friend_Conn *newgroup_cons = (Friend_Conn *)mem_vrealloc(fr_c->mem, fr_c->conns, num, sizeof(Friend_Conn)); + Friend_Conn *owner newgroup_cons = + (Friend_Conn *owner)mem_vrealloc(fr_c->mem, fr_c->conns, num, sizeof(Friend_Conn)); if (newgroup_cons == nullptr) { return false; @@ -920,7 +921,7 @@ Friend_Connections *new_friend_connections( return nullptr; } - Friend_Connections *const temp = (Friend_Connections *)mem_alloc(mem, sizeof(Friend_Connections)); + Friend_Connections *const owner temp = (Friend_Connections *owner)mem_alloc(mem, sizeof(Friend_Connections)); if (temp == nullptr) { return nullptr; @@ -1028,7 +1029,7 @@ void do_friend_connections(Friend_Connections *fr_c, void *userdata) } /** Free everything related with friend_connections. */ -void kill_friend_connections(Friend_Connections *fr_c) +void kill_friend_connections(Friend_Connections *owner fr_c) { if (fr_c == nullptr) { return; diff --git a/toxcore/friend_requests.c b/toxcore/friend_requests.c index e28ce7df85..1cdcf8a698 100644 --- a/toxcore/friend_requests.c +++ b/toxcore/friend_requests.c @@ -170,9 +170,9 @@ void friendreq_init(Friend_Requests *fr, Friend_Connections *fr_c) set_friend_request_callback(fr_c, &friendreq_handlepacket, fr); } -Friend_Requests *friendreq_new(const Memory *mem) +Friend_Requests *owner friendreq_new(const Memory *mem) { - Friend_Requests *fr = (Friend_Requests *)mem_alloc(mem, sizeof(Friend_Requests)); + Friend_Requests *owner fr = (Friend_Requests *owner)mem_alloc(mem, sizeof(Friend_Requests)); if (fr == nullptr) { return nullptr; @@ -183,7 +183,7 @@ Friend_Requests *friendreq_new(const Memory *mem) return fr; } -void friendreq_kill(Friend_Requests *fr) +void friendreq_kill(Friend_Requests *owner fr) { if (fr == nullptr) { return; diff --git a/toxcore/group.c b/toxcore/group.c index dbc3876403..c5cda00d4f 100644 --- a/toxcore/group.c +++ b/toxcore/group.c @@ -112,10 +112,10 @@ typedef struct Group_c { bool need_send_name; bool title_fresh; - Group_Peer *group; + Group_Peer *owner group; uint32_t numpeers; - Group_Peer *frozen; + Group_Peer *owner frozen; uint32_t numfrozen; uint32_t maxfrozen; @@ -154,7 +154,7 @@ struct Group_Chats { Messenger *m; Friend_Connections *fr_c; - Group_c *chats; + Group_c *owner chats; uint16_t num_chats; g_conference_invite_cb *invite_callback; @@ -260,7 +260,7 @@ static bool realloc_conferences(Group_Chats *g_c, uint16_t num) return true; } - Group_c *newgroup_chats = (Group_c *)mem_vrealloc(g_c->mem, g_c->chats, num, sizeof(Group_c)); + Group_c *owner newgroup_chats = (Group_c *owner)mem_vrealloc(g_c->mem, g_c->chats, num, sizeof(Group_c)); if (newgroup_chats == nullptr) { return false; @@ -685,7 +685,7 @@ static bool delete_frozen(const Memory *mem, Group_c *g, uint32_t frozen_index) g->frozen[frozen_index] = g->frozen[g->numfrozen]; } - Group_Peer *const frozen_temp = (Group_Peer *)mem_vrealloc(mem, g->frozen, g->numfrozen, sizeof(Group_Peer)); + Group_Peer *const owner frozen_temp = (Group_Peer *owner)mem_vrealloc(mem, g->frozen, g->numfrozen, sizeof(Group_Peer)); if (frozen_temp == nullptr) { return false; @@ -726,7 +726,7 @@ static int note_peer_active(Group_Chats *g_c, uint32_t groupnumber, uint16_t pee /* Now thaw the peer */ - Group_Peer *temp = (Group_Peer *)mem_vrealloc(g_c->mem, g->group, g->numpeers + 1, sizeof(Group_Peer)); + Group_Peer *owner temp = (Group_Peer *owner)mem_vrealloc(g_c->mem, g->group, g->numpeers + 1, sizeof(Group_Peer)); if (temp == nullptr) { return -1; @@ -839,7 +839,7 @@ static int addpeer(Group_Chats *g_c, uint32_t groupnumber, const uint8_t *real_p delete_any_peer_with_pk(g_c, groupnumber, real_pk, userdata); - Group_Peer *temp = (Group_Peer *)mem_vrealloc(g_c->mem, g->group, g->numpeers + 1, sizeof(Group_Peer)); + Group_Peer *owner temp = (Group_Peer *owner)mem_vrealloc(g_c->mem, g->group, g->numpeers + 1, sizeof(Group_Peer)); if (temp == nullptr) { return -1; @@ -937,7 +937,7 @@ static bool delpeer(Group_Chats *g_c, uint32_t groupnumber, int peer_index, void g->group[peer_index] = g->group[g->numpeers]; } - Group_Peer *temp = (Group_Peer *)mem_vrealloc(g_c->mem, g->group, g->numpeers, sizeof(Group_Peer)); + Group_Peer *owner temp = (Group_Peer *owner)mem_vrealloc(g_c->mem, g->group, g->numpeers, sizeof(Group_Peer)); if (temp == nullptr) { return false; @@ -1042,7 +1042,7 @@ static bool delete_old_frozen(Group_c *g, const Memory *mem) merge_sort(g->frozen, g->numfrozen, mem, &group_peer_cmp_funcs); - Group_Peer *temp = (Group_Peer *)mem_vrealloc(mem, g->frozen, g->maxfrozen, sizeof(Group_Peer)); + Group_Peer *owner temp = (Group_Peer *owner)mem_vrealloc(mem, g->frozen, g->maxfrozen, sizeof(Group_Peer)); if (temp == nullptr) { return false; @@ -1067,7 +1067,7 @@ static bool freeze_peer(Group_Chats *g_c, uint32_t groupnumber, int peer_index, return false; } - Group_Peer *temp = (Group_Peer *)mem_vrealloc(g_c->mem, g->frozen, g->numfrozen + 1, sizeof(Group_Peer)); + Group_Peer *owner temp = (Group_Peer *owner)mem_vrealloc(g_c->mem, g->frozen, g->numfrozen + 1, sizeof(Group_Peer)); if (temp == nullptr) { return false; @@ -3665,7 +3665,7 @@ static uint32_t load_group(Group_c *g, const Group_Chats *g_c, const uint8_t *da } // This is inefficient, but allows us to check data consistency before allocating memory - Group_Peer *tmp_frozen = (Group_Peer *)mem_vrealloc(g_c->mem, g->frozen, j + 1, sizeof(Group_Peer)); + Group_Peer *owner tmp_frozen = (Group_Peer *owner)mem_vrealloc(g_c->mem, g->frozen, j + 1, sizeof(Group_Peer)); if (tmp_frozen == nullptr) { // Memory allocation failure @@ -3811,7 +3811,7 @@ Group_Chats *new_groupchats(const Mono_Time *mono_time, const Memory *mem, Messe return nullptr; } - Group_Chats *temp = (Group_Chats *)mem_alloc(mem, sizeof(Group_Chats)); + Group_Chats *owner temp = (Group_Chats *owner)mem_alloc(mem, sizeof(Group_Chats)); if (temp == nullptr) { return nullptr; @@ -3855,7 +3855,7 @@ void do_groupchats(Group_Chats *g_c, void *userdata) } /** Free everything related with group chats. */ -void kill_groupchats(Group_Chats *g_c) +void kill_groupchats(Group_Chats *owner g_c) { if (g_c == nullptr) { return; diff --git a/toxcore/group_announce.c b/toxcore/group_announce.c index caa70193aa..d347aa8c4e 100644 --- a/toxcore/group_announce.c +++ b/toxcore/group_announce.c @@ -20,7 +20,7 @@ * Removes `announces` from `gc_announces_list`. */ non_null() -static void remove_announces(GC_Announces_List *gc_announces_list, GC_Announces *announces) +static void remove_announces(GC_Announces_List *gc_announces_list, GC_Announces *owner announces) { if (announces == nullptr || gc_announces_list == nullptr) { return; @@ -44,9 +44,9 @@ static void remove_announces(GC_Announces_List *gc_announces_list, GC_Announces * Returns null if no announce is found. */ non_null() -static GC_Announces *get_announces_by_chat_id(const GC_Announces_List *gc_announces_list, const uint8_t *chat_id) +static const GC_Announces *get_announces_by_chat_id(const GC_Announces_List *gc_announces_list, const uint8_t *chat_id) { - GC_Announces *announces = gc_announces_list->root_announces; + const GC_Announces *announces = gc_announces_list->root_announces; while (announces != nullptr) { if (memcmp(announces->chat_id, chat_id, CHAT_ID_SIZE) == 0) { @@ -59,6 +59,37 @@ static GC_Announces *get_announces_by_chat_id(const GC_Announces_List *gc_announ return nullptr; } +non_null() +static GC_Announces *owner get_announces_by_chat_id_mut(const GC_Announces_List *gc_announces_list, const uint8_t *chat_id) +{ + GC_Announces *owner announces = gc_announces_list->root_announces; + + while (announces != nullptr) { + if (memcmp(announces->chat_id, chat_id, CHAT_ID_SIZE) == 0) { + return announces; + } + + announces = announces->next_announce; + } + + return nullptr; +} + +non_null() +static void remove_announces_by_chat_id(GC_Announces_List *gc_announces_list, const uint8_t *chat_id) +{ + GC_Announces *owner announces = gc_announces_list->root_announces; + + while (announces != nullptr) { + if (memcmp(announces->chat_id, chat_id, CHAT_ID_SIZE) == 0) { + remove_announces(gc_announces_list, announces); + return; + } + + announces = announces->next_announce; + } +} + int gca_get_announces(const GC_Announces_List *gc_announces_list, GC_Announce *gc_announces, uint8_t max_nodes, const uint8_t *chat_id, const uint8_t *except_public_key) { @@ -342,12 +373,12 @@ int gca_unpack_announces_list(const Logger *log, const uint8_t *data, uint16_t l } non_null() -static GC_Announces *gca_new_announces( +static GC_Announces *owner gca_new_announces( const Memory *mem, GC_Announces_List *gc_announces_list, const GC_Public_Announce *public_announce) { - GC_Announces *announces = (GC_Announces *)mem_alloc(mem, sizeof(GC_Announces)); + GC_Announces *owner announces = (GC_Announces *owner)mem_alloc(mem, sizeof(GC_Announces)); if (announces == nullptr) { return nullptr; @@ -374,7 +405,7 @@ GC_Peer_Announce *gca_add_announce(const Memory *mem, const Mono_Time *mono_time return nullptr; } - GC_Announces *announces = get_announces_by_chat_id(gc_announces_list, public_announce->chat_public_key); + GC_Announces *owner announces = get_announces_by_chat_id_mut(gc_announces_list, public_announce->chat_public_key); // No entry for this chat_id exists so we create one if (announces == nullptr) { @@ -413,7 +444,7 @@ bool gca_is_valid_announce(const GC_Announce *announce) GC_Announces_List *new_gca_list(const Memory *mem) { - GC_Announces_List *announces_list = (GC_Announces_List *)mem_alloc(mem, sizeof(GC_Announces_List)); + GC_Announces_List *owner announces_list = (GC_Announces_List *owner)mem_alloc(mem, sizeof(GC_Announces_List)); if (announces_list == nullptr) { return nullptr; @@ -424,16 +455,16 @@ GC_Announces_List *new_gca_list(const Memory *mem) return announces_list; } -void kill_gca(GC_Announces_List *announces_list) +void kill_gca(GC_Announces_List *owner announces_list) { if (announces_list == nullptr) { return; } - GC_Announces *root = announces_list->root_announces; + GC_Announces *owner root = announces_list->root_announces; while (root != nullptr) { - GC_Announces *next = root->next_announce; + GC_Announces *owner next = root->next_announce; mem_delete(announces_list->mem, root); root = next; } @@ -459,11 +490,11 @@ void do_gca(const Mono_Time *mono_time, GC_Announces_List *gc_announces_list) gc_announces_list->last_timeout_check = mono_time_get(mono_time); - GC_Announces *announces = gc_announces_list->root_announces; + GC_Announces *owner announces = gc_announces_list->root_announces; while (announces != nullptr) { if (mono_time_is_timeout(mono_time, announces->last_announce_received_timestamp, GCA_ANNOUNCE_SAVE_TIMEOUT)) { - GC_Announces *to_delete = announces; + GC_Announces *owner to_delete = announces; announces = announces->next_announce; remove_announces(gc_announces_list, to_delete); continue; @@ -479,9 +510,5 @@ void cleanup_gca(GC_Announces_List *gc_announces_list, const uint8_t *chat_id) return; } - GC_Announces *announces = get_announces_by_chat_id(gc_announces_list, chat_id); - - if (announces != nullptr) { - remove_announces(gc_announces_list, announces); - } + remove_announces_by_chat_id(gc_announces_list, chat_id); } diff --git a/toxcore/group_announce.h b/toxcore/group_announce.h index 2e043a069a..1a6464dec6 100644 --- a/toxcore/group_announce.h +++ b/toxcore/group_announce.h @@ -74,7 +74,7 @@ struct GC_Announces { GC_Peer_Announce peer_announces[GCA_MAX_SAVED_ANNOUNCES_PER_GC]; - GC_Announces *next_announce; + GC_Announces *owner next_announce; GC_Announces *prev_announce; }; @@ -82,7 +82,7 @@ struct GC_Announces { struct GC_Announces_List { const Memory *mem; - GC_Announces *root_announces; + GC_Announces *owner root_announces; uint64_t last_timeout_check; }; diff --git a/toxcore/group_chats.c b/toxcore/group_chats.c index a364ac938c..3e1c69857b 100644 --- a/toxcore/group_chats.c +++ b/toxcore/group_chats.c @@ -1485,7 +1485,7 @@ static int group_packet_unwrap(const Logger *log, const Memory *mem, const GC_Co return -1; } - uint8_t *plain = (uint8_t *)mem_balloc(mem, length); + uint8_t *owner plain = (uint8_t *owner)mem_balloc(mem, length); if (plain == nullptr) { LOGGER_ERROR(log, "Failed to allocate memory for plain data buffer"); @@ -1553,7 +1553,7 @@ int group_packet_wrap( return -1; } - uint8_t *plain = (uint8_t *)mem_balloc(mem, packet_size); + uint8_t *owner plain = (uint8_t *owner)mem_balloc(mem, packet_size); if (plain == nullptr) { return -1; @@ -1581,7 +1581,7 @@ int group_packet_wrap( const uint16_t plain_len = padding_len + enc_header_len + length; const uint16_t encrypt_buf_size = plain_len + CRYPTO_MAC_SIZE; - uint8_t *encrypt = (uint8_t *)mem_balloc(mem, encrypt_buf_size); + uint8_t *owner encrypt = (uint8_t *owner)mem_balloc(mem, encrypt_buf_size); if (encrypt == nullptr) { mem_delete(mem, plain); @@ -1627,7 +1627,7 @@ static bool send_lossy_group_packet(const GC_Chat *chat, const GC_Connection *gc } const uint16_t packet_size = gc_get_wrapped_packet_size(length, NET_PACKET_GC_LOSSY); - uint8_t *packet = (uint8_t *)mem_balloc(chat->mem, packet_size); + uint8_t *owner packet = (uint8_t *owner)mem_balloc(chat->mem, packet_size); if (packet == nullptr) { return false; @@ -1883,7 +1883,7 @@ static bool sync_response_send_peers(GC_Chat *chat, GC_Connection *gconn, uint32 return true; } - uint8_t *response = (uint8_t *)mem_balloc(chat->mem, MAX_GC_PACKET_CHUNK_SIZE); + uint8_t *owner response = (uint8_t *owner)mem_balloc(chat->mem, MAX_GC_PACKET_CHUNK_SIZE); if (response == nullptr) { return false; @@ -2387,7 +2387,7 @@ static bool send_gc_broadcast_message(const GC_Chat *chat, const uint8_t *data, return false; } - uint8_t *packet = (uint8_t *)mem_balloc(chat->mem, length + GC_BROADCAST_ENC_HEADER_SIZE); + uint8_t *owner packet = (uint8_t *owner)mem_balloc(chat->mem, length + GC_BROADCAST_ENC_HEADER_SIZE); if (packet == nullptr) { return false; @@ -2631,7 +2631,7 @@ void gc_get_chat_id(const GC_Chat *chat, uint8_t *dest) non_null() static bool send_self_to_peer(const GC_Chat *chat, GC_Connection *gconn) { - GC_Peer *self = (GC_Peer *)mem_alloc(chat->mem, sizeof(GC_Peer)); + GC_Peer *owner self = (GC_Peer *owner)mem_alloc(chat->mem, sizeof(GC_Peer)); if (self == nullptr) { return false; @@ -2640,7 +2640,7 @@ static bool send_self_to_peer(const GC_Chat *chat, GC_Connection *gconn) copy_self(chat, self); const uint16_t data_size = PACKED_GC_PEER_SIZE + sizeof(uint16_t) + MAX_GC_PASSWORD_SIZE; - uint8_t *data = (uint8_t *)mem_balloc(chat->mem, data_size); + uint8_t *owner data = (uint8_t *owner)mem_balloc(chat->mem, data_size); if (data == nullptr) { mem_delete(chat->mem, self); @@ -2781,7 +2781,7 @@ static int handle_gc_peer_info_response(const GC_Session *c, GC_Chat *chat, uint return -1; } - GC_Peer *peer_info = (GC_Peer *)mem_alloc(chat->mem, sizeof(GC_Peer)); + GC_Peer *owner peer_info = (GC_Peer *owner)mem_alloc(chat->mem, sizeof(GC_Peer)); if (peer_info == nullptr) { return -8; @@ -3181,7 +3181,7 @@ static int handle_gc_sanctions_list(const GC_Session *c, GC_Chat *chat, const ui Mod_Sanction_Creds creds; - Mod_Sanction *sanctions = (Mod_Sanction *)mem_valloc(chat->mem, num_sanctions, sizeof(Mod_Sanction)); + Mod_Sanction *owner sanctions = (Mod_Sanction *owner)mem_valloc(chat->mem, num_sanctions, sizeof(Mod_Sanction)); if (sanctions == nullptr) { return -1; @@ -3247,7 +3247,7 @@ static int make_gc_mod_list_packet(const GC_Chat *chat, uint8_t *data, uint32_t const uint16_t length = sizeof(uint16_t) + mod_list_size; if (mod_list_size > 0) { - uint8_t *packed_mod_list = (uint8_t *)mem_balloc(chat->mem, mod_list_size); + uint8_t *owner packed_mod_list = (uint8_t *owner)mem_balloc(chat->mem, mod_list_size); if (packed_mod_list == nullptr) { return -1; @@ -3271,7 +3271,7 @@ static bool send_peer_mod_list(const GC_Chat *chat, GC_Connection *gconn) { const uint16_t mod_list_size = chat->moderation.num_mods * MOD_LIST_ENTRY_SIZE; const uint16_t length = sizeof(uint16_t) + mod_list_size; - uint8_t *packet = (uint8_t *)mem_balloc(chat->mem, length); + uint8_t *owner packet = (uint8_t *owner)mem_balloc(chat->mem, length); if (packet == nullptr) { return false; @@ -3330,7 +3330,7 @@ static bool send_peer_sanctions_list(const GC_Chat *chat, GC_Connection *gconn) const uint16_t packet_size = MOD_SANCTION_PACKED_SIZE * chat->moderation.num_sanctions + sizeof(uint16_t) + MOD_SANCTIONS_CREDS_SIZE; - uint8_t *packet = (uint8_t *)mem_balloc(chat->mem, packet_size); + uint8_t *owner packet = (uint8_t *owner)mem_balloc(chat->mem, packet_size); if (packet == nullptr) { return false; @@ -3360,7 +3360,7 @@ static bool broadcast_gc_sanctions_list(const GC_Chat *chat) const uint16_t packet_size = MOD_SANCTION_PACKED_SIZE * chat->moderation.num_sanctions + sizeof(uint16_t) + MOD_SANCTIONS_CREDS_SIZE; - uint8_t *packet = (uint8_t *)mem_balloc(chat->mem, packet_size); + uint8_t *owner packet = (uint8_t *owner)mem_balloc(chat->mem, packet_size); if (packet == nullptr) { return false; @@ -3406,7 +3406,7 @@ static bool broadcast_gc_mod_list(const GC_Chat *chat) { const uint16_t mod_list_size = chat->moderation.num_mods * MOD_LIST_ENTRY_SIZE; const uint16_t length = sizeof(uint16_t) + mod_list_size; - uint8_t *packet = (uint8_t *)mem_balloc(chat->mem, length); + uint8_t *owner packet = (uint8_t *owner)mem_balloc(chat->mem, length); if (packet == nullptr) { return false; @@ -3699,7 +3699,7 @@ non_null() static bool send_peer_topic(const GC_Chat *chat, GC_Connection *gconn) { const uint16_t packet_buf_size = SIGNATURE_SIZE + chat->topic_info.length + GC_MIN_PACKED_TOPIC_INFO_SIZE; - uint8_t *packet = (uint8_t *)mem_balloc(chat->mem, packet_buf_size); + uint8_t *owner packet = (uint8_t *owner)mem_balloc(chat->mem, packet_buf_size); if (packet == nullptr) { return false; @@ -3767,7 +3767,7 @@ non_null() static bool broadcast_gc_topic(const GC_Chat *chat) { const uint16_t packet_buf_size = SIGNATURE_SIZE + chat->topic_info.length + GC_MIN_PACKED_TOPIC_INFO_SIZE; - uint8_t *packet = (uint8_t *)mem_balloc(chat->mem, packet_buf_size); + uint8_t *owner packet = (uint8_t *owner)mem_balloc(chat->mem, packet_buf_size); if (packet == nullptr) { return false; @@ -3832,7 +3832,7 @@ int gc_set_topic(GC_Chat *chat, const uint8_t *topic, uint16_t length) chat->topic_info.checksum = get_gc_topic_checksum(&chat->topic_info); const uint16_t packet_buf_size = length + GC_MIN_PACKED_TOPIC_INFO_SIZE; - uint8_t *packed_topic = (uint8_t *)mem_balloc(chat->mem, packet_buf_size); + uint8_t *owner packed_topic = (uint8_t *owner)mem_balloc(chat->mem, packet_buf_size); if (packed_topic == nullptr) { return -3; @@ -4116,7 +4116,7 @@ int gc_founder_set_password(GC_Chat *chat, const uint8_t *password, uint16_t pas } const uint16_t oldlen = chat->shared_state.password_length; - uint8_t *oldpasswd = memdup(chat->mem, chat->shared_state.password, oldlen); + uint8_t *owner oldpasswd = memdup(chat->mem, chat->shared_state.password, oldlen); if (oldpasswd == nullptr && oldlen > 0) { return -4; @@ -4258,7 +4258,7 @@ non_null() static bool send_gc_set_mod(const GC_Chat *chat, const GC_Connection *gconn, bool add_mod) { const uint16_t length = 1 + SIG_PUBLIC_KEY_SIZE; - uint8_t *data = (uint8_t *)mem_balloc(chat->mem, length); + uint8_t *owner data = (uint8_t *owner)mem_balloc(chat->mem, length); if (data == nullptr) { return false; @@ -4461,7 +4461,7 @@ static bool send_gc_set_observer(const GC_Chat *chat, const Extended_Public_Key const uint8_t *sanction_data, uint16_t length, bool add_obs) { const uint16_t packet_len = 1 + ENC_PUBLIC_KEY_SIZE + SIG_PUBLIC_KEY_SIZE + length; - uint8_t *packet = (uint8_t *)mem_balloc(chat->mem, packet_len); + uint8_t *owner packet = (uint8_t *owner)mem_balloc(chat->mem, packet_len); if (packet == nullptr) { return false; @@ -4895,7 +4895,7 @@ int gc_send_message(const GC_Chat *chat, const uint8_t *message, uint16_t length const uint8_t packet_type = type == GC_MESSAGE_TYPE_NORMAL ? GM_PLAIN_MESSAGE : GM_ACTION_MESSAGE; const uint16_t length_raw = length + GC_MESSAGE_PSEUDO_ID_SIZE; - uint8_t *message_raw = (uint8_t *)mem_balloc(chat->mem, length_raw); + uint8_t *owner message_raw = (uint8_t *owner)mem_balloc(chat->mem, length_raw); if (message_raw == nullptr) { return -5; @@ -4983,7 +4983,7 @@ int gc_send_private_message(const GC_Chat *chat, GC_Peer_Id peer_id, uint8_t typ } const uint16_t raw_length = 1 + length + GC_MESSAGE_PSEUDO_ID_SIZE; - uint8_t *message_with_type = (uint8_t *)mem_balloc(chat->mem, raw_length); + uint8_t *owner message_with_type = (uint8_t *owner)mem_balloc(chat->mem, raw_length); if (message_with_type == nullptr) { return -6; @@ -4996,7 +4996,7 @@ int gc_send_private_message(const GC_Chat *chat, GC_Peer_Id peer_id, uint8_t typ memcpy(message_with_type + 1 + GC_MESSAGE_PSEUDO_ID_SIZE, message, length); - uint8_t *packet = (uint8_t *)mem_balloc(chat->mem, raw_length + GC_BROADCAST_ENC_HEADER_SIZE); + uint8_t *owner packet = (uint8_t *owner)mem_balloc(chat->mem, raw_length + GC_BROADCAST_ENC_HEADER_SIZE); if (packet == nullptr) { mem_delete(chat->mem, message_with_type); @@ -5552,7 +5552,7 @@ static int wrap_group_handshake_packet( random_nonce(rng, nonce); const size_t encrypt_buf_size = length + CRYPTO_MAC_SIZE; - uint8_t *encrypt = (uint8_t *)mem_balloc(mem, encrypt_buf_size); + uint8_t *owner encrypt = (uint8_t *owner)mem_balloc(mem, encrypt_buf_size); if (encrypt == nullptr) { return -2; @@ -5898,7 +5898,7 @@ static int handle_gc_handshake_request(GC_Chat *chat, const IP_Port *ipp, const if (nodes_count > 0) { const int add_tcp_result = add_tcp_relay_connection(chat->tcp_conn, gconn->tcp_connection_num, - &node->ip_port, node->public_key); + &node[0].ip_port, node[0].public_key); if (add_tcp_result < 0 && is_new_peer && ipp == nullptr) { LOGGER_WARNING(chat->log, "Broken tcp relay for new peer"); @@ -5945,7 +5945,7 @@ static int handle_gc_handshake_packet(GC_Chat *chat, const uint8_t *sender_pk, c } const size_t data_buf_size = length - CRYPTO_NONCE_SIZE - CRYPTO_MAC_SIZE; - uint8_t *data = (uint8_t *)mem_balloc(chat->mem, data_buf_size); + uint8_t *owner data = (uint8_t *owner)mem_balloc(chat->mem, data_buf_size); if (data == nullptr) { return -1; @@ -6171,7 +6171,7 @@ static bool handle_gc_lossless_packet(const GC_Session *c, GC_Chat *chat, const return true; } - uint8_t *data = (uint8_t *)mem_balloc(chat->mem, length); + uint8_t *owner data = (uint8_t *owner)mem_balloc(chat->mem, length); if (data == nullptr) { LOGGER_DEBUG(chat->log, "Failed to allocate memory for packet data buffer"); @@ -6324,7 +6324,7 @@ static bool handle_gc_lossy_packet(const GC_Session *c, GC_Chat *chat, const uin return false; } - uint8_t *data = (uint8_t *)mem_balloc(chat->mem, length); + uint8_t *owner data = (uint8_t *owner)mem_balloc(chat->mem, length); if (data == nullptr) { LOGGER_ERROR(chat->log, "Failed to allocate memory for packet buffer"); @@ -6731,7 +6731,7 @@ static bool peer_delete(const GC_Session *c, GC_Chat *chat, uint32_t peer_number 0 }; - GC_Peer *tmp_group = (GC_Peer *)mem_vrealloc(chat->mem, chat->group, chat->numpeers, sizeof(GC_Peer)); + GC_Peer *owner tmp_group = (GC_Peer *owner)mem_vrealloc(chat->mem, chat->group, chat->numpeers, sizeof(GC_Peer)); if (tmp_group == nullptr) { return false; @@ -6807,8 +6807,8 @@ int peer_add(GC_Chat *chat, const IP_Port *ipp, const uint8_t *public_key) } } - GC_Message_Array_Entry *send = (GC_Message_Array_Entry *)mem_valloc(chat->mem, GCC_BUFFER_SIZE, sizeof(GC_Message_Array_Entry)); - GC_Message_Array_Entry *recv = (GC_Message_Array_Entry *)mem_valloc(chat->mem, GCC_BUFFER_SIZE, sizeof(GC_Message_Array_Entry)); + GC_Message_Array_Entry *owner send = (GC_Message_Array_Entry *owner)mem_valloc(chat->mem, GCC_BUFFER_SIZE, sizeof(GC_Message_Array_Entry)); + GC_Message_Array_Entry *owner recv = (GC_Message_Array_Entry *owner)mem_valloc(chat->mem, GCC_BUFFER_SIZE, sizeof(GC_Message_Array_Entry)); if (send == nullptr || recv == nullptr) { LOGGER_ERROR(chat->log, "Failed to allocate memory for gconn buffers"); @@ -6822,7 +6822,7 @@ int peer_add(GC_Chat *chat, const IP_Port *ipp, const uint8_t *public_key) return -1; } - GC_Peer *tmp_group = (GC_Peer *)mem_vrealloc(chat->mem, chat->group, chat->numpeers + 1, sizeof(GC_Peer)); + GC_Peer *owner tmp_group = (GC_Peer *owner)mem_vrealloc(chat->mem, chat->group, chat->numpeers + 1, sizeof(GC_Peer)); if (tmp_group == nullptr) { LOGGER_ERROR(chat->log, "Failed to allocate memory for group mem_vrealloc"); @@ -7055,7 +7055,7 @@ non_null() static bool ping_peer(const GC_Chat *chat, const GC_Connection *gconn) { const uint16_t buf_size = GC_PING_PACKET_MIN_DATA_SIZE + sizeof(IP_Port); - uint8_t *data = (uint8_t *)mem_balloc(chat->mem, buf_size); + uint8_t *owner data = (uint8_t *owner)mem_balloc(chat->mem, buf_size); if (data == nullptr) { return false; @@ -7326,7 +7326,7 @@ static bool realloc_groupchats(const Memory *mem, GC_Session *c, uint32_t n) return true; } - GC_Chat *temp = (GC_Chat *)mem_vrealloc(mem, c->chats, n, sizeof(GC_Chat)); + GC_Chat *owner temp = (GC_Chat *owner)mem_vrealloc(mem, c->chats, n, sizeof(GC_Chat)); if (temp == nullptr) { return false; @@ -7357,7 +7357,7 @@ static int get_new_group_index(const Memory *mem, GC_Session *c) c->chats[new_index] = empty_gc_chat; - for (size_t i = 0; i < sizeof(c->chats[new_index].saved_invites) / sizeof(*c->chats[new_index].saved_invites); ++i) { + for (size_t i = 0; i < MAX_GC_SAVED_INVITES; ++i) { c->chats[new_index].saved_invites[i] = -1; } @@ -7377,7 +7377,7 @@ static void add_tcp_relays_to_chat(const GC_Session *c, GC_Chat *chat) return; } - Node_format *tcp_relays = (Node_format *)mem_valloc(chat->mem, num_relays, sizeof(Node_format)); + Node_format *owner tcp_relays = (Node_format *owner)mem_valloc(chat->mem, num_relays, sizeof(Node_format)); if (tcp_relays == nullptr) { return; @@ -7874,7 +7874,7 @@ int gc_invite_friend(const GC_Session *c, GC_Chat *chat, int32_t friend_number, assert(group_name_length <= MAX_GC_GROUP_NAME_SIZE); - uint8_t *packet = (uint8_t *)mem_balloc(chat->mem, 2 + CHAT_ID_SIZE + ENC_PUBLIC_KEY_SIZE + group_name_length); + uint8_t *owner packet = (uint8_t *owner)mem_balloc(chat->mem, 2 + CHAT_ID_SIZE + ENC_PUBLIC_KEY_SIZE + group_name_length); if (packet == nullptr) { return -1; @@ -7968,7 +7968,7 @@ static bool send_gc_invite_confirmed_packet(const Messenger *m, const GC_Chat *c } const uint16_t packet_length = 2 + length; - uint8_t *packet = (uint8_t *)mem_balloc(chat->mem, packet_length); + uint8_t *owner packet = (uint8_t *owner)mem_balloc(chat->mem, packet_length); if (packet == nullptr) { return false; @@ -8246,7 +8246,7 @@ GC_Session *new_dht_groupchats(Messenger *m) return nullptr; } - GC_Session *c = (GC_Session *)mem_alloc(m->mem, sizeof(GC_Session)); + GC_Session *owner c = (GC_Session *owner)mem_alloc(m->mem, sizeof(GC_Session)); if (c == nullptr) { return nullptr; @@ -8332,7 +8332,7 @@ static int kill_group(GC_Session *c, GC_Chat *chat) return ret; } -void kill_dht_groupchats(GC_Session *c) +void kill_dht_groupchats(GC_Session *owner c) { if (c == nullptr) { return; diff --git a/toxcore/group_common.h b/toxcore/group_common.h index 4c2e98ae52..42ff31365c 100644 --- a/toxcore/group_common.h +++ b/toxcore/group_common.h @@ -84,7 +84,7 @@ typedef struct GC_PeerAddress { } GC_PeerAddress; typedef struct GC_Message_Array_Entry { - uint8_t *data; + uint8_t *owner data; uint16_t data_length; uint8_t packet_type; uint64_t message_id; @@ -96,10 +96,10 @@ typedef struct GC_Connection { uint64_t send_message_id; /* message_id of the next message we send to peer */ uint16_t send_array_start; /* send_array index of oldest item */ - GC_Message_Array_Entry *send_array; + GC_Message_Array_Entry *owner send_array; uint64_t received_message_id; /* message_id of peer's last message to us */ - GC_Message_Array_Entry *recv_array; + GC_Message_Array_Entry *owner recv_array; uint64_t last_chunk_id; /* The message ID of the last packet fragment we received */ @@ -273,12 +273,12 @@ typedef struct GC_Chat { IP_Port self_ip_port; Networking_Core *net; - TCP_Connections *tcp_conn; + TCP_Connections *owner tcp_conn; uint64_t last_checked_tcp_relays; Group_Handshake_Join_Type join_type; - GC_Peer *group; + GC_Peer *owner group; Moderation moderation; GC_Conn_State connection_state; @@ -376,7 +376,7 @@ typedef void gc_rejected_cb(const Messenger *m, uint32_t group_number, unsigned typedef struct GC_Session { Messenger *messenger; - GC_Chat *chats; + GC_Chat *owner chats; struct GC_Announces_List *announces_list; uint32_t chats_index; diff --git a/toxcore/group_connection.c b/toxcore/group_connection.c index 79c982959f..4ed4cba2eb 100644 --- a/toxcore/group_connection.c +++ b/toxcore/group_connection.c @@ -109,7 +109,7 @@ static bool create_array_entry(const Logger *log, const Memory *mem, const Mono_ return false; } - uint8_t *entry_data = (uint8_t *)mem_balloc(mem, length); + uint8_t *owner entry_data = (uint8_t *owner)mem_balloc(mem, length); if (entry_data == nullptr) { return false; @@ -395,7 +395,7 @@ static uint16_t reassemble_packet(const Logger *log, const Memory *mem, GC_Conne return 0; } - uint8_t *tmp_payload = (uint8_t *)mem_balloc(mem, packet_length); + uint8_t *owner tmp_payload = (uint8_t *owner)mem_balloc(mem, packet_length); if (tmp_payload == nullptr) { LOGGER_ERROR(log, "Failed to allocate %u bytes for payload buffer", packet_length); @@ -441,7 +441,7 @@ int gcc_handle_packet_fragment(const GC_Session *c, GC_Chat *chat, uint32_t peer uint8_t sender_pk[ENC_PUBLIC_KEY_SIZE]; memcpy(sender_pk, get_enc_key(&gconn->addr.public_key), ENC_PUBLIC_KEY_SIZE); - uint8_t *payload = nullptr; + uint8_t *owner payload = nullptr; const uint16_t processed_len = reassemble_packet(chat->log, chat->mem, gconn, &payload, message_id); if (processed_len == 0) { @@ -621,7 +621,7 @@ int gcc_encrypt_and_send_lossless_packet(const GC_Chat *chat, const GC_Connectio uint16_t length, uint64_t message_id, uint8_t packet_type) { const uint16_t packet_size = gc_get_wrapped_packet_size(length, NET_PACKET_GC_LOSSLESS); - uint8_t *packet = (uint8_t *)mem_balloc(chat->mem, packet_size); + uint8_t *owner packet = (uint8_t *owner)mem_balloc(chat->mem, packet_size); if (packet == nullptr) { LOGGER_ERROR(chat->log, "Failed to allocate memory for packet buffer"); diff --git a/toxcore/group_moderation.c b/toxcore/group_moderation.c index acc17bd570..121f5bfa5f 100644 --- a/toxcore/group_moderation.c +++ b/toxcore/group_moderation.c @@ -51,7 +51,7 @@ int mod_list_unpack(Moderation *moderation, const uint8_t *data, uint16_t length return 0; } - uint8_t **tmp_list = (uint8_t **)mem_valloc(moderation->mem, num_mods, sizeof(uint8_t *)); + uint8_t **owner tmp_list = (uint8_t **owner)mem_valloc(moderation->mem, num_mods, sizeof(uint8_t *)); if (tmp_list == nullptr) { return -1; @@ -60,7 +60,7 @@ int mod_list_unpack(Moderation *moderation, const uint8_t *data, uint16_t length uint16_t unpacked_len = 0; for (uint16_t i = 0; i < num_mods; ++i) { - uint8_t *entry = (uint8_t *)mem_balloc(moderation->mem, MOD_LIST_ENTRY_SIZE); + uint8_t *owner entry = (uint8_t *owner)mem_balloc(moderation->mem, MOD_LIST_ENTRY_SIZE); if (entry == nullptr) { free_uint8_t_pointer_array(moderation->mem, tmp_list, i); @@ -102,7 +102,7 @@ bool mod_list_make_hash(const Moderation *moderation, uint8_t *hash) assert(data_buf_size > 0); - uint8_t *data = (uint8_t *)mem_balloc(moderation->mem, data_buf_size); + uint8_t *owner data = (uint8_t *owner)mem_balloc(moderation->mem, data_buf_size); if (data == nullptr) { return false; @@ -169,7 +169,7 @@ bool mod_list_remove_index(Moderation *moderation, uint16_t index) mem_delete(moderation->mem, moderation->mod_list[moderation->num_mods]); moderation->mod_list[moderation->num_mods] = nullptr; - uint8_t **tmp_list = (uint8_t **)mem_vrealloc(moderation->mem, moderation->mod_list, moderation->num_mods, sizeof(uint8_t *)); + uint8_t **owner tmp_list = (uint8_t **owner)mem_vrealloc(moderation->mem, moderation->mod_list, moderation->num_mods, sizeof(uint8_t *)); if (tmp_list == nullptr) { return false; @@ -203,7 +203,7 @@ bool mod_list_add_entry(Moderation *moderation, const uint8_t *mod_data) return false; } - uint8_t **tmp_list = (uint8_t **)mem_vrealloc(moderation->mem, moderation->mod_list, moderation->num_mods + 1, sizeof(uint8_t *)); + uint8_t **owner tmp_list = (uint8_t **owner)mem_vrealloc(moderation->mem, moderation->mod_list, moderation->num_mods + 1, sizeof(uint8_t *)); if (tmp_list == nullptr) { return false; @@ -211,7 +211,7 @@ bool mod_list_add_entry(Moderation *moderation, const uint8_t *mod_data) moderation->mod_list = tmp_list; - uint8_t *entry = (uint8_t *)mem_balloc(moderation->mem, MOD_LIST_ENTRY_SIZE); + uint8_t *owner entry = (uint8_t *owner)mem_balloc(moderation->mem, MOD_LIST_ENTRY_SIZE); if (entry == nullptr) { return false; @@ -422,7 +422,7 @@ static bool sanctions_list_make_hash(const Memory *mem, const Mod_Sanction *sanc return false; } - uint8_t *data = (uint8_t *)mem_balloc(mem, data_buf_size); + uint8_t *owner data = (uint8_t *owner)mem_balloc(mem, data_buf_size); if (data == nullptr) { return false; @@ -588,7 +588,7 @@ bool sanctions_list_check_integrity(const Moderation *moderation, const Mod_Sanc * @retval false if sanctions credentials validation fails. */ non_null(1, 2) nullable(3) -static bool sanctions_apply_new(Moderation *moderation, Mod_Sanction *new_sanctions, +static bool sanctions_apply_new(Moderation *moderation, Mod_Sanction *owner new_sanctions, const Mod_Sanction_Creds *new_creds, uint16_t num_sanctions) { @@ -612,9 +612,9 @@ static bool sanctions_apply_new(Moderation *moderation, Mod_Sanction *new_sancti * memory returned by this function. */ non_null() -static Mod_Sanction *sanctions_list_copy(const Memory *mem, const Mod_Sanction *sanctions, uint16_t num_sanctions) +static Mod_Sanction *owner sanctions_list_copy(const Memory *mem, const Mod_Sanction *sanctions, uint16_t num_sanctions) { - Mod_Sanction *copy = (Mod_Sanction *)mem_valloc(mem, num_sanctions, sizeof(Mod_Sanction)); + Mod_Sanction *owner copy = (Mod_Sanction *owner)mem_valloc(mem, num_sanctions, sizeof(Mod_Sanction)); if (copy == nullptr) { return nullptr; @@ -655,7 +655,7 @@ static bool sanctions_list_remove_index(Moderation *moderation, uint16_t index, } /* 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); + Mod_Sanction *owner sanctions_copy = sanctions_list_copy(moderation->mem, moderation->sanctions, moderation->num_sanctions); if (sanctions_copy == nullptr) { return false; @@ -665,7 +665,7 @@ static bool sanctions_list_remove_index(Moderation *moderation, uint16_t index, sanctions_copy[index] = sanctions_copy[new_num]; } - Mod_Sanction *new_list = (Mod_Sanction *)mem_vrealloc(moderation->mem, sanctions_copy, new_num, sizeof(Mod_Sanction)); + Mod_Sanction *owner new_list = (Mod_Sanction *owner)mem_vrealloc(moderation->mem, sanctions_copy, new_num, sizeof(Mod_Sanction)); if (new_list == nullptr) { mem_delete(moderation->mem, sanctions_copy); @@ -750,7 +750,7 @@ bool sanctions_list_add_entry(Moderation *moderation, const Mod_Sanction *sancti } /* Operate on a copy of the list in case something goes wrong. */ - Mod_Sanction *sanctions_copy = nullptr; + Mod_Sanction *owner sanctions_copy = nullptr; if (moderation->num_sanctions > 0) { sanctions_copy = sanctions_list_copy(moderation->mem, moderation->sanctions, moderation->num_sanctions); @@ -761,7 +761,7 @@ bool sanctions_list_add_entry(Moderation *moderation, const Mod_Sanction *sancti } const uint16_t index = moderation->num_sanctions; - Mod_Sanction *new_list = (Mod_Sanction *)mem_vrealloc(moderation->mem, sanctions_copy, index + 1, sizeof(Mod_Sanction)); + Mod_Sanction *owner new_list = (Mod_Sanction *owner)mem_vrealloc(moderation->mem, sanctions_copy, index + 1, sizeof(Mod_Sanction)); if (new_list == nullptr) { mem_delete(moderation->mem, sanctions_copy); diff --git a/toxcore/group_moderation.h b/toxcore/group_moderation.h index eeab32fc0a..08b48371b5 100644 --- a/toxcore/group_moderation.h +++ b/toxcore/group_moderation.h @@ -84,12 +84,12 @@ typedef struct Moderation { const Memory *mem; const Logger *log; - Mod_Sanction *sanctions; + Mod_Sanction *owner sanctions; uint16_t num_sanctions; Mod_Sanction_Creds sanctions_creds; - uint8_t **mod_list; // array of public signature keys of all the mods + uint8_t *owner *owner mod_list; // array of public signature keys of all the mods uint16_t num_mods; // copies from parent/sibling chat/shared state objects diff --git a/toxcore/group_pack.c b/toxcore/group_pack.c index 877df7409e..3c55cdaee5 100644 --- a/toxcore/group_pack.c +++ b/toxcore/group_pack.c @@ -183,7 +183,7 @@ static bool load_unpack_mod_list(GC_Chat *chat, Bin_Unpack *bu) chat->moderation.num_mods = MOD_MAX_NUM_MODERATORS; } - uint8_t *packed_mod_list = (uint8_t *)mem_balloc(chat->mem, chat->moderation.num_mods * MOD_LIST_ENTRY_SIZE); + uint8_t *owner packed_mod_list = (uint8_t *owner)mem_balloc(chat->mem, chat->moderation.num_mods * MOD_LIST_ENTRY_SIZE); if (packed_mod_list == nullptr) { LOGGER_ERROR(chat->log, "Failed to allocate memory for packed mod list"); @@ -302,7 +302,7 @@ static bool load_unpack_saved_peers(GC_Chat *chat, Bin_Unpack *bu) return true; } - uint8_t *saved_peers = (uint8_t *)mem_balloc(chat->mem, saved_peers_size * GC_SAVED_PEER_SIZE); + uint8_t *owner saved_peers = (uint8_t *owner)mem_balloc(chat->mem, saved_peers_size * GC_SAVED_PEER_SIZE); if (saved_peers == nullptr) { LOGGER_ERROR(chat->log, "Failed to allocate memory for saved peer list"); @@ -393,7 +393,7 @@ static void save_pack_mod_list(const GC_Chat *chat, Bin_Pack *bp) return; } - uint8_t *packed_mod_list = (uint8_t *)mem_balloc(chat->mem, num_mods * MOD_LIST_ENTRY_SIZE); + uint8_t *owner packed_mod_list = (uint8_t *owner)mem_balloc(chat->mem, num_mods * MOD_LIST_ENTRY_SIZE); // we can still recover without the mod list if (packed_mod_list == nullptr) { @@ -448,7 +448,7 @@ static void save_pack_saved_peers(const GC_Chat *chat, Bin_Pack *bp) { bin_pack_array(bp, 2); - uint8_t *saved_peers = (uint8_t *)mem_balloc(chat->mem, GC_MAX_SAVED_PEERS * GC_SAVED_PEER_SIZE); + uint8_t *owner saved_peers = (uint8_t *owner)mem_balloc(chat->mem, GC_MAX_SAVED_PEERS * GC_SAVED_PEER_SIZE); // we can still recover without the saved peers list if (saved_peers == nullptr) { diff --git a/toxcore/list.c b/toxcore/list.c index 689ad0a614..1e1ef70683 100644 --- a/toxcore/list.c +++ b/toxcore/list.c @@ -115,7 +115,7 @@ static bool resize(BS_List *list, uint32_t new_size) return true; } - uint8_t *data = (uint8_t *)mem_brealloc(list->mem, list->data, new_size * list->element_size); + uint8_t *owner data = (uint8_t *owner)mem_brealloc(list->mem, list->data, new_size * list->element_size); if (data == nullptr) { return false; @@ -123,7 +123,7 @@ static bool resize(BS_List *list, uint32_t new_size) list->data = data; - int *ids = (int *)mem_vrealloc(list->mem, list->ids, new_size, sizeof(int)); + int *owner ids = (int *owner)mem_vrealloc(list->mem, list->ids, new_size, sizeof(int)); if (ids == nullptr) { return false; diff --git a/toxcore/list.h b/toxcore/list.h index ae0ea32746..e4f7e6ca0d 100644 --- a/toxcore/list.h +++ b/toxcore/list.h @@ -30,8 +30,8 @@ typedef struct BS_List { uint32_t n; // number of elements uint32_t capacity; // number of elements memory is allocated for uint32_t element_size; // size of the elements - uint8_t *data; // array of elements - int *ids; // array of element ids + uint8_t *owner data; // array of elements + int *owner ids; // array of element ids bs_list_cmp_cb *cmp_callback; } BS_List; diff --git a/toxcore/logger.c b/toxcore/logger.c index b97ef8e184..d556e24d5e 100644 --- a/toxcore/logger.c +++ b/toxcore/logger.c @@ -29,9 +29,9 @@ struct Logger { * Public Functions */ -Logger *logger_new(const Memory *mem) +Logger *owner logger_new(const Memory *mem) { - Logger *log = (Logger *)mem_alloc(mem, sizeof(Logger)); + Logger *owner log = (Logger *owner)mem_alloc(mem, sizeof(Logger)); if (log == nullptr) { return nullptr; @@ -42,7 +42,7 @@ Logger *logger_new(const Memory *mem) return log; } -void logger_kill(Logger *log) +void logger_kill(Logger *owner log) { if (log == nullptr) { return; diff --git a/toxcore/logger.h b/toxcore/logger.h index 893c21fd10..ef618290dc 100644 --- a/toxcore/logger.h +++ b/toxcore/logger.h @@ -75,7 +75,7 @@ void logger_abort(void); #define LOGGER_WRITE(log, level, ...) \ do { \ if (level >= MIN_LOGGER_LEVEL) { \ - logger_write(log, level, __FILE__, __LINE__, __func__, __VA_ARGS__); \ + logger_write(log, level, "__FILE__", __LINE__, "__func__", __VA_ARGS__); \ } \ } while (0) diff --git a/toxcore/mem.c b/toxcore/mem.c index 32e7eec07c..0867e6506b 100644 --- a/toxcore/mem.c +++ b/toxcore/mem.c @@ -11,25 +11,25 @@ #include "ccompat.h" nullable(1) -static void *sys_malloc(void *obj, uint32_t size) +static void *owner sys_malloc(void *obj, uint32_t size) { return malloc(size); } nullable(1) -static void *sys_calloc(void *obj, uint32_t nmemb, uint32_t size) +static void *owner sys_calloc(void *obj, uint32_t nmemb, uint32_t size) { return calloc(nmemb, size); } nullable(1, 2) -static void *sys_realloc(void *obj, void *ptr, uint32_t size) +static void *owner sys_realloc(void *obj, void *ptr, uint32_t size) { return realloc(ptr, size); } nullable(1, 2) -static void sys_free(void *obj, void *ptr) +static void sys_free(void *obj, void *owner ptr) { free(ptr); } @@ -47,9 +47,9 @@ const Memory *os_memory(void) return &os_memory_obj; } -void *mem_balloc(const Memory *mem, uint32_t size) +void *owner mem_balloc(const Memory *mem, uint32_t size) { - void *const ptr = mem->funcs->malloc(mem->obj, size); + void *const owner ptr = mem->funcs->malloc(mem->obj, size); return ptr; } @@ -59,13 +59,13 @@ void *mem_brealloc(const Memory *mem, void *ptr, uint32_t size) return new_ptr; } -void *mem_alloc(const Memory *mem, uint32_t size) +void *owner mem_alloc(const Memory *mem, uint32_t size) { - void *const ptr = mem->funcs->calloc(mem->obj, 1, size); + void *const owner ptr = mem->funcs->calloc(mem->obj, 1, size); return ptr; } -void *mem_valloc(const Memory *mem, uint32_t nmemb, uint32_t size) +void *owner mem_valloc(const Memory *mem, uint32_t nmemb, uint32_t size) { const uint32_t bytes = nmemb * size; @@ -73,11 +73,11 @@ void *mem_valloc(const Memory *mem, uint32_t nmemb, uint32_t size) return nullptr; } - void *const ptr = mem->funcs->calloc(mem->obj, nmemb, size); + void *const owner ptr = mem->funcs->calloc(mem->obj, nmemb, size); return ptr; } -void *mem_vrealloc(const Memory *mem, void *ptr, uint32_t nmemb, uint32_t size) +void *owner mem_vrealloc(const Memory *mem, void *ptr, uint32_t nmemb, uint32_t size) { const uint32_t bytes = nmemb * size; @@ -85,11 +85,25 @@ void *mem_vrealloc(const Memory *mem, void *ptr, uint32_t nmemb, uint32_t size) return nullptr; } - void *const new_ptr = mem->funcs->realloc(mem->obj, ptr, bytes); + void *const owner new_ptr = mem->funcs->realloc(mem->obj, ptr, bytes); return new_ptr; } -void mem_delete(const Memory *mem, void *ptr) +void *owner mem_vresize(const Memory *mem, void *owner ptr, uint32_t nmemb, uint32_t size, bool *ok) +{ + void *const owner new_ptr = mem_vrealloc(mem, ptr, nmemb, size); + + if (new_ptr == nullptr) { + *ok = false; + return ptr; + } + + *ok = true; + static_set(ptr, "moved"); + return new_ptr; +} + +void mem_delete(const Memory *mem, void *owner ptr) { mem->funcs->free(mem->obj, ptr); } diff --git a/toxcore/mem.h b/toxcore/mem.h index 6c36027ce7..b1959f95da 100644 --- a/toxcore/mem.h +++ b/toxcore/mem.h @@ -9,6 +9,7 @@ #ifndef C_TOXCORE_TOXCORE_MEM_H #define C_TOXCORE_TOXCORE_MEM_H +#include #include // uint*_t #include "attributes.h" @@ -17,10 +18,10 @@ extern "C" { #endif -typedef void *mem_malloc_cb(void *obj, uint32_t size); -typedef void *mem_calloc_cb(void *obj, uint32_t nmemb, uint32_t size); -typedef void *mem_realloc_cb(void *obj, void *ptr, uint32_t size); -typedef void mem_free_cb(void *obj, void *ptr); +typedef void *owner mem_malloc_cb(void *obj, uint32_t size); +typedef void *owner mem_calloc_cb(void *obj, uint32_t nmemb, uint32_t size); +typedef void *owner mem_realloc_cb(void *obj, void *ptr, uint32_t size); +typedef void mem_free_cb(void *obj, void *owner ptr); /** @brief Functions wrapping standard C memory allocation functions. */ typedef struct Memory_Funcs { @@ -43,7 +44,7 @@ const Memory *os_memory(void); * The array will not be initialised. Supported built-in types are * `uint8_t`, `int8_t`, and `int16_t`. */ -non_null() void *mem_balloc(const Memory *mem, uint32_t size); +non_null() void *owner mem_balloc(const Memory *mem, uint32_t size); /** * @brief Resize an array of a given size for built-in types. @@ -58,14 +59,14 @@ non_null(1) nullable(2) void *mem_brealloc(const Memory *mem, void *ptr, uint32_ * * Always use as `(T *)mem_alloc(mem, sizeof(T))`. */ -non_null() void *mem_alloc(const Memory *mem, uint32_t size); +non_null() void *owner mem_alloc(const Memory *mem, uint32_t size); /** * @brief Allocate a vector (array) of objects. * * Always use as `(T *)mem_valloc(mem, N, sizeof(T))`. */ -non_null() void *mem_valloc(const Memory *mem, uint32_t nmemb, uint32_t size); +non_null() void *owner mem_valloc(const Memory *mem, uint32_t nmemb, uint32_t size); /** * @brief Resize an object vector. @@ -82,10 +83,29 @@ non_null() void *mem_valloc(const Memory *mem, uint32_t nmemb, uint32_t size); * case where the multiplication would overflow. If such an overflow occurs, * `mem_vrealloc()` returns `nullptr`. */ -non_null(1) nullable(2) void *mem_vrealloc(const Memory *mem, void *ptr, uint32_t nmemb, uint32_t size); +non_null(1) nullable(2) void *owner mem_vrealloc(const Memory *mem, void *ptr, uint32_t nmemb, uint32_t size); + +/** + * @brief Resize an object vector. + * + * Changes the size of (and possibly moves) the memory block pointed to by + * @p ptr to be large enough for an array of @p nmemb elements, each of which + * is @p size bytes. It is similar to the call + * + * @code + * ptr = realloc(ptr, nmemb * size); + * @endcode + * + * However, unlike that `realloc()` call, `mem_vresize()` fails safely in the + * case where the multiplication would overflow. If such an overflow occurs, + * `mem_vresize()` returns `ptr`. Also, if allocation fails in any way + * (overflow or out-of-memory), the original pointer is returned and `*ok` is + * set to `false`. On success, `*ok` is set to `true`. + */ +non_null(1, 5) nullable(2) void *owner mem_vresize(const Memory *mem, void *owner ptr, uint32_t nmemb, uint32_t size, bool *ok); /** @brief Free an array, object, or object vector. */ -non_null(1) nullable(2) void mem_delete(const Memory *mem, void *ptr); +non_null(1) nullable(2) void mem_delete(const Memory *mem, void *owner ptr); #ifdef __cplusplus } /* extern "C" */ diff --git a/toxcore/mono_time.c b/toxcore/mono_time.c index 8a3044c6f0..bb01367e79 100644 --- a/toxcore/mono_time.c +++ b/toxcore/mono_time.c @@ -44,7 +44,7 @@ struct Mono_Time { #ifndef ESP_PLATFORM /* protect `time` from concurrent access */ - pthread_rwlock_t *time_update_lock; + pthread_rwlock_t *owner time_update_lock; #endif /* ESP_PLATFORM */ mono_time_current_time_cb *current_time_callback; @@ -112,14 +112,14 @@ static uint64_t current_time_monotonic_default(void *user_data) Mono_Time *mono_time_new(const Memory *mem, mono_time_current_time_cb *current_time_callback, void *user_data) { - Mono_Time *mono_time = (Mono_Time *)mem_alloc(mem, sizeof(Mono_Time)); + Mono_Time *owner mono_time = (Mono_Time *owner)mem_alloc(mem, sizeof(Mono_Time)); if (mono_time == nullptr) { return nullptr; } #ifndef ESP_PLATFORM - pthread_rwlock_t *rwlock = (pthread_rwlock_t *)mem_alloc(mem, sizeof(pthread_rwlock_t)); + pthread_rwlock_t *owner rwlock = (pthread_rwlock_t *owner)mem_alloc(mem, sizeof(pthread_rwlock_t)); if (rwlock == nullptr) { mem_delete(mem, mono_time); @@ -152,7 +152,7 @@ Mono_Time *mono_time_new(const Memory *mem, mono_time_current_time_cb *current_t return mono_time; } -void mono_time_free(const Memory *mem, Mono_Time *mono_time) +void mono_time_free(const Memory *mem, Mono_Time *owner mono_time) { if (mono_time == nullptr) { return; diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index b9602415c6..780fde1dd7 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 *owner 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 *owner 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; @@ -139,9 +139,9 @@ struct Net_Crypto { const Network *ns; DHT *dht; - TCP_Connections *tcp_c; + TCP_Connections *owner tcp_c; - Crypto_Connection *crypto_connections; + Crypto_Connection *owner crypto_connections; uint32_t crypto_connections_length; /* Length of connections array. */ @@ -758,7 +758,7 @@ static int add_data_to_buffer(const Memory *mem, Packets_Array *array, uint32_t return -1; } - Packet_Data *new_d = (Packet_Data *)mem_alloc(mem, sizeof(Packet_Data)); + Packet_Data *owner new_d = (Packet_Data *owner)mem_alloc(mem, sizeof(Packet_Data)); if (new_d == nullptr) { return -1; @@ -814,7 +814,7 @@ static int64_t add_data_end_of_buffer(const Logger *logger, const Memory *mem, P return -1; } - Packet_Data *new_d = (Packet_Data *)mem_alloc(mem, sizeof(Packet_Data)); + Packet_Data *owner new_d = (Packet_Data *owner)mem_alloc(mem, sizeof(Packet_Data)); if (new_d == nullptr) { LOGGER_ERROR(logger, "packet data allocation failed"); @@ -1367,7 +1367,7 @@ static int new_temp_packet(const Net_Crypto *c, int crypt_connection_id, const u return -1; } - uint8_t *temp_packet = (uint8_t *)mem_balloc(c->mem, length); + uint8_t *owner temp_packet = (uint8_t *owner)mem_balloc(c->mem, length); if (temp_packet == nullptr) { return -1; @@ -1783,8 +1783,8 @@ static int realloc_cryptoconnection(Net_Crypto *c, uint32_t num) return 0; } - Crypto_Connection *newcrypto_connections = (Crypto_Connection *)mem_vrealloc( - c->mem, c->crypto_connections, num, sizeof(Crypto_Connection)); + Crypto_Connection *owner newcrypto_connections = + (Crypto_Connection *owner)mem_vrealloc(c->mem, c->crypto_connections, num, sizeof(Crypto_Connection)); if (newcrypto_connections == nullptr) { return -1; @@ -1959,7 +1959,7 @@ non_null(1, 2, 3) nullable(5) static int handle_new_connection_handshake(Net_Crypto *c, const IP_Port *source, const uint8_t *data, uint16_t length, void *userdata) { - uint8_t *cookie = (uint8_t *)mem_balloc(c->mem, COOKIE_LENGTH); + uint8_t *owner cookie = (uint8_t *owner)mem_balloc(c->mem, COOKIE_LENGTH); if (cookie == nullptr) { return -1; @@ -2994,7 +2994,7 @@ Net_Crypto *new_net_crypto(const Logger *log, const Memory *mem, const Random *r return nullptr; } - Net_Crypto *temp = (Net_Crypto *)mem_alloc(mem, sizeof(Net_Crypto)); + Net_Crypto *owner temp = (Net_Crypto *owner)mem_alloc(mem, sizeof(Net_Crypto)); if (temp == nullptr) { return nullptr; @@ -3077,7 +3077,7 @@ void do_net_crypto(Net_Crypto *c, void *userdata) send_crypto_packets(c); } -void kill_net_crypto(Net_Crypto *c) +void kill_net_crypto(Net_Crypto *owner c) { if (c == nullptr) { return; diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h index c21ac7d217..71a9d645af 100644 --- a/toxcore/net_crypto.h +++ b/toxcore/net_crypto.h @@ -130,7 +130,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 *cookie; + uint8_t *owner cookie; uint8_t cookie_length; } New_Connection; diff --git a/toxcore/network.c b/toxcore/network.c index 396d56b215..1fe7f62d07 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -1271,7 +1271,7 @@ Networking_Core *new_networking_ex( return nullptr; } - Networking_Core *temp = (Networking_Core *)mem_alloc(mem, sizeof(Networking_Core)); + Networking_Core *owner temp = (Networking_Core *owner)mem_alloc(mem, sizeof(Networking_Core)); if (temp == nullptr) { return nullptr; @@ -1484,7 +1484,7 @@ Networking_Core *new_networking_ex( Networking_Core *new_networking_no_udp(const Logger *log, const Memory *mem, const Network *ns) { /* this is the easiest way to completely disable UDP without changing too much code. */ - Networking_Core *net = (Networking_Core *)mem_alloc(mem, sizeof(Networking_Core)); + Networking_Core *owner net = (Networking_Core *owner)mem_alloc(mem, sizeof(Networking_Core)); if (net == nullptr) { return nullptr; @@ -1498,7 +1498,7 @@ Networking_Core *new_networking_no_udp(const Logger *log, const Memory *mem, con } /** Function to cleanup networking stuff (doesn't do much right now). */ -void kill_networking(Networking_Core *net) +void kill_networking(Networking_Core *owner net) { if (net == nullptr) { return; @@ -2121,7 +2121,7 @@ int32_t net_getipport(const Network *ns, const Memory *mem, const char *node, IP *res = nullptr; if (addr_parse_ip(node, &parsed.ip)) { - IP_Port *tmp = (IP_Port *)mem_alloc(mem, sizeof(IP_Port)); + IP_Port *owner tmp = (IP_Port *owner)mem_alloc(mem, sizeof(IP_Port)); if (tmp == nullptr) { return -1; @@ -2138,7 +2138,7 @@ int32_t net_getipport(const Network *ns, const Memory *mem, const char *node, IP #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION if ((true)) { - IP_Port *ip_port = (IP_Port *)mem_alloc(mem, sizeof(IP_Port)); + IP_Port *owner ip_port = (IP_Port *owner)mem_alloc(mem, sizeof(IP_Port)); if (ip_port == nullptr) { abort(); } @@ -2186,7 +2186,7 @@ int32_t net_getipport(const Network *ns, const Memory *mem, const char *node, IP return 0; } - IP_Port *ip_port = (IP_Port *)mem_valloc(mem, count, sizeof(IP_Port)); + IP_Port *owner ip_port = (IP_Port *owner)mem_valloc(mem, count, sizeof(IP_Port)); if (ip_port == nullptr) { ns->funcs->freeaddrinfo(ns->obj, mem, addrs); @@ -2225,7 +2225,7 @@ int32_t net_getipport(const Network *ns, const Memory *mem, const char *node, IP return count; } -void net_freeipport(const Memory *mem, IP_Port *ip_ports) +void net_freeipport(const Memory *mem, IP_Port *owner ip_ports) { mem_delete(mem, ip_ports); } @@ -2407,7 +2407,7 @@ static const char *net_strerror_r(int error, char *tmp, size_t tmp_size) return tmp; } #endif /* GNU */ -char *net_new_strerror(int error) +char *owner net_new_strerror(int error) { char tmp[256]; @@ -2416,7 +2416,7 @@ char *net_new_strerror(int error) const char *retstr = net_strerror_r(error, tmp, sizeof(tmp)); const size_t retstr_len = strlen(retstr); - char *str = (char *)malloc(retstr_len + 1); + char *owner str = (char *owner)malloc(retstr_len + 1); if (str == nullptr) { return nullptr; @@ -2428,10 +2428,10 @@ char *net_new_strerror(int error) } #endif /* OS_WIN32 */ -void net_kill_strerror(char *strerror) +void net_kill_strerror(char *owner strerror) { #ifdef OS_WIN32 - LocalFree((char *)strerror); + LocalFree(strerror); #else free(strerror); #endif /* OS_WIN32 */ diff --git a/toxcore/onion.c b/toxcore/onion.c index f948be52cb..a0c221cc4c 100644 --- a/toxcore/onion.c +++ b/toxcore/onion.c @@ -720,7 +720,7 @@ Onion *new_onion(const Logger *log, const Memory *mem, const Mono_Time *mono_tim return nullptr; } - Onion *onion = (Onion *)mem_alloc(mem, sizeof(Onion)); + Onion *owner onion = (Onion *owner)mem_alloc(mem, sizeof(Onion)); if (onion == nullptr) { return nullptr; @@ -759,7 +759,7 @@ Onion *new_onion(const Logger *log, const Memory *mem, const Mono_Time *mono_tim return onion; } -void kill_onion(Onion *onion) +void kill_onion(Onion *owner onion) { if (onion == nullptr) { return; diff --git a/toxcore/onion_announce.c b/toxcore/onion_announce.c index 50e2e00aef..7e3b8a44e0 100644 --- a/toxcore/onion_announce.c +++ b/toxcore/onion_announce.c @@ -493,7 +493,7 @@ static int handle_announce_request_common( return 1; } - uint8_t *plain = (uint8_t *)mem_balloc(onion_a->mem, plain_size); + uint8_t *owner plain = (uint8_t *owner)mem_balloc(onion_a->mem, plain_size); if (plain == nullptr) { return 1; @@ -544,7 +544,7 @@ static int handle_announce_request_common( const uint16_t response_size = nodes_offset + MAX_SENT_NODES * PACKED_NODE_SIZE_IP6 + max_extra_size; - uint8_t *response = (uint8_t *)mem_balloc(onion_a->mem, response_size); + uint8_t *owner response = (uint8_t *owner)mem_balloc(onion_a->mem, response_size); if (response == nullptr) { mem_delete(onion_a->mem, plain); @@ -707,7 +707,7 @@ Onion_Announce *new_onion_announce(const Logger *log, const Memory *mem, const R return nullptr; } - Onion_Announce *onion_a = (Onion_Announce *)mem_alloc(mem, sizeof(Onion_Announce)); + Onion_Announce *owner onion_a = (Onion_Announce *owner)mem_alloc(mem, sizeof(Onion_Announce)); if (onion_a == nullptr) { return nullptr; @@ -738,7 +738,7 @@ Onion_Announce *new_onion_announce(const Logger *log, const Memory *mem, const R return onion_a; } -void kill_onion_announce(Onion_Announce *onion_a) +void kill_onion_announce(Onion_Announce *owner onion_a) { if (onion_a == nullptr) { return; diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c index 7f76c83a01..99a1fe1038 100644 --- a/toxcore/onion_client.c +++ b/toxcore/onion_client.c @@ -124,7 +124,7 @@ struct Onion_Client { DHT *dht; Net_Crypto *c; Networking_Core *net; - Onion_Friend *friends_list; + Onion_Friend *owner friends_list; uint16_t num_friends; Onion_Node clients_announce_list[MAX_ONION_CLIENTS_ANNOUNCE]; @@ -1558,7 +1558,7 @@ static int realloc_onion_friends(Onion_Client *onion_c, uint32_t num) return 0; } - Onion_Friend *newonion_friends = (Onion_Friend *)mem_vrealloc(onion_c->mem, onion_c->friends_list, num, sizeof(Onion_Friend)); + Onion_Friend *owner newonion_friends = (Onion_Friend *owner)mem_vrealloc(onion_c->mem, onion_c->friends_list, num, sizeof(Onion_Friend)); if (newonion_friends == nullptr) { return -1; @@ -2225,7 +2225,7 @@ Onion_Client *new_onion_client(const Logger *logger, const Memory *mem, const Ra return nullptr; } - Onion_Client *onion_c = (Onion_Client *)mem_alloc(mem, sizeof(Onion_Client)); + Onion_Client *owner onion_c = (Onion_Client *owner)mem_alloc(mem, sizeof(Onion_Client)); if (onion_c == nullptr) { return nullptr; @@ -2257,7 +2257,7 @@ Onion_Client *new_onion_client(const Logger *logger, const Memory *mem, const Ra return onion_c; } -void kill_onion_client(Onion_Client *onion_c) +void kill_onion_client(Onion_Client *owner onion_c) { if (onion_c == nullptr) { return; diff --git a/toxcore/ping.c b/toxcore/ping.c index bfb3c92cd3..1654ee79d8 100644 --- a/toxcore/ping.c +++ b/toxcore/ping.c @@ -334,7 +334,7 @@ void ping_iterate(Ping *ping) Ping *ping_new(const Memory *mem, const Mono_Time *mono_time, const Random *rng, DHT *dht) { - Ping *ping = (Ping *)mem_alloc(mem, sizeof(Ping)); + Ping *owner ping = (Ping *owner)mem_alloc(mem, sizeof(Ping)); if (ping == nullptr) { return nullptr; @@ -357,7 +357,7 @@ Ping *ping_new(const Memory *mem, const Mono_Time *mono_time, const Random *rng, return ping; } -void ping_kill(const Memory *mem, Ping *ping) +void ping_kill(const Memory *mem, Ping *owner ping) { if (ping == nullptr) { return; diff --git a/toxcore/ping_array.c b/toxcore/ping_array.c index 91b9064ecc..760b9ca29f 100644 --- a/toxcore/ping_array.c +++ b/toxcore/ping_array.c @@ -17,7 +17,7 @@ #include "mono_time.h" typedef struct Ping_Array_Entry { - uint8_t *data; + uint8_t *owner data; uint32_t length; uint64_t ping_time; uint64_t ping_id; @@ -25,7 +25,7 @@ typedef struct Ping_Array_Entry { struct Ping_Array { const Memory *mem; - Ping_Array_Entry *entries; + Ping_Array_Entry *owner 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. */ @@ -44,13 +44,13 @@ Ping_Array *ping_array_new(const Memory *mem, uint32_t size, uint32_t timeout) return nullptr; } - Ping_Array *const empty_array = (Ping_Array *)mem_alloc(mem, sizeof(Ping_Array)); + Ping_Array *const owner empty_array = (Ping_Array *owner)mem_alloc(mem, sizeof(Ping_Array)); if (empty_array == nullptr) { return nullptr; } - Ping_Array_Entry *entries = (Ping_Array_Entry *)mem_valloc(mem, size, sizeof(Ping_Array_Entry)); + Ping_Array_Entry *owner entries = (Ping_Array_Entry *owner)mem_valloc(mem, size, sizeof(Ping_Array_Entry)); if (entries == nullptr) { mem_delete(mem, empty_array); @@ -74,7 +74,7 @@ static void clear_entry(Ping_Array *array, uint32_t index) array->entries[index] = empty; } -void ping_array_kill(Ping_Array *array) +void ping_array_kill(Ping_Array *owner array) { if (array == nullptr) { return; @@ -117,7 +117,7 @@ uint64_t ping_array_add(Ping_Array *array, const Mono_Time *mono_time, const Ran clear_entry(array, index); } - uint8_t *entry_data = (uint8_t *)mem_balloc(array->mem, length); + uint8_t *owner entry_data = (uint8_t *owner)mem_balloc(array->mem, length); if (entry_data == nullptr) { array->entries[index].data = nullptr; diff --git a/toxcore/shared_key_cache.c b/toxcore/shared_key_cache.c index 64906b569c..f4cd3517ff 100644 --- a/toxcore/shared_key_cache.c +++ b/toxcore/shared_key_cache.c @@ -21,7 +21,7 @@ typedef struct Shared_Key { } Shared_Key; struct Shared_Key_Cache { - Shared_Key *keys; + Shared_Key *owner keys; const uint8_t *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; @@ -61,7 +61,7 @@ Shared_Key_Cache *shared_key_cache_new(const Logger *log, const Mono_Time *mono_ return nullptr; } - Shared_Key_Cache *res = (Shared_Key_Cache *)mem_alloc(mem, sizeof(Shared_Key_Cache)); + Shared_Key_Cache *owner res = (Shared_Key_Cache *owner)mem_alloc(mem, sizeof(Shared_Key_Cache)); if (res == nullptr) { return nullptr; } @@ -74,7 +74,7 @@ Shared_Key_Cache *shared_key_cache_new(const Logger *log, const Mono_Time *mono_ // We take one byte from the public key for each bucket and store keys_per_slot elements there const size_t cache_size = 256 * keys_per_slot; - Shared_Key *keys = (Shared_Key *)mem_valloc(mem, cache_size, sizeof(Shared_Key)); + Shared_Key *owner keys = (Shared_Key *owner)mem_valloc(mem, cache_size, sizeof(Shared_Key)); if (keys == nullptr) { mem_delete(mem, res); @@ -88,7 +88,7 @@ Shared_Key_Cache *shared_key_cache_new(const Logger *log, const Mono_Time *mono_ return res; } -void shared_key_cache_free(Shared_Key_Cache *cache) +void shared_key_cache_free(Shared_Key_Cache *owner cache) { if (cache == nullptr) { return; diff --git a/toxcore/tox.c b/toxcore/tox.c index 7349928e4d..219f601345 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -803,7 +803,7 @@ static Tox *tox_new_system(const struct Tox_Options *options, Tox_Err_New *error m_options.local_discovery_enabled = false; } - Tox *tox = (Tox *)mem_alloc(sys->mem, sizeof(Tox)); + Tox *owner tox = (Tox *owner)mem_alloc(sys->mem, sizeof(Tox)); if (tox == nullptr) { SET_ERROR_PARAMETER(error, TOX_ERR_NEW_MALLOC); @@ -881,7 +881,7 @@ static Tox *tox_new_system(const struct Tox_Options *options, Tox_Err_New *error } 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 *owner mutex = (pthread_mutex_t *owner)mem_alloc(sys->mem, sizeof(pthread_mutex_t)); if (mutex == nullptr) { SET_ERROR_PARAMETER(error, TOX_ERR_NEW_MALLOC); @@ -1054,7 +1054,7 @@ Tox *tox_new_testing(const Tox_Options *options, Tox_Err_New *error, const Tox_O return tox_new_system(options, error, sys); } -void tox_kill(Tox *tox) +void tox_kill(Tox *owner tox) { if (tox == nullptr) { return; @@ -2085,11 +2085,12 @@ 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); - file_id = f_id; + } else { + memcpy(f_id, file_id, TOX_FILE_ID_LENGTH); } tox_lock(tox); - const long int file_num = new_filesender(tox->m, friend_number, kind, file_size, file_id, filename, filename_length); + const long int file_num = new_filesender(tox->m, friend_number, kind, file_size, f_id, filename, filename_length); tox_unlock(tox); if (file_num >= 0) { diff --git a/toxcore/tox_events.c b/toxcore/tox_events.c index 0f2b1f2b8f..acfa5af853 100644 --- a/toxcore/tox_events.c +++ b/toxcore/tox_events.c @@ -162,7 +162,7 @@ static bool tox_events_unpack_handler(void *obj, Bin_Unpack *bu) 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 *owner events = (Tox_Events *owner)mem_alloc(sys->mem, sizeof(Tox_Events)); if (events == nullptr) { return nullptr; @@ -193,8 +193,8 @@ 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 *owner a_bytes = (uint8_t *owner)mem_balloc(sys->mem, a_size); + uint8_t *owner b_bytes = (uint8_t *owner)mem_balloc(sys->mem, b_size); if (a_bytes == nullptr || b_bytes == nullptr) { mem_delete(sys->mem, b_bytes); diff --git a/toxcore/tox_struct.h b/toxcore/tox_struct.h index 88f74ead62..0e2bfddfa3 100644 --- a/toxcore/tox_struct.h +++ b/toxcore/tox_struct.h @@ -19,10 +19,10 @@ extern "C" { #endif struct Tox { - Messenger *m; + Messenger *owner m; Mono_Time *mono_time; Tox_System sys; - pthread_mutex_t *mutex; + pthread_mutex_t *owner mutex; tox_log_cb *log_callback; tox_self_connection_status_cb *self_connection_status_callback; diff --git a/toxcore/util.c b/toxcore/util.c index 455e513cef..c3b5fce090 100644 --- a/toxcore/util.c +++ b/toxcore/util.c @@ -23,7 +23,7 @@ bool is_power_of_2(uint64_t x) return x != 0 && (x & (~x + 1)) == x; } -void free_uint8_t_pointer_array(const Memory *mem, uint8_t **ary, size_t n_items) +void free_uint8_t_pointer_array(const Memory *mem, uint8_t *owner *owner ary, size_t n_items) { if (ary == nullptr) { return; @@ -80,13 +80,13 @@ bool memeq(const uint8_t *a, size_t a_size, const uint8_t *b, size_t b_size) return a_size == b_size && memcmp(a, b, a_size) == 0; } -uint8_t *memdup(const Memory *mem, const uint8_t *data, size_t data_size) +uint8_t *owner memdup(const Memory *mem, const uint8_t *data, size_t data_size) { if (data == nullptr || data_size == 0) { return nullptr; } - uint8_t *copy = (uint8_t *)mem_balloc(mem, data_size); + uint8_t *owner copy = (uint8_t *owner)mem_balloc(mem, data_size); if (copy != nullptr) { memcpy(copy, data, data_size); diff --git a/toxcore/util.h b/toxcore/util.h index 9be721318b..46c458618b 100644 --- a/toxcore/util.h +++ b/toxcore/util.h @@ -46,7 +46,7 @@ non_null() bool memeq(const uint8_t *a, size_t a_size, const uint8_t *b, size_t * * @return nullptr on allocation failure or if the input data was nullptr or data_size was 0. */ -non_null(1) nullable(2) uint8_t *memdup(const Memory *mem, const uint8_t *data, size_t data_size); +non_null(1) nullable(2) uint8_t *owner memdup(const Memory *mem, const uint8_t *data, size_t data_size); /** * @brief Set all bytes in `data` to 0.