From ca2daf302efe891a2f3d668f8bee9b2a5b7379d9 Mon Sep 17 00:00:00 2001 From: iphydf Date: Mon, 11 Sep 2023 10:51:20 +0000 Subject: [PATCH] fix: Ensure we have allocators available for the error paths. --- testing/fuzzing/bootstrap_harness.cc | 13 +++++++------ toxcore/group_chats.c | 4 ++++ toxcore/network.c | 4 +++- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/testing/fuzzing/bootstrap_harness.cc b/testing/fuzzing/bootstrap_harness.cc index 9ffb69d1f1b..2e9639e2c4e 100644 --- a/testing/fuzzing/bootstrap_harness.cc +++ b/testing/fuzzing/bootstrap_harness.cc @@ -107,6 +107,9 @@ void setup_callbacks(Tox_Dispatch *dispatch) void TestBootstrap(Fuzz_Data &input) { + // Null system for regularly working memory allocations needed in + // tox_events_equal. + Null_System null_sys; Fuzz_System sys(input); Ptr opts(tox_options_new(nullptr), tox_options_free); @@ -154,11 +157,9 @@ void TestBootstrap(Fuzz_Data &input) uint8_t pub_key[TOX_PUBLIC_KEY_SIZE] = {0}; - const bool udp_success = tox_bootstrap(tox, "127.0.0.2", 33446, pub_key, nullptr); - assert(udp_success); - - const bool tcp_success = tox_add_tcp_relay(tox, "127.0.0.2", 33446, pub_key, nullptr); - assert(tcp_success); + // These may fail, but that's ok. We ignore their return values. + tox_bootstrap(tox, "127.0.0.2", 33446, pub_key, nullptr); + tox_add_tcp_relay(tox, "127.0.0.2", 33446, pub_key, nullptr); tox_events_init(tox); @@ -169,7 +170,7 @@ void TestBootstrap(Fuzz_Data &input) while (input.size > 0) { Tox_Err_Events_Iterate error_iterate; Tox_Events *events = tox_events_iterate(tox, true, &error_iterate); - assert(tox_events_equal(sys.sys.get(), events, events)); + assert(tox_events_equal(null_sys.sys.get(), events, events)); tox_dispatch_invoke(dispatch, events, tox, nullptr); tox_events_free(events); // Move the clock forward a decent amount so all the time-based checks diff --git a/toxcore/group_chats.c b/toxcore/group_chats.c index 23eca0a909f..ac10e8b1e69 100644 --- a/toxcore/group_chats.c +++ b/toxcore/group_chats.c @@ -7483,6 +7483,10 @@ int gc_group_load(GC_Session *c, Bin_Unpack *bu) chat->last_ping_interval = tm; chat->friend_connection_id = -1; + // Initialise these first, because we may need to log/dealloc things on cleanup. + chat->moderation.log = m->log; + chat->moderation.mem = m->mem; + if (!gc_load_unpack_group(chat, bu)) { LOGGER_ERROR(chat->log, "Failed to unpack group"); return -1; diff --git a/toxcore/network.c b/toxcore/network.c index 90b3d2d919a..cc04cde70a9 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -1772,6 +1772,9 @@ int32_t net_getipport(const Memory *mem, const char *node, IP_Port **res, int to { // Try parsing as IP address first. IP_Port parsed = {{{0}}}; + // Initialise to nullptr. In error paths, at least we initialise the out + // parameter. + *res = nullptr; if (addr_parse_ip(node, &parsed.ip)) { IP_Port *tmp = (IP_Port *)mem_alloc(mem, sizeof(IP_Port)); @@ -1800,7 +1803,6 @@ int32_t net_getipport(const Memory *mem, const char *node, IP_Port **res, int to // It's not an IP address, so now we try doing a DNS lookup. struct addrinfo *infos; const int ret = getaddrinfo(node, nullptr, nullptr, &infos); - *res = nullptr; if (ret != 0) { return -1;