diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7988ea26b1a..a6b5c8f2ab5 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-latest 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 00000000000..bc0351ae3e5 --- /dev/null +++ b/other/docker/cake/cake.Dockerfile @@ -0,0 +1,74 @@ +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="b725baa328206cb8a11776b470255cec8efcb170" +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-multichar -Wno-int-conversion -Wno-unused-but-set-variable -Wno-incompatible-pointer-types-discards-qualifiers -Wno-unused-function -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 -Wno-analyzer-maybe-uninitialized" + +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 "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 00000000000..71cc148807e --- /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 00000000000..ea132355abc --- /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 00000000000..690efd47e5b --- /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 00000000000..5a0f34bc843 --- /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 2567d1b5a8f..2f5672e6575 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -110,10 +110,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; @@ -370,7 +370,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; @@ -920,7 +920,7 @@ static void sort_client_list(const Memory *mem, Client_data *list, uint64_t cur_ { // Pass comp_public_key to qsort with each Client_data entry, so the // comparison function can use it as the base of comparison. - DHT_Cmp_Data *cmp_list = (DHT_Cmp_Data *)mem_valloc(mem, length, sizeof(DHT_Cmp_Data)); + DHT_Cmp_Data *owner cmp_list = (DHT_Cmp_Data *owner)mem_valloc(mem, length, sizeof(DHT_Cmp_Data)); if (cmp_list == nullptr) { return; @@ -1591,13 +1591,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); @@ -1641,13 +1642,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; } @@ -1694,8 +1696,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; @@ -2513,15 +2517,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)); @@ -2620,7 +2625,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; @@ -2693,7 +2698,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); @@ -2790,7 +2796,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 19a9e1d9370..dde834e4dae 100644 --- a/toxcore/DHT.h +++ b/toxcore/DHT.h @@ -463,11 +463,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 aead9759116..3f0e839b3c8 100644 --- a/toxcore/LAN_discovery.c +++ b/toxcore/LAN_discovery.c @@ -53,15 +53,15 @@ struct Broadcast_Info { #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) non_null() -static Broadcast_Info *fetch_broadcast_info(const Network *ns) +static Broadcast_Info *owner fetch_broadcast_info(const Network *ns) { - Broadcast_Info *broadcast = (Broadcast_Info *)calloc(1, sizeof(Broadcast_Info)); + Broadcast_Info *owner broadcast = (Broadcast_Info *owner)calloc(1, sizeof(Broadcast_Info)); if (broadcast == nullptr) { return nullptr; } - IP_ADAPTER_INFO *adapter_info = (IP_ADAPTER_INFO *)malloc(sizeof(IP_ADAPTER_INFO)); + IP_ADAPTER_INFO *owner adapter_info = (IP_ADAPTER_INFO *owner)malloc(sizeof(IP_ADAPTER_INFO)); if (adapter_info == nullptr) { free(broadcast); @@ -72,7 +72,7 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns) if (GetAdaptersInfo(adapter_info, &out_buf_len) == ERROR_BUFFER_OVERFLOW) { free(adapter_info); - IP_ADAPTER_INFO *new_adapter_info = (IP_ADAPTER_INFO *)malloc(out_buf_len); + IP_ADAPTER_INFO *owner new_adapter_info = (IP_ADAPTER_INFO *owner)malloc(out_buf_len); if (new_adapter_info == nullptr) { free(broadcast); @@ -122,9 +122,9 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns) #elif !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) && (defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__)) non_null() -static Broadcast_Info *fetch_broadcast_info(const Network *ns) +static Broadcast_Info *owner fetch_broadcast_info(const Network *ns) { - Broadcast_Info *broadcast = (Broadcast_Info *)calloc(1, sizeof(Broadcast_Info)); + Broadcast_Info *owner broadcast = (Broadcast_Info *owner)calloc(1, sizeof(Broadcast_Info)); if (broadcast == nullptr) { return nullptr; @@ -197,9 +197,9 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns) #else // TODO(irungentoo): Other platforms? non_null() -static Broadcast_Info *fetch_broadcast_info(const Network *ns) +static Broadcast_Info *owner fetch_broadcast_info(const Network *ns) { - return (Broadcast_Info *)calloc(1, sizeof(Broadcast_Info)); + return (Broadcast_Info *owner)calloc(1, sizeof(Broadcast_Info)); } #endif /* platforms */ @@ -375,12 +375,12 @@ bool lan_discovery_send(const Networking_Core *net, const Broadcast_Info *broadc return res; } -Broadcast_Info *lan_discovery_init(const Network *ns) +Broadcast_Info *owner lan_discovery_init(const Network *ns) { return fetch_broadcast_info(ns); } -void lan_discovery_kill(Broadcast_Info *broadcast) +void lan_discovery_kill(Broadcast_Info *owner broadcast) { free(broadcast); } diff --git a/toxcore/LAN_discovery.h b/toxcore/LAN_discovery.h index 94e5d3581c8..6abd4eeb1f4 100644 --- a/toxcore/LAN_discovery.h +++ b/toxcore/LAN_discovery.h @@ -32,13 +32,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 Network *ns); +Broadcast_Info *owner lan_discovery_init(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 d20d740577f..6d5706fd51a 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); @@ -2949,14 +2949,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; @@ -3543,8 +3543,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; @@ -3554,7 +3555,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; @@ -3763,7 +3764,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 cace3340b26..f582ff5c3e8 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -84,14 +84,14 @@ 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; } Messenger_Options; struct Receipts { uint32_t packet_num; uint32_t msg_id; - struct Receipts *next; + struct Receipts *owner next; }; /** Status definitions. */ @@ -245,7 +245,7 @@ typedef struct Friend { RTP_Packet_Handler lossy_rtp_packethandlers[PACKET_ID_RANGE_LOSSY_AV_SIZE]; - struct Receipts *receipts_start; + struct Receipts *owner receipts_start; struct Receipts *receipts_end; } Friend; @@ -258,10 +258,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; @@ -269,7 +269,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; @@ -279,7 +279,7 @@ struct Messenger { Userstatus userstatus; - Friend *friendlist; + Friend *owner friendlist; uint32_t numfriends; uint64_t lastdump; @@ -811,15 +811,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 2b8c6e448f5..4dccb979c9d 100644 --- a/toxcore/TCP_client.c +++ b/toxcore/TCP_client.c @@ -579,7 +579,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) @@ -622,7 +622,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) { kill_sock(ns, sock); @@ -1014,7 +1014,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 ea2654b953b..ce15e71edc7 100644 --- a/toxcore/TCP_client.h +++ b/toxcore/TCP_client.h @@ -61,7 +61,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) -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); @@ -73,7 +73,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 bd3b7ca4c74..224038c08ee 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); @@ -62,7 +62,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; @@ -76,7 +76,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); @@ -100,13 +100,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 9fa136609ca..da2c5cc7b8a 100644 --- a/toxcore/TCP_common.h +++ b/toxcore/TCP_common.h @@ -14,14 +14,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) @@ -64,7 +64,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; } TCP_Connection; diff --git a/toxcore/TCP_connection.c b/toxcore/TCP_connection.c index c7161a92894..126d1fdab52 100644 --- a/toxcore/TCP_connection.c +++ b/toxcore/TCP_connection.c @@ -34,10 +34,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; @@ -77,7 +77,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); @@ -85,8 +85,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; @@ -98,7 +98,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); @@ -106,7 +106,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; @@ -1603,7 +1604,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; @@ -1711,7 +1712,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 2d35919f10e..7bd2eef0081 100644 --- a/toxcore/TCP_connection.h +++ b/toxcore/TCP_connection.h @@ -66,7 +66,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; @@ -305,8 +305,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); @@ -315,6 +316,6 @@ 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); #endif /* C_TOXCORE_TOXCORE_TCP_CONNECTION_H */ diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c index 1363d902207..d8a9fbf513f 100644 --- a/toxcore/TCP_server.c +++ b/toxcore/TCP_server.c @@ -74,7 +74,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]; @@ -84,7 +84,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; @@ -127,7 +127,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)); @@ -962,7 +962,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"); @@ -974,7 +974,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"); @@ -1389,7 +1389,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 1d3933a415e..7ae641592c0 100644 --- a/toxcore/TCP_server.h +++ b/toxcore/TCP_server.h @@ -40,9 +40,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() @@ -50,6 +51,6 @@ 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); #endif /* C_TOXCORE_TOXCORE_TCP_SERVER_H */ diff --git a/toxcore/announce.c b/toxcore/announce.c index b983cb0574a..ec734596bb3 100644 --- a/toxcore/announce.c +++ b/toxcore/announce.c @@ -52,7 +52,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; @@ -241,7 +241,7 @@ bool announce_store_data(Announcements *announce, const uint8_t *data_public_key free(entry->data); - uint8_t *entry_data = (uint8_t *)malloc(length); + uint8_t *owner entry_data = (uint8_t *owner)malloc(length); if (entry_data == nullptr) { entry->data = nullptr; // TODO(iphydf): Is this necessary? @@ -644,14 +644,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 *)calloc(1, sizeof(Announcements)); + Announcements *owner announce = (Announcements *owner)calloc(1, sizeof(Announcements)); if (announce == nullptr) { return nullptr; @@ -684,7 +685,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 bd05088d06c..15524042835 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 087efe20dcb..177b9a9b072 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 d6b1c52ea56..5f8d02c2153 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 *const 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 47ab9ed2bf8..225f9f3aa98 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 7bb5bb929fb..66a098030d3 100644 --- a/toxcore/crypto_core.c +++ b/toxcore/crypto_core.c @@ -88,9 +88,9 @@ const uint8_t *get_chat_id(const Extended_Public_Key *key) } #if !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION) -static uint8_t *crypto_malloc(size_t bytes) +static uint8_t *owner crypto_malloc(size_t bytes) { - uint8_t *ptr = (uint8_t *)malloc(bytes); + uint8_t *owner ptr = (uint8_t *owner)malloc(bytes); if (ptr != nullptr) { crypto_memlock(ptr, bytes); @@ -100,7 +100,7 @@ static uint8_t *crypto_malloc(size_t bytes) } nullable(1) -static void crypto_free(uint8_t *ptr, size_t bytes) +static void crypto_free(uint8_t *owner ptr, size_t bytes) { if (ptr != nullptr) { crypto_memzero(ptr, bytes); @@ -258,8 +258,8 @@ int32_t encrypt_data_symmetric(const uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE], 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(size_temp_plain); - uint8_t *temp_encrypted = crypto_malloc(size_temp_encrypted); + uint8_t *owner temp_plain = crypto_malloc(size_temp_plain); + uint8_t *owner temp_encrypted = crypto_malloc(size_temp_encrypted); if (temp_plain == nullptr || temp_encrypted == nullptr) { crypto_free(temp_plain, size_temp_plain); @@ -310,8 +310,8 @@ int32_t decrypt_data_symmetric(const uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE], 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(size_temp_plain); - uint8_t *temp_encrypted = crypto_malloc(size_temp_encrypted); + uint8_t *owner temp_plain = crypto_malloc(size_temp_plain); + uint8_t *owner temp_encrypted = crypto_malloc(size_temp_encrypted); if (temp_plain == nullptr || temp_encrypted == nullptr) { crypto_free(temp_plain, size_temp_plain); diff --git a/toxcore/forwarding.c b/toxcore/forwarding.c index 18ff3203fc4..b20f96ca6f1 100644 --- a/toxcore/forwarding.c +++ b/toxcore/forwarding.c @@ -357,13 +357,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 Random *rng, const Mono_Time *mono_time, DHT *dht) +Forwarding *owner new_forwarding( + const Logger *log, const Random *rng, const Mono_Time *mono_time, DHT *dht) { if (log == nullptr || mono_time == nullptr || dht == nullptr) { return nullptr; } - Forwarding *forwarding = (Forwarding *)calloc(1, sizeof(Forwarding)); + Forwarding *owner forwarding = (Forwarding *owner)calloc(1, sizeof(Forwarding)); if (forwarding == nullptr) { return nullptr; @@ -384,7 +385,7 @@ Forwarding *new_forwarding(const Logger *log, const Random *rng, const Mono_Time 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 bd0ef09e1c4..484acda100c 100644 --- a/toxcore/forwarding.h +++ b/toxcore/forwarding.h @@ -116,10 +116,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 Random *rng, const Mono_Time *mono_time, DHT *dht); +Forwarding *owner new_forwarding( + const Logger *log, 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 33bfa40d346..521eb2369d1 100644 --- a/toxcore/friend_connection.c +++ b/toxcore/friend_connection.c @@ -72,10 +72,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; @@ -131,7 +131,8 @@ static bool realloc_friendconns(Friend_Connections *fr_c, uint32_t num) return true; } - Friend_Conn *newgroup_cons = (Friend_Conn *)realloc(fr_c->conns, num * sizeof(Friend_Conn)); + Friend_Conn *owner newgroup_cons = + (Friend_Conn *owner)realloc(fr_c->conns, num * sizeof(Friend_Conn)); if (newgroup_cons == nullptr) { return false; @@ -915,7 +916,7 @@ Friend_Connections *new_friend_connections( return nullptr; } - Friend_Connections *const temp = (Friend_Connections *)calloc(1, sizeof(Friend_Connections)); + Friend_Connections *const owner temp = (Friend_Connections *owner)calloc(1, sizeof(Friend_Connections)); if (temp == nullptr) { return nullptr; @@ -1022,7 +1023,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 8b915449cdb..af880a03d9c 100644 --- a/toxcore/friend_requests.c +++ b/toxcore/friend_requests.c @@ -164,12 +164,12 @@ void friendreq_init(Friend_Requests *fr, Friend_Connections *fr_c) set_friend_request_callback(fr_c, &friendreq_handlepacket, fr); } -Friend_Requests *friendreq_new(void) +Friend_Requests *owner friendreq_new(void) { - return (Friend_Requests *)calloc(1, sizeof(Friend_Requests)); + return (Friend_Requests *owner)calloc(1, sizeof(Friend_Requests)); } -void friendreq_kill(Friend_Requests *fr) +void friendreq_kill(Friend_Requests *owner fr) { free(fr); } diff --git a/toxcore/group.c b/toxcore/group.c index 14e61e6ffc3..334ed07eb64 100644 --- a/toxcore/group.c +++ b/toxcore/group.c @@ -111,10 +111,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; @@ -152,7 +152,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; @@ -258,7 +258,7 @@ static bool realloc_conferences(Group_Chats *g_c, uint16_t num) return true; } - Group_c *newgroup_chats = (Group_c *)realloc(g_c->chats, num * sizeof(Group_c)); + Group_c *owner newgroup_chats = (Group_c *owner)realloc(g_c->chats, num * sizeof(Group_c)); if (newgroup_chats == nullptr) { return false; @@ -683,7 +683,7 @@ static bool delete_frozen(Group_c *g, uint32_t frozen_index) g->frozen[frozen_index] = g->frozen[g->numfrozen]; } - Group_Peer *const frozen_temp = (Group_Peer *)realloc(g->frozen, g->numfrozen * sizeof(Group_Peer)); + Group_Peer *const owner frozen_temp = (Group_Peer *owner)realloc(g->frozen, g->numfrozen * sizeof(Group_Peer)); if (frozen_temp == nullptr) { return false; @@ -724,7 +724,7 @@ static int note_peer_active(Group_Chats *g_c, uint32_t groupnumber, uint16_t pee /* Now thaw the peer */ - Group_Peer *temp = (Group_Peer *)realloc(g->group, (g->numpeers + 1) * sizeof(Group_Peer)); + Group_Peer *owner temp = (Group_Peer *owner)realloc(g->group, (g->numpeers + 1) * sizeof(Group_Peer)); if (temp == nullptr) { return -1; @@ -837,7 +837,7 @@ static int addpeer(Group_Chats *g_c, uint32_t groupnumber, const uint8_t *real_p delete_any_peer_with_pk(g_c, groupnumber, real_pk, userdata); - Group_Peer *temp = (Group_Peer *)realloc(g->group, (g->numpeers + 1) * sizeof(Group_Peer)); + Group_Peer *owner temp = (Group_Peer *owner)realloc(g->group, (g->numpeers + 1) * sizeof(Group_Peer)); if (temp == nullptr) { return -1; @@ -935,7 +935,7 @@ static bool delpeer(Group_Chats *g_c, uint32_t groupnumber, int peer_index, void g->group[peer_index] = g->group[g->numpeers]; } - Group_Peer *temp = (Group_Peer *)realloc(g->group, g->numpeers * sizeof(Group_Peer)); + Group_Peer *owner temp = (Group_Peer *owner)realloc(g->group, g->numpeers * sizeof(Group_Peer)); if (temp == nullptr) { return false; @@ -989,7 +989,7 @@ static bool delete_old_frozen(Group_c *g) qsort(g->frozen, g->numfrozen, sizeof(Group_Peer), cmp_frozen); - Group_Peer *temp = (Group_Peer *)realloc(g->frozen, g->maxfrozen * sizeof(Group_Peer)); + Group_Peer *owner temp = (Group_Peer *owner)realloc(g->frozen, g->maxfrozen * sizeof(Group_Peer)); if (temp == nullptr) { return false; @@ -1014,7 +1014,7 @@ static bool freeze_peer(Group_Chats *g_c, uint32_t groupnumber, int peer_index, return false; } - Group_Peer *temp = (Group_Peer *)realloc(g->frozen, (g->numfrozen + 1) * sizeof(Group_Peer)); + Group_Peer *owner temp = (Group_Peer *owner)realloc(g->frozen, (g->numfrozen + 1) * sizeof(Group_Peer)); if (temp == nullptr) { return false; @@ -3612,7 +3612,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 *)realloc(g->frozen, (j + 1) * sizeof(Group_Peer)); + Group_Peer *owner tmp_frozen = (Group_Peer *owner)realloc(g->frozen, (j + 1) * sizeof(Group_Peer)); if (tmp_frozen == nullptr) { // Memory allocation failure @@ -3758,7 +3758,7 @@ Group_Chats *new_groupchats(const Mono_Time *mono_time, Messenger *m) return nullptr; } - Group_Chats *temp = (Group_Chats *)calloc(1, sizeof(Group_Chats)); + Group_Chats *owner temp = (Group_Chats *owner)calloc(1, sizeof(Group_Chats)); if (temp == nullptr) { return nullptr; @@ -3801,7 +3801,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 ee083198c1c..6070fed68e7 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,11 +373,11 @@ 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( GC_Announces_List *gc_announces_list, const GC_Public_Announce *public_announce) { - GC_Announces *announces = (GC_Announces *)calloc(1, sizeof(GC_Announces)); + GC_Announces *owner announces = (GC_Announces *owner)calloc(1, sizeof(GC_Announces)); if (announces == nullptr) { return nullptr; @@ -373,7 +404,7 @@ GC_Peer_Announce *gca_add_announce(const Mono_Time *mono_time, GC_Announces_List 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) { @@ -412,19 +443,19 @@ bool gca_is_valid_announce(const GC_Announce *announce) GC_Announces_List *new_gca_list(void) { - return (GC_Announces_List *)calloc(1, sizeof(GC_Announces_List)); + return (GC_Announces_List *owner)calloc(1, sizeof(GC_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; free(root); root = next; } @@ -450,11 +481,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; @@ -470,9 +501,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 72f2cfc1b8b..6930fa19f22 100644 --- a/toxcore/group_announce.h +++ b/toxcore/group_announce.h @@ -73,13 +73,13 @@ 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; }; /* A list of all announces. */ struct GC_Announces_List { - 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 03077d08062..07522123539 100644 --- a/toxcore/group_chats.c +++ b/toxcore/group_chats.c @@ -1485,7 +1485,7 @@ static int group_packet_unwrap(const Logger *log, const GC_Connection *gconn, ui return -1; } - uint8_t *plain = (uint8_t *)malloc(length); + uint8_t *owner plain = (uint8_t *owner)malloc(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 *)malloc(packet_size); + uint8_t *owner plain = (uint8_t *owner)malloc(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 *)malloc(encrypt_buf_size); + uint8_t *owner encrypt = (uint8_t *owner)malloc(encrypt_buf_size); if (encrypt == nullptr) { free(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 *)malloc(packet_size); + uint8_t *owner packet = (uint8_t *owner)malloc(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 *)malloc(MAX_GC_PACKET_CHUNK_SIZE); + uint8_t *owner response = (uint8_t *owner)malloc(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 *)malloc(length + GC_BROADCAST_ENC_HEADER_SIZE); + uint8_t *owner packet = (uint8_t *owner)malloc(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 *)calloc(1, sizeof(GC_Peer)); + GC_Peer *owner self = (GC_Peer *owner)calloc(1, 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 *)malloc(data_size); + uint8_t *owner data = (uint8_t *owner)malloc(data_size); if (data == nullptr) { free(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 *)calloc(1, sizeof(GC_Peer)); + GC_Peer *owner peer_info = (GC_Peer *owner)calloc(1, 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 *)calloc(num_sanctions, sizeof(Mod_Sanction)); + Mod_Sanction *owner sanctions = (Mod_Sanction *owner)calloc(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 *)malloc(mod_list_size); + uint8_t *owner packed_mod_list = (uint8_t *owner)malloc(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 *)malloc(length); + uint8_t *owner packet = (uint8_t *owner)malloc(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 *)malloc(packet_size); + uint8_t *owner packet = (uint8_t *owner)malloc(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 *)malloc(packet_size); + uint8_t *owner packet = (uint8_t *owner)malloc(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 *)malloc(length); + uint8_t *owner packet = (uint8_t *owner)malloc(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 *)malloc(packet_buf_size); + uint8_t *owner packet = (uint8_t *owner)malloc(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 *)malloc(packet_buf_size); + uint8_t *owner packet = (uint8_t *owner)malloc(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 *)malloc(packet_buf_size); + uint8_t *owner packed_topic = (uint8_t *owner)malloc(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->shared_state.password, oldlen); + uint8_t *owner oldpasswd = memdup(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 *)malloc(length); + uint8_t *owner data = (uint8_t *owner)malloc(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 *)malloc(packet_len); + uint8_t *owner packet = (uint8_t *owner)malloc(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 *)malloc(length_raw); + uint8_t *owner message_raw = (uint8_t *owner)malloc(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 *)malloc(raw_length); + uint8_t *owner message_with_type = (uint8_t *owner)malloc(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 *)malloc(raw_length + GC_BROADCAST_ENC_HEADER_SIZE); + uint8_t *owner packet = (uint8_t *owner)malloc(raw_length + GC_BROADCAST_ENC_HEADER_SIZE); if (packet == nullptr) { free(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 *)malloc(encrypt_buf_size); + uint8_t *owner encrypt = (uint8_t *owner)malloc(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 *)malloc(data_buf_size); + uint8_t *owner data = (uint8_t *owner)malloc(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 *)malloc(length); + uint8_t *owner data = (uint8_t *owner)malloc(length); if (data == nullptr) { LOGGER_DEBUG(chat->log, "Failed to allocate memory for packet data buffer"); @@ -6325,7 +6325,7 @@ static bool handle_gc_lossy_packet(const GC_Session *c, GC_Chat *chat, const uin return false; } - uint8_t *data = (uint8_t *)malloc(length); + uint8_t *owner data = (uint8_t *owner)malloc(length); if (data == nullptr) { LOGGER_ERROR(chat->log, "Failed to allocate memory for packet buffer"); @@ -6732,7 +6732,7 @@ static bool peer_delete(const GC_Session *c, GC_Chat *chat, uint32_t peer_number 0 }; - GC_Peer *tmp_group = (GC_Peer *)realloc(chat->group, chat->numpeers * sizeof(GC_Peer)); + GC_Peer *owner tmp_group = (GC_Peer *owner)realloc(chat->group, chat->numpeers * sizeof(GC_Peer)); if (tmp_group == nullptr) { return false; @@ -6808,8 +6808,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 *)calloc(GCC_BUFFER_SIZE, sizeof(GC_Message_Array_Entry)); - GC_Message_Array_Entry *recv = (GC_Message_Array_Entry *)calloc(GCC_BUFFER_SIZE, sizeof(GC_Message_Array_Entry)); + GC_Message_Array_Entry *owner send = (GC_Message_Array_Entry *owner)calloc(GCC_BUFFER_SIZE, sizeof(GC_Message_Array_Entry)); + GC_Message_Array_Entry *owner recv = (GC_Message_Array_Entry *owner)calloc(GCC_BUFFER_SIZE, sizeof(GC_Message_Array_Entry)); if (send == nullptr || recv == nullptr) { LOGGER_ERROR(chat->log, "Failed to allocate memory for gconn buffers"); @@ -6823,7 +6823,7 @@ int peer_add(GC_Chat *chat, const IP_Port *ipp, const uint8_t *public_key) return -1; } - GC_Peer *tmp_group = (GC_Peer *)realloc(chat->group, (chat->numpeers + 1) * sizeof(GC_Peer)); + GC_Peer *owner tmp_group = (GC_Peer *owner)realloc(chat->group, (chat->numpeers + 1) * sizeof(GC_Peer)); if (tmp_group == nullptr) { LOGGER_ERROR(chat->log, "Failed to allocate memory for group realloc"); @@ -7056,7 +7056,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 *)malloc(buf_size); + uint8_t *owner data = (uint8_t *owner)malloc(buf_size); if (data == nullptr) { return false; @@ -7327,7 +7327,7 @@ static bool realloc_groupchats(GC_Session *c, uint32_t n) return true; } - GC_Chat *temp = (GC_Chat *)realloc(c->chats, n * sizeof(GC_Chat)); + GC_Chat *owner temp = (GC_Chat *owner)realloc(c->chats, n * sizeof(GC_Chat)); if (temp == nullptr) { return false; @@ -7358,7 +7358,7 @@ static int get_new_group_index(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; } @@ -7378,7 +7378,7 @@ static void add_tcp_relays_to_chat(const GC_Session *c, GC_Chat *chat) return; } - Node_format *tcp_relays = (Node_format *)calloc(num_relays, sizeof(Node_format)); + Node_format *owner tcp_relays = (Node_format *owner)calloc(num_relays, sizeof(Node_format)); if (tcp_relays == nullptr) { return; @@ -7860,7 +7860,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 *)malloc(2 + CHAT_ID_SIZE + ENC_PUBLIC_KEY_SIZE + group_name_length); + uint8_t *owner packet = (uint8_t *owner)malloc(2 + CHAT_ID_SIZE + ENC_PUBLIC_KEY_SIZE + group_name_length); if (packet == nullptr) { return -1; @@ -7954,7 +7954,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 *)malloc(packet_length); + uint8_t *owner packet = (uint8_t *owner)malloc(packet_length); if (packet == nullptr) { return false; @@ -8232,7 +8232,7 @@ GC_Session *new_dht_groupchats(Messenger *m) return nullptr; } - GC_Session *c = (GC_Session *)calloc(1, sizeof(GC_Session)); + GC_Session *owner c = (GC_Session *owner)calloc(1, sizeof(GC_Session)); if (c == nullptr) { return nullptr; @@ -8318,7 +8318,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 daa8fe17c83..f93dc5012bc 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 1c2d1ec3dac..817fc5a7c6f 100644 --- a/toxcore/group_connection.c +++ b/toxcore/group_connection.c @@ -109,7 +109,7 @@ static bool create_array_entry(const Logger *log, const Mono_Time *mono_time, GC return false; } - uint8_t *entry_data = (uint8_t *)malloc(length); + uint8_t *owner entry_data = (uint8_t *owner)malloc(length); if (entry_data == nullptr) { return false; @@ -395,7 +395,7 @@ static uint16_t reassemble_packet(const Logger *log, GC_Connection *gconn, uint8 return 0; } - uint8_t *tmp_payload = (uint8_t *)malloc(packet_length); + uint8_t *owner tmp_payload = (uint8_t *owner)malloc(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, 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 *)malloc(packet_size); + uint8_t *owner packet = (uint8_t *owner)malloc(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 3cb69e9403e..8049abd56d0 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 **)calloc(num_mods, sizeof(uint8_t *)); + uint8_t **owner tmp_list = (uint8_t **owner)calloc(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 *)malloc(MOD_LIST_ENTRY_SIZE); + uint8_t *owner entry = (uint8_t *owner)malloc(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 *)malloc(data_buf_size); + uint8_t *owner data = (uint8_t *owner)malloc(data_buf_size); if (data == nullptr) { return false; @@ -169,7 +169,7 @@ bool mod_list_remove_index(Moderation *moderation, uint16_t index) free(moderation->mod_list[moderation->num_mods]); moderation->mod_list[moderation->num_mods] = nullptr; - uint8_t **tmp_list = (uint8_t **)realloc(moderation->mod_list, moderation->num_mods * sizeof(uint8_t *)); + uint8_t **owner tmp_list = (uint8_t **owner)realloc(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 **)realloc(moderation->mod_list, (moderation->num_mods + 1) * sizeof(uint8_t *)); + uint8_t **owner tmp_list = (uint8_t **owner)realloc(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 *)malloc(MOD_LIST_ENTRY_SIZE); + uint8_t *owner entry = (uint8_t *owner)malloc(MOD_LIST_ENTRY_SIZE); if (entry == nullptr) { return false; @@ -422,7 +422,7 @@ static bool sanctions_list_make_hash(const Mod_Sanction *sanctions, uint32_t new return false; } - uint8_t *data = (uint8_t *)malloc(data_buf_size); + uint8_t *owner data = (uint8_t *owner)malloc(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 Mod_Sanction *sanctions, uint16_t num_sanctions) +static Mod_Sanction *owner sanctions_list_copy(const Mod_Sanction *sanctions, uint16_t num_sanctions) { - Mod_Sanction *copy = (Mod_Sanction *)calloc(num_sanctions, sizeof(Mod_Sanction)); + Mod_Sanction *owner copy = (Mod_Sanction *owner)calloc(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->sanctions, moderation->num_sanctions); + Mod_Sanction *owner sanctions_copy = sanctions_list_copy(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 *)realloc(sanctions_copy, new_num * sizeof(Mod_Sanction)); + Mod_Sanction *owner new_list = (Mod_Sanction *owner)realloc(sanctions_copy, new_num * sizeof(Mod_Sanction)); if (new_list == nullptr) { free(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->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 *)realloc(sanctions_copy, (index + 1) * sizeof(Mod_Sanction)); + Mod_Sanction *owner new_list = (Mod_Sanction *owner)realloc(sanctions_copy, (index + 1) * sizeof(Mod_Sanction)); if (new_list == nullptr) { free(sanctions_copy); diff --git a/toxcore/group_moderation.h b/toxcore/group_moderation.h index eeab32fc0ae..08b48371b56 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 c20d6a1213e..6c087cb16fc 100644 --- a/toxcore/group_pack.c +++ b/toxcore/group_pack.c @@ -180,7 +180,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 *)malloc(chat->moderation.num_mods * MOD_LIST_ENTRY_SIZE); + uint8_t *owner packed_mod_list = (uint8_t *owner)malloc(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"); @@ -299,7 +299,7 @@ static bool load_unpack_saved_peers(GC_Chat *chat, Bin_Unpack *bu) return true; } - uint8_t *saved_peers = (uint8_t *)malloc(saved_peers_size * GC_SAVED_PEER_SIZE); + uint8_t *owner saved_peers = (uint8_t *owner)malloc(saved_peers_size * GC_SAVED_PEER_SIZE); if (saved_peers == nullptr) { LOGGER_ERROR(chat->log, "Failed to allocate memory for saved peer list"); @@ -390,7 +390,7 @@ static void save_pack_mod_list(const GC_Chat *chat, Bin_Pack *bp) return; } - uint8_t *packed_mod_list = (uint8_t *)malloc(num_mods * MOD_LIST_ENTRY_SIZE); + uint8_t *owner packed_mod_list = (uint8_t *owner)malloc(num_mods * MOD_LIST_ENTRY_SIZE); // we can still recover without the mod list if (packed_mod_list == nullptr) { @@ -445,7 +445,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 *)malloc(GC_MAX_SAVED_PEERS * GC_SAVED_PEER_SIZE); + uint8_t *owner saved_peers = (uint8_t *owner)malloc(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 bf3db247f1a..02e6647e0f4 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 *)realloc(list->data, list->element_size * new_size); + uint8_t *owner data = (uint8_t *owner)realloc(list->data, list->element_size * new_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 *)realloc(list->ids, new_size * sizeof(int)); + int *owner ids = (int *owner)realloc(list->ids, new_size * sizeof(int)); if (ids == nullptr) { return false; diff --git a/toxcore/list.h b/toxcore/list.h index 1dc66d01a4a..bd3d6d13ae2 100644 --- a/toxcore/list.h +++ b/toxcore/list.h @@ -27,8 +27,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 67fa52330b0..6321bd47b6d 100644 --- a/toxcore/logger.c +++ b/toxcore/logger.c @@ -70,10 +70,10 @@ static const Logger logger_stderr = { Logger *logger_new(void) { - return (Logger *)calloc(1, sizeof(Logger)); + return (Logger *owner)calloc(1, sizeof(Logger)); } -void logger_kill(Logger *log) +void logger_kill(Logger *owner log) { free(log); } diff --git a/toxcore/logger.h b/toxcore/logger.h index 830db883aa4..dc0f1c7987c 100644 --- a/toxcore/logger.h +++ b/toxcore/logger.h @@ -73,7 +73,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 bddc335b826..115785e175e 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,19 +47,19 @@ 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; } -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; @@ -67,11 +67,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; @@ -79,11 +79,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 7a96b6d3580..38e6d9f35ed 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,21 +44,21 @@ 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 Allocate a single object. * * 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. @@ -74,10 +75,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 124f94fc231..1a4799622e7 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 1680b078b72..0693049090d 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -33,7 +33,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; @@ -64,7 +64,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; @@ -123,7 +123,7 @@ typedef struct Crypto_Connection { bool maximum_speed_reached; /* Must be a pointer, because the struct is moved in memory */ - pthread_mutex_t *mutex; + pthread_mutex_t *owner mutex; dht_pk_cb *dht_pk_callback; void *dht_pk_callback_object; @@ -140,9 +140,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; pthread_mutex_t tcp_mutex; pthread_mutex_t connections_mutex; @@ -773,7 +773,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; @@ -829,7 +829,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"); @@ -1387,7 +1387,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; @@ -1816,8 +1816,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; @@ -1863,7 +1863,7 @@ static int create_crypto_connection(Net_Crypto *c) } if (id != -1) { - pthread_mutex_t *mutex = (pthread_mutex_t *)mem_alloc(c->mem, sizeof(pthread_mutex_t)); + pthread_mutex_t *owner mutex = (pthread_mutex_t *owner)mem_alloc(c->mem, sizeof(pthread_mutex_t)); if (mutex == nullptr) { pthread_mutex_unlock(&c->connections_mutex); @@ -2017,7 +2017,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; @@ -3113,7 +3113,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; @@ -3203,7 +3203,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 0d817e43151..d7e53437a70 100644 --- a/toxcore/net_crypto.h +++ b/toxcore/net_crypto.h @@ -133,7 +133,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 55aa4e28184..27c6e781c46 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -1157,7 +1157,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; @@ -1361,7 +1361,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; @@ -1375,7 +1375,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; @@ -1985,7 +1985,7 @@ int32_t net_getipport(const Memory *mem, const char *node, IP_Port **res, int to *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; @@ -1998,7 +1998,7 @@ int32_t net_getipport(const Memory *mem, const char *node, IP_Port **res, int to #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(); } @@ -2042,7 +2042,7 @@ int32_t net_getipport(const Memory *mem, const char *node, IP_Port **res, int to 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) { freeaddrinfo(infos); @@ -2085,7 +2085,7 @@ int32_t net_getipport(const Memory *mem, const char *node, IP_Port **res, int to 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); } @@ -2267,7 +2267,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]; @@ -2276,7 +2276,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; @@ -2288,10 +2288,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 9fab57af90a..01f59fea570 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 593d81aa2ca..0de26e28d47 100644 --- a/toxcore/onion_announce.c +++ b/toxcore/onion_announce.c @@ -331,7 +331,7 @@ static void sort_onion_announce_list(const Memory *mem, const Mono_Time *mono_ti { // Pass comp_public_key to qsort with each Client_data entry, so the // comparison function can use it as the base of comparison. - Cmp_Data *cmp_list = (Cmp_Data *)mem_valloc(mem, length, sizeof(Cmp_Data)); + Cmp_Data *owner cmp_list = (Cmp_Data *owner)mem_valloc(mem, length, sizeof(Cmp_Data)); if (cmp_list == nullptr) { return; @@ -449,7 +449,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; @@ -494,7 +494,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); @@ -657,7 +657,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; @@ -688,7 +688,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 9b0ac96102a..656e31f0819 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]; @@ -743,7 +743,7 @@ static void sort_onion_node_list(const Memory *mem, const Mono_Time *mono_time, { // Pass comp_public_key to qsort with each Client_data entry, so the // comparison function can use it as the base of comparison. - Onion_Client_Cmp_Data *cmp_list = (Onion_Client_Cmp_Data *)mem_valloc(mem, length, sizeof(Onion_Client_Cmp_Data)); + Onion_Client_Cmp_Data *owner cmp_list = (Onion_Client_Cmp_Data *owner)mem_valloc(mem, length, sizeof(Onion_Client_Cmp_Data)); if (cmp_list == nullptr) { return; @@ -1509,7 +1509,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; @@ -2176,7 +2176,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; @@ -2208,7 +2208,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 303c418ceac..dd5993d24b9 100644 --- a/toxcore/ping.c +++ b/toxcore/ping.c @@ -333,7 +333,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; @@ -355,7 +355,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 c40215ea47d..c81c8f1f102 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 80d74aecb49..e8dc802f9b5 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/test.c b/toxcore/test.c new file mode 100644 index 00000000000..0e13db7a603 --- /dev/null +++ b/toxcore/test.c @@ -0,0 +1,12 @@ +typedef unsigned char uint8_t; +typedef unsigned int uint32_t; + +typedef struct { + uint8_t d; + _Bool e; +} f; +void g(f *b) { + for (uint32_t c; c < 10; ++c) { + f *a = &b[c]; + } +} diff --git a/toxcore/tox.c b/toxcore/tox.c index 7943167a710..8808019f344 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -801,7 +801,7 @@ Tox *tox_new(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); @@ -878,7 +878,7 @@ Tox *tox_new(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); @@ -1020,7 +1020,7 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) return tox; } -void tox_kill(Tox *tox) +void tox_kill(Tox *owner tox) { if (tox == nullptr) { return; @@ -2051,11 +2051,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 a5d995cf508..cd9f0702b16 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 bd42fcce8d6..89336d51d59 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 1851e58a080..469cfa8a764 100644 --- a/toxcore/util.c +++ b/toxcore/util.c @@ -24,7 +24,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; @@ -81,13 +81,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 uint8_t *data, size_t data_size) +uint8_t *owner memdup(const uint8_t *data, size_t data_size) { if (data == nullptr || data_size == 0) { return nullptr; } - uint8_t *copy = (uint8_t *)malloc(data_size); + uint8_t *owner copy = (uint8_t *owner)malloc(data_size); if (copy != nullptr) { memcpy(copy, data, data_size); diff --git a/toxcore/util.h b/toxcore/util.h index 5be74a8d86c..b10a9578159 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. */ -nullable(1) uint8_t *memdup(const uint8_t *data, size_t data_size); +nullable(1) uint8_t *owner memdup(const uint8_t *data, size_t data_size); /** * @brief Set all bytes in `data` to 0.