diff --git a/external/toxcore/c-toxcore/.circleci/config.yml b/external/toxcore/c-toxcore/.circleci/config.yml index 5531177d..9fd1c822 100644 --- a/external/toxcore/c-toxcore/.circleci/config.yml +++ b/external/toxcore/c-toxcore/.circleci/config.yml @@ -16,7 +16,6 @@ workflows: # Static analysis - clang-analyze - cpplint - - infer - static-analysis jobs: @@ -69,6 +68,7 @@ jobs: cmake git libconfig-dev + libgmock-dev libgtest-dev libopus-dev libsodium-dev @@ -102,27 +102,6 @@ jobs: - run: git submodule update --init --recursive - run: CC=clang .circleci/cmake-ubsan - infer: - working_directory: ~/work - docker: - - image: toxchat/infer - - steps: - - run: *apt_install - - checkout - - run: git submodule update --init --recursive - - run: infer --no-progress-bar -- cc - auto_tests/auto_test_support.c - auto_tests/lossless_packet_test.c - testing/misc_tools.c - toxav/*.c - toxcore/*.c - toxcore/*/*.c - toxencryptsave/*.c - third_party/cmp/*.c - -lpthread - $(pkg-config --cflags --libs libsodium opus vpx) - static-analysis: working_directory: ~/work docker: diff --git a/external/toxcore/c-toxcore/.github/scripts/coverage-linux b/external/toxcore/c-toxcore/.github/scripts/coverage-linux deleted file mode 100755 index 4e73dae5..00000000 --- a/external/toxcore/c-toxcore/.github/scripts/coverage-linux +++ /dev/null @@ -1,61 +0,0 @@ -#!/bin/bash - -set -eu - -NPROC=$(nproc) - -sudo apt-get install -y --no-install-recommends \ - libgtest-dev \ - libopus-dev \ - libsodium-dev \ - libvpx-dev \ - llvm-14 \ - ninja-build -git clone --depth=1 https://github.com/ralight/mallocfail /tmp/mallocfail -cd /tmp/mallocfail # pushd -make -sudo make install -cd - # popd - -export CC=clang -export CXX=clang++ - -sudo install other/docker/coverage/run_mallocfail /usr/local/bin/run_mallocfail -(cd other/proxy && go get && go build) -other/proxy/proxy & - -. ".github/scripts/flags-coverage.sh" - -cmake -B_build -H. -GNinja \ - -DCMAKE_C_FLAGS="$C_FLAGS" \ - -DCMAKE_CXX_FLAGS="$CXX_FLAGS" \ - -DCMAKE_EXE_LINKER_FLAGS="$LD_FLAGS" \ - -DCMAKE_SHARED_LINKER_FLAGS="$LD_FLAGS" \ - -DCMAKE_INSTALL_PREFIX:PATH="$PWD/_install" \ - -DENABLE_SHARED=OFF \ - -DMIN_LOGGER_LEVEL=TRACE \ - -DMUST_BUILD_TOXAV=ON \ - -DNON_HERMETIC_TESTS=OFF \ - -DSTRICT_ABI=ON \ - -DTEST_TIMEOUT_SECONDS=120 \ - -DUSE_IPV6=OFF \ - -DAUTOTEST=ON \ - -DPROXY_TEST=ON - -cmake --build _build --parallel "$NPROC" --target install -- -k 0 - -cd _build # pushd -ctest -j50 --output-on-failure --rerun-failed --repeat until-pass:6 || - ctest -j50 --output-on-failure --rerun-failed --repeat until-pass:6 - -export PYTHONUNBUFFERED=1 -run_mallocfail --ctest=2 --jobs=8 -cd - # popd - -#coveralls \ -# --exclude auto_tests \ -# --exclude other \ -# --exclude testing \ -# --gcov-options '\-lp' - -bash <(curl -s https://codecov.io/bash) -x "llvm-cov-14 gcov" diff --git a/external/toxcore/c-toxcore/.github/scripts/flags-coverage.sh b/external/toxcore/c-toxcore/.github/scripts/flags-coverage.sh index ebfa2b1c..6e848842 100644 --- a/external/toxcore/c-toxcore/.github/scripts/flags-coverage.sh +++ b/external/toxcore/c-toxcore/.github/scripts/flags-coverage.sh @@ -5,10 +5,10 @@ add_ld_flag -Wl,-z,defs # Make compilation error on a warning -add_flag -Werror +add_flag -Werror -Wno-unsafe-buffer-usage # Coverage flags. -add_flag --coverage +add_flag -fprofile-instr-generate -fcoverage-mapping # Optimisation, but keep stack traces useful. add_c_flag -fno-inline -fno-omit-frame-pointer diff --git a/external/toxcore/c-toxcore/.github/workflows/ci.yml b/external/toxcore/c-toxcore/.github/workflows/ci.yml index 01eb0fb7..d5c10cf2 100644 --- a/external/toxcore/c-toxcore/.github/workflows/ci.yml +++ b/external/toxcore/c-toxcore/.github/workflows/ci.yml @@ -16,7 +16,7 @@ jobs: analysis: strategy: matrix: - tool: [autotools, clang-tidy, compcert, cppcheck, doxygen, misra, tcc, tokstyle] + tool: [autotools, clang-tidy, compcert, cppcheck, doxygen, infer, misra, tcc, tokstyle] runs-on: ubuntu-latest steps: - name: Set up Docker Buildx diff --git a/external/toxcore/c-toxcore/.gitignore b/external/toxcore/c-toxcore/.gitignore index 9ed3743e..2e4a095a 100644 --- a/external/toxcore/c-toxcore/.gitignore +++ b/external/toxcore/c-toxcore/.gitignore @@ -93,4 +93,5 @@ cscope.files # rpm tox.spec +/infer .idea/ diff --git a/external/toxcore/c-toxcore/CMakeLists.txt b/external/toxcore/c-toxcore/CMakeLists.txt index 8abcf609..761f53a6 100644 --- a/external/toxcore/c-toxcore/CMakeLists.txt +++ b/external/toxcore/c-toxcore/CMakeLists.txt @@ -160,21 +160,7 @@ if(BOOTSTRAP_DAEMON AND WIN32) set(BOOTSTRAP_DAEMON OFF) endif() -# Enabling this breaks all other tests and no network connections will be possible option(BUILD_FUZZ_TESTS "Build fuzzing harnesses" OFF) -if(BUILD_FUZZ_TESTS) - message(STATUS "Building in fuzz testing mode, no network connection will be possible") - # Disable everything we can - set(AUTOTEST OFF) - set(BUILD_MISC_TESTS OFF) - set(BUILD_FUN_UTILS OFF) - set(ENABLE_SHARED OFF) - set(MUST_BUILD_TOXAV OFF) - set(BUILD_TOXAV OFF) - set(BOOTSTRAP_DAEMON OFF) - set(DHT_BOOTSTRAP OFF) -endif() - if(MSVC) option(MSVC_STATIC_SODIUM "Whether to link libsodium statically for MSVC" OFF) @@ -468,20 +454,33 @@ endif() ################################################################################ # Create combined library from all the sources. -add_module(toxcore ${toxcore_SOURCES}) +if(ENABLE_SHARED) + add_library(toxcore_shared SHARED ${toxcore_SOURCES}) + set_target_properties(toxcore_shared PROPERTIES OUTPUT_NAME toxcore) + target_link_libraries(toxcore_shared PRIVATE ${toxcore_LINK_LIBRARIES}) + target_link_directories(toxcore_shared PUBLIC ${toxcore_LINK_DIRECTORIES}) + target_include_directories(toxcore_shared SYSTEM PRIVATE ${toxcore_INCLUDE_DIRECTORIES}) + target_compile_options(toxcore_shared PRIVATE ${toxcore_COMPILE_OPTIONS}) +endif() -# Link it to all dependencies. -if(TARGET toxcore_static) +if(ENABLE_STATIC) + add_library(toxcore_static STATIC ${toxcore_SOURCES}) + if(NOT MSVC) + set_target_properties(toxcore_static PROPERTIES OUTPUT_NAME toxcore) + endif() target_link_libraries(toxcore_static PRIVATE ${toxcore_LINK_LIBRARIES}) target_link_directories(toxcore_static PUBLIC ${toxcore_LINK_DIRECTORIES}) target_include_directories(toxcore_static SYSTEM PRIVATE ${toxcore_INCLUDE_DIRECTORIES}) target_compile_options(toxcore_static PRIVATE ${toxcore_COMPILE_OPTIONS}) endif() -if(TARGET toxcore_shared) - target_link_libraries(toxcore_shared PRIVATE ${toxcore_LINK_LIBRARIES}) - target_link_directories(toxcore_shared PUBLIC ${toxcore_LINK_DIRECTORIES}) - target_include_directories(toxcore_shared SYSTEM PRIVATE ${toxcore_INCLUDE_DIRECTORIES}) - target_compile_options(toxcore_shared PRIVATE ${toxcore_COMPILE_OPTIONS}) + +if(BUILD_FUZZ_TESTS) + add_library(toxcore_fuzz STATIC ${toxcore_SOURCES}) + target_link_libraries(toxcore_fuzz PRIVATE ${toxcore_LINK_LIBRARIES}) + target_link_directories(toxcore_fuzz PUBLIC ${toxcore_LINK_DIRECTORIES}) + target_include_directories(toxcore_fuzz SYSTEM PRIVATE ${toxcore_INCLUDE_DIRECTORIES}) + target_compile_options(toxcore_fuzz PRIVATE ${toxcore_COMPILE_OPTIONS}) + target_compile_definitions(toxcore_fuzz PUBLIC "FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION") endif() # Make version script (on systems that support it) to limit symbol visibility. @@ -497,14 +496,25 @@ install_module(toxcore DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/tox) # ################################################################################ +add_library(test_util STATIC + toxcore/DHT_test_util.cc + toxcore/DHT_test_util.hh + toxcore/crypto_core_test_util.cc + toxcore/crypto_core_test_util.hh + toxcore/network_test_util.cc + toxcore/network_test_util.hh + toxcore/test_util.cc + toxcore/test_util.hh) + function(unit_test subdir target) add_executable(unit_${target}_test ${subdir}/${target}_test.cc) + target_link_libraries(unit_${target}_test PRIVATE test_util) if(TARGET toxcore_static) target_link_libraries(unit_${target}_test PRIVATE toxcore_static) else() target_link_libraries(unit_${target}_test PRIVATE toxcore_shared) endif() - target_link_libraries(unit_${target}_test PRIVATE GTest::GTest GTest::Main) + target_link_libraries(unit_${target}_test PRIVATE GTest::gtest GTest::gtest_main GTest::gmock) set_target_properties(unit_${target}_test PROPERTIES COMPILE_FLAGS "${TEST_CXX_FLAGS}") add_test(NAME ${target} COMMAND ${CROSSCOMPILING_EMULATOR} unit_${target}_test) set_property(TEST ${target} PROPERTY ENVIRONMENT "LLVM_PROFILE_FILE=${target}.profraw") @@ -524,6 +534,7 @@ if(GTEST_FOUND) unit_test(toxcore mem) unit_test(toxcore mono_time) unit_test(toxcore ping_array) + unit_test(toxcore test_util) unit_test(toxcore tox) unit_test(toxcore util) endif() diff --git a/external/toxcore/c-toxcore/auto_tests/Makefile.inc b/external/toxcore/c-toxcore/auto_tests/Makefile.inc index db22d7ff..d55f7140 100644 --- a/external/toxcore/c-toxcore/auto_tests/Makefile.inc +++ b/external/toxcore/c-toxcore/auto_tests/Makefile.inc @@ -187,14 +187,14 @@ TCP_test_SOURCES = ../auto_tests/TCP_test.c TCP_test_CFLAGS = $(AUTOTEST_CFLAGS) TCP_test_LDADD = $(AUTOTEST_LDADD) -tox_events_test_SOURCES = ../auto_tests/tox_events_test.c -tox_events_test_CFLAGS = $(AUTOTEST_CFLAGS) -tox_events_test_LDADD = $(AUTOTEST_LDADD) - tox_dispatch_test_SOURCES = ../auto_tests/tox_dispatch_test.c tox_dispatch_test_CFLAGS = $(AUTOTEST_CFLAGS) tox_dispatch_test_LDADD = $(AUTOTEST_LDADD) +tox_events_test_SOURCES = ../auto_tests/tox_events_test.c +tox_events_test_CFLAGS = $(AUTOTEST_CFLAGS) +tox_events_test_LDADD = $(AUTOTEST_LDADD) + tox_many_tcp_test_SOURCES = ../auto_tests/tox_many_tcp_test.c tox_many_tcp_test_CFLAGS = $(AUTOTEST_CFLAGS) tox_many_tcp_test_LDADD = $(AUTOTEST_LDADD) diff --git a/external/toxcore/c-toxcore/auto_tests/check_compat.h b/external/toxcore/c-toxcore/auto_tests/check_compat.h index 72fa55bc..f60bc7ae 100644 --- a/external/toxcore/c-toxcore/auto_tests/check_compat.h +++ b/external/toxcore/c-toxcore/auto_tests/check_compat.h @@ -9,7 +9,7 @@ #define ck_assert(ok) do { \ if (!(ok)) { \ fprintf(stderr, "%s:%d: failed `%s'\n", __FILE__, __LINE__, #ok); \ - abort(); \ + exit(7); \ } \ } while (0) @@ -18,7 +18,7 @@ fprintf(stderr, "%s:%d: failed `%s': ", __FILE__, __LINE__, #ok); \ fprintf(stderr, __VA_ARGS__); \ fprintf(stderr, "\n"); \ - abort(); \ + exit(7); \ } \ } while (0) @@ -26,7 +26,7 @@ fprintf(stderr, "%s:%d: ", __FILE__, __LINE__); \ fprintf(stderr, __VA_ARGS__); \ fprintf(stderr, "\n"); \ - abort(); \ + exit(7); \ } while (0) #endif // C_TOXCORE_AUTO_TESTS_CHECK_COMPAT_H diff --git a/external/toxcore/c-toxcore/auto_tests/tox_events_test.c b/external/toxcore/c-toxcore/auto_tests/tox_events_test.c index 918bc17e..1b7cb729 100644 --- a/external/toxcore/c-toxcore/auto_tests/tox_events_test.c +++ b/external/toxcore/c-toxcore/auto_tests/tox_events_test.c @@ -22,21 +22,23 @@ static bool await_message(Tox **toxes) Tox_Events *events = tox_events_iterate(toxes[1], false, nullptr); if (events != nullptr) { - ck_assert(tox_events_get_friend_message_size(events) == 1); - const Tox_Event_Friend_Message *msg_event = tox_events_get_friend_message(events, 0); + uint32_t events_size = tox_events_get_size(events); + ck_assert(events_size == 1); + + const Tox_Event_Friend_Message *msg_event = nullptr; + for (uint32_t j = 0; j < events_size; ++j) { + const Tox_Event *ev = tox_events_get(events, j); + if (tox_event_get_type(ev) == TOX_EVENT_FRIEND_MESSAGE) { + msg_event = tox_event_get_friend_message(ev); + } + } + + ck_assert(msg_event != nullptr); ck_assert(tox_event_friend_message_get_message_length(msg_event) == sizeof("hello")); const uint8_t *msg = tox_event_friend_message_get_message(msg_event); ck_assert_msg(memcmp(msg, "hello", sizeof("hello")) == 0, "message was not expected 'hello' but '%s'", (const char *)msg); - const uint32_t event_count = tox_events_get_size(events); - for (uint32_t j = 0; j < event_count; ++j) { - const Tox_Event *event = tox_events_get(events, j); - if (tox_event_get_type(event) == TOX_EVENT_FRIEND_MESSAGE) { - ck_assert(tox_event_get_friend_message(event) == msg_event); - } - } - tox_events_free(events); return true; } diff --git a/external/toxcore/c-toxcore/cmake/ModulePackage.cmake b/external/toxcore/c-toxcore/cmake/ModulePackage.cmake index 332110d7..c20a80a1 100644 --- a/external/toxcore/c-toxcore/cmake/ModulePackage.cmake +++ b/external/toxcore/c-toxcore/cmake/ModulePackage.cmake @@ -19,19 +19,6 @@ if(FULLY_STATIC) set(ENABLE_STATIC ON) endif() -function(add_module lib) - if(ENABLE_SHARED) - add_library(${lib}_shared SHARED ${ARGN}) - set_target_properties(${lib}_shared PROPERTIES OUTPUT_NAME ${lib}) - endif() - if(ENABLE_STATIC) - add_library(${lib}_static STATIC ${ARGN}) - if(NOT MSVC) - set_target_properties(${lib}_static PROPERTIES OUTPUT_NAME ${lib}) - endif() - endif() -endfunction() - function(install_module lib) if(TARGET ${lib}_shared) set_target_properties(${lib}_shared PROPERTIES diff --git a/external/toxcore/c-toxcore/heroku.yml b/external/toxcore/c-toxcore/heroku.yml deleted file mode 100644 index 96fee696..00000000 --- a/external/toxcore/c-toxcore/heroku.yml +++ /dev/null @@ -1,4 +0,0 @@ ---- -build: - docker: - web: other/bootstrap_daemon/websocket/Dockerfile diff --git a/external/toxcore/c-toxcore/other/analysis/check_includes b/external/toxcore/c-toxcore/other/analysis/check_includes index 7e4e29fd..6d8245a6 100755 --- a/external/toxcore/c-toxcore/other/analysis/check_includes +++ b/external/toxcore/c-toxcore/other/analysis/check_includes @@ -31,8 +31,8 @@ out = (subprocess.run( errors = 0 for line in out.split("\n"): - # other/fun can do what it wants. - if "/fun/" in line: + # other/fun and mallocfail can do what they want. + if "/fun/" in line or "/mallocfail/" in line: continue filename, include = line.split(":", 1) # We only check headers. diff --git a/external/toxcore/c-toxcore/other/analysis/gen-file.sh b/external/toxcore/c-toxcore/other/analysis/gen-file.sh index 623f8d28..10edd0b5 100644 --- a/external/toxcore/c-toxcore/other/analysis/gen-file.sh +++ b/external/toxcore/c-toxcore/other/analysis/gen-file.sh @@ -15,7 +15,7 @@ CPPFLAGS+=("-Itoxav") CPPFLAGS+=("-Itoxencryptsave") CPPFLAGS+=("-Ithird_party/cmp") -LDFLAGS=("-lopus" "-lsodium" "-lvpx" "-lpthread" "-lconfig" "-lgtest") +LDFLAGS=("-lopus" "-lsodium" "-lvpx" "-lpthread" "-lconfig" "-lgmock" "-lgtest") LDFLAGS+=("-fuse-ld=gold") LDFLAGS+=("-Wl,--detect-odr-violations") LDFLAGS+=("-Wl,--warn-common") diff --git a/external/toxcore/c-toxcore/other/analysis/run-clang b/external/toxcore/c-toxcore/other/analysis/run-clang index c1846407..f5ec50f4 100755 --- a/external/toxcore/c-toxcore/other/analysis/run-clang +++ b/external/toxcore/c-toxcore/other/analysis/run-clang @@ -10,7 +10,7 @@ run() { "${CPPFLAGS[@]}" \ "${LDFLAGS[@]}" \ "$@" \ - -std=c++11 \ + -std=c++17 \ -Werror \ -Weverything \ -Wno-alloca \ diff --git a/external/toxcore/c-toxcore/other/analysis/run-clang-analyze b/external/toxcore/c-toxcore/other/analysis/run-clang-analyze index 05e94e78..55cd0d91 100755 --- a/external/toxcore/c-toxcore/other/analysis/run-clang-analyze +++ b/external/toxcore/c-toxcore/other/analysis/run-clang-analyze @@ -9,7 +9,7 @@ run() { clang++ --analyze amalgamation.cc \ "${CPPFLAGS[@]}" \ "$@" \ - -std=c++11 + -std=c++20 } . other/analysis/variants.sh diff --git a/external/toxcore/c-toxcore/other/analysis/run-cppcheck b/external/toxcore/c-toxcore/other/analysis/run-cppcheck index 9dc6f179..7e341f69 100755 --- a/external/toxcore/c-toxcore/other/analysis/run-cppcheck +++ b/external/toxcore/c-toxcore/other/analysis/run-cppcheck @@ -34,6 +34,8 @@ CPPCHECK_CXX+=("--suppress=AssignmentAddressToInteger") CPPCHECK_CXX+=("--suppress=cstyleCast") # Used in Messenger.c for a static_assert(...) CPPCHECK_CXX+=("--suppress=sizeofFunctionCall") +# This is outdated. Range-for is a good choice. +CPPCHECK_CXX+=("--suppress=useStlAlgorithm") run() { echo "Running cppcheck in variant '$*'" diff --git a/external/toxcore/c-toxcore/other/analysis/run-gcc b/external/toxcore/c-toxcore/other/analysis/run-gcc index ce3338c5..8a49b325 100755 --- a/external/toxcore/c-toxcore/other/analysis/run-gcc +++ b/external/toxcore/c-toxcore/other/analysis/run-gcc @@ -11,7 +11,7 @@ run() { "${CPPFLAGS[@]}" \ "${LDFLAGS[@]}" \ "$@" \ - -std=c++11 \ + -std=c++17 \ -fdiagnostics-color=always \ -Wall \ -Wextra \ diff --git a/external/toxcore/c-toxcore/other/analysis/run-infer b/external/toxcore/c-toxcore/other/analysis/run-infer deleted file mode 100755 index bbfd8123..00000000 --- a/external/toxcore/c-toxcore/other/analysis/run-infer +++ /dev/null @@ -1,25 +0,0 @@ -#!/bin/sh - -# --bufferoverrun \ -# --pulse \ - -read -r -d '' SCRIPT <<'EOF' -infer \ - --report-console-limit 100 \ - --jobs 8 \ - --biabduction \ - --loop-hoisting \ - --quandary \ - --racerd \ - --starvation \ - --uninit \ - -- clang++ -fsyntax-only \ - $(pkg-config --cflags libconfig libsodium opus vpx) \ - /work/other/bootstrap_daemon/src/*.c \ - /work/other/bootstrap_node_packets.c \ - /work/toxav/*.c \ - /work/toxcore/*.c \ - /work/toxencryptsave/*.c -EOF - -docker run --rm -it -v "$PWD:/work" toxchat/infer bash -c "$SCRIPT" diff --git a/external/toxcore/c-toxcore/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 b/external/toxcore/c-toxcore/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 index 3175359d..fec53be0 100644 --- a/external/toxcore/c-toxcore/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 +++ b/external/toxcore/c-toxcore/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 @@ -1 +1 @@ -8fadc6fd894bb8f60fd53d51dcd51ff24e62b41ae404d3921e0247b9337c9a30 /usr/local/bin/tox-bootstrapd +08fe68095b88c142e0c7f3369c5f297f1377e219d0e0f0ff6f30ca94d76fca98 /usr/local/bin/tox-bootstrapd diff --git a/external/toxcore/c-toxcore/other/docker/circleci/Dockerfile b/external/toxcore/c-toxcore/other/docker/circleci/Dockerfile index 446e8477..3b23eed7 100644 --- a/external/toxcore/c-toxcore/other/docker/circleci/Dockerfile +++ b/external/toxcore/c-toxcore/other/docker/circleci/Dockerfile @@ -7,6 +7,7 @@ RUN apt-get update && \ clang \ cmake \ libconfig-dev \ + libgmock-dev \ libgtest-dev \ libopus-dev \ libsodium-dev \ diff --git a/external/toxcore/c-toxcore/other/docker/coverage/Dockerfile b/external/toxcore/c-toxcore/other/docker/coverage/Dockerfile index 42530fb7..8a289869 100644 --- a/external/toxcore/c-toxcore/other/docker/coverage/Dockerfile +++ b/external/toxcore/c-toxcore/other/docker/coverage/Dockerfile @@ -1,53 +1,57 @@ FROM toxchat/c-toxcore:sources AS src FROM ubuntu:20.04 AS build +SHELL ["/bin/bash", "-o", "pipefail", "-c"] + RUN apt-get update && \ DEBIAN_FRONTEND="noninteractive" apt-get install -y --no-install-recommends \ ca-certificates \ - clang \ - cmake \ curl \ - gcc \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/* + +RUN echo "deb http://apt.llvm.org/focal/ llvm-toolchain-focal-17 main" >> /etc/apt/sources.list \ + && curl -L https://apt.llvm.org/llvm-snapshot.gpg.key | tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc \ + && apt-get update && \ + DEBIAN_FRONTEND="noninteractive" apt-get install -y --no-install-recommends \ + clang-17 \ + cmake \ git \ golang-1.18 \ + libclang-rt-17-dev \ libconfig-dev \ + libgmock-dev \ libgtest-dev \ libopus-dev \ libsodium-dev \ + libunwind-17-dev \ libvpx-dev \ - llvm-dev \ + lld-17 \ + llvm-17-dev \ make \ ninja-build \ pkg-config \ - python3-lxml \ - python3-pip \ - python3-pygments \ && apt-get clean \ - && rm -rf /var/lib/apt/lists/* \ - && pip3 install --no-cache-dir gcovr -# strip gtest so it doesn't end up in stack traces. This speeds up the -# mallocfail run below by a lot. -RUN ["strip", "-g",\ - "/usr/lib/x86_64-linux-gnu/libgtest.a",\ - "/usr/lib/x86_64-linux-gnu/libgtest_main.a"] + && rm -rf /var/lib/apt/lists/* RUN ["curl", "-s", "https://codecov.io/bash", "-o", "/usr/local/bin/codecov"] RUN ["chmod", "+x", "/usr/local/bin/codecov"] -ENV CC=clang \ - CXX=clang++ \ +ENV CC=clang-17 \ + CXX=clang++-17 \ PYTHONUNBUFFERED=1 \ PATH=$PATH:/usr/lib/go-1.18/bin -SHELL ["/bin/bash", "-c"] + +COPY --from=src /src/ /work/ WORKDIR /work -COPY --from=src /src/ /work/ +RUN git clone --depth=1 https://github.com/TokTok/toktok-fuzzer /work/testing/fuzzing/toktok-fuzzer RUN source .github/scripts/flags-coverage.sh \ && go version \ && (cd other/proxy && go get github.com/things-go/go-socks5 && go build proxy_server.go) \ && cmake -B_build -H. -GNinja \ -DCMAKE_C_FLAGS="$C_FLAGS" \ -DCMAKE_CXX_FLAGS="$CXX_FLAGS" \ - -DCMAKE_EXE_LINKER_FLAGS="$LD_FLAGS" \ + -DCMAKE_EXE_LINKER_FLAGS="$LD_FLAGS -fuse-ld=lld" \ -DCMAKE_UNITY_BUILD=ON \ -DENABLE_SHARED=OFF \ -DMIN_LOGGER_LEVEL=TRACE \ @@ -56,6 +60,7 @@ RUN source .github/scripts/flags-coverage.sh \ -DSTRICT_ABI=ON \ -DAUTOTEST=ON \ -DPROXY_TEST=ON \ + -DBUILD_FUZZ_TESTS=ON \ -DUSE_IPV6=OFF \ -DTEST_TIMEOUT_SECONDS=40 \ && cmake --build _build --parallel 8 --target install @@ -66,24 +71,15 @@ RUN /work/other/proxy/proxy_server \ ctest -j50 --output-on-failure --rerun-failed --repeat until-pass:6) WORKDIR /work/mallocfail -RUN ["git", "clone", "--depth=1", "https://github.com/ralight/mallocfail", "/work/mallocfail"] -COPY other/docker/coverage/syscall_funcs.c src/ -RUN gcc -fPIC -shared -O2 -g3 -Wall -Ideps/uthash -Ideps/sha3 deps/*/*.c src/*.c -o mallocfail.so -ldl -lbacktrace \ +RUN ["git", "clone", "--depth=1", "https://github.com/TokTok/mallocfail", "/work/mallocfail"] +RUN clang-17 -fuse-ld=lld -fPIC -shared -O2 -g3 -Wall -I/usr/lib/llvm-17/include -L/usr/lib/llvm-17/lib -Ideps/uthash -Ideps/sha3 deps/*/*.c src/*.c -o mallocfail.so -ldl -lunwind \ && install mallocfail.so /usr/local/lib/mallocfail.so WORKDIR /work/_build COPY other/docker/coverage/run_mallocfail /usr/local/bin/ -RUN ["run_mallocfail", "--ctest=2", "--jobs=8"] -RUN ["gcovr", \ - "--sort-percentage", \ - "--gcov-executable=llvm-cov gcov", \ - "--html-details=html/", \ - "--root=..", \ - "--exclude=CMakeFiles/", \ - "--exclude=_deps/", \ - "--exclude=(.+/)?auto_tests/", \ - "--exclude=.+_test.cc?$", \ - "--exclude=(.+/)?other/", \ - "--exclude=(.+/)?testing/"] +RUN ["run_mallocfail", "--ctest=1", "--jobs=8"] +RUN llvm-profdata-17 merge -sparse $(find . -name "*.profraw") -o toxcore.profdata +RUN llvm-cov-17 show -format=text -instr-profile=toxcore.profdata -sources $(cmake --build . --target help | grep -o '[^:]*_test:' | grep -o '[^:]*' | xargs -n1 find . -type f -name | awk '{print "-object "$1}') > coverage.txt +RUN llvm-cov-17 show -format=html -instr-profile=toxcore.profdata -sources $(cmake --build . --target help | grep -o '[^:]*_test:' | grep -o '[^:]*' | xargs -n1 find . -type f -name | awk '{print "-object "$1}') -output-dir=html WORKDIR /work diff --git a/external/toxcore/c-toxcore/other/docker/coverage/Dockerfile.nginx b/external/toxcore/c-toxcore/other/docker/coverage/Dockerfile.nginx index 5e085978..b858dd26 100644 --- a/external/toxcore/c-toxcore/other/docker/coverage/Dockerfile.nginx +++ b/external/toxcore/c-toxcore/other/docker/coverage/Dockerfile.nginx @@ -1,5 +1,4 @@ # vim:ft=dockerfile FROM toxchat/c-toxcore:coverage AS build FROM nginx:alpine -COPY --from=build /work/_build/html/coverage_details.html /usr/share/nginx/html/index.html -COPY --from=build /work/_build/html/ /usr/share/nginx/html/ +COPY --from=build --chown=nginx:nginx /work/_build/html/ /usr/share/nginx/html/ diff --git a/external/toxcore/c-toxcore/other/docker/coverage/run b/external/toxcore/c-toxcore/other/docker/coverage/run index 368d8429..aea7603c 100755 --- a/external/toxcore/c-toxcore/other/docker/coverage/run +++ b/external/toxcore/c-toxcore/other/docker/coverage/run @@ -6,4 +6,4 @@ read -a ci_env <<<"$(bash <(curl -s https://codecov.io/env))" docker build -t toxchat/c-toxcore:sources -f other/docker/sources/Dockerfile . docker build -t toxchat/c-toxcore:coverage -f other/docker/coverage/Dockerfile . -docker run "${ci_env[@]}" -e CI=true --name toxcore-coverage --rm -t toxchat/c-toxcore:coverage /usr/local/bin/codecov -x "llvm-cov gcov" +docker run "${ci_env[@]}" -e CI=true --name toxcore-coverage --rm -t toxchat/c-toxcore:coverage /usr/local/bin/codecov diff --git a/external/toxcore/c-toxcore/other/docker/coverage/run_mallocfail b/external/toxcore/c-toxcore/other/docker/coverage/run_mallocfail index d75e5f73..2604e961 100755 --- a/external/toxcore/c-toxcore/other/docker/coverage/run_mallocfail +++ b/external/toxcore/c-toxcore/other/docker/coverage/run_mallocfail @@ -27,11 +27,12 @@ from typing import NoReturn from typing import Optional from typing import Tuple -_PRIMER = "./unit_util_test" +_PRIMER = "auto_tests/auto_version_test" _MALLOCFAIL_SO = "/usr/local/lib/mallocfail.so" _HASHES = "mallocfail_hashes" _HASHES_PREV = "mallocfail_hashes.prev" _TIMEOUT = 3.0 +_BUILD_DIR = os.getcwd() _ENV = { "LD_PRELOAD": _MALLOCFAIL_SO, @@ -45,10 +46,19 @@ def run_mallocfail(tmpdir: str, timeout: float, exe: str, iteration: int, print(f"\x1b[1;33mmallocfail '{exe}' run #{iteration}\x1b[0m") hashes = os.path.join(tmpdir, _HASHES) hashes_prev = os.path.join(tmpdir, _HASHES_PREV) + profraw = os.path.join(_BUILD_DIR, "mallocfail.out", exe, "%p.profraw") if os.path.exists(hashes): shutil.copy(hashes, hashes_prev) try: - proc = subprocess.run([exe], timeout=timeout, env=_ENV, cwd=tmpdir) + proc = subprocess.run( + [exe], + timeout=timeout, + env={ + "LLVM_PROFILE_FILE": profraw, + **_ENV, + }, + cwd=tmpdir, + ) except subprocess.TimeoutExpired: print(f"\x1b[1;34mProgram {exe} timed out\x1b[0m") return True @@ -65,13 +75,16 @@ def run_mallocfail(tmpdir: str, timeout: float, exe: str, iteration: int, # Process exited cleanly (success or failure). pass elif proc.returncode == -6: - # Assertion failed. + # abort(), we allow it. + pass + elif proc.returncode == 7: + # ck_assert failed, also fine for us. pass elif proc.returncode == -14: print(f"\x1b[0;34mProgram '{exe}' timed out\x1b[0m") else: print( - f"\x1b[1;32mProgram '{exe}' failed to handle OOM situation cleanly\x1b[0m" + f"\x1b[1;32mProgram '{exe}' failed to handle OOM situation cleanly (code {proc.returncode})\x1b[0m" ) if not keep_going: raise Exception("Aborting test") @@ -96,8 +109,8 @@ def find_prog(name: str) -> Tuple[Optional[str], ...]: return path return None - return (attempt(f"./unit_{name}_test"), - attempt(f"auto_tests/auto_{name}_test")) + return (attempt(f"auto_tests/auto_{name}_test"), + ) # attempt(f"./unit_{name}_test"), def parse_flags(args: List[str]) -> Tuple[Dict[str, str], List[str]]: @@ -128,6 +141,9 @@ def isolated_mallocfail(timeout: int, exe: str) -> None: shutil.copy(exe, os.path.join(tmpdir, exe)) shutil.copy(_HASHES, os.path.join(tmpdir, _HASHES)) loop_mallocfail(tmpdir, timeout, exe) + profraw = os.path.join(tmpdir, "default.profraw") + if os.path.exists(profraw): + shutil.copy(profraw, exe + ".mallocfail.profraw") def main(args: List[str]) -> None: @@ -150,12 +166,12 @@ def main(args: List[str]) -> None: else: jobs = 1 - # Start by running util_test, which allocates no memory of its own, just + # Start by running version_test, which allocates no memory of its own, just # to prime the mallocfail hashes so it doesn't make global initialisers # such as llvm_gcov_init fail. if os.path.exists(_PRIMER): - print(f"\x1b[1;33mPriming hashes with unit_util_test\x1b[0m") - loop_mallocfail(".", timeout, _PRIMER, keep_going=True) + print(f"\x1b[1;33mPriming hashes with {_PRIMER}\x1b[0m") + loop_mallocfail(os.getcwd(), timeout, _PRIMER, keep_going=True) print(f"\x1b[1;33m--------------------------------\x1b[0m") print(f"\x1b[1;33mStarting mallocfail for {len(exes)} programs:\x1b[0m") diff --git a/external/toxcore/c-toxcore/other/docker/coverage/syscall_funcs.c b/external/toxcore/c-toxcore/other/docker/coverage/syscall_funcs.c deleted file mode 100644 index 3d768571..00000000 --- a/external/toxcore/c-toxcore/other/docker/coverage/syscall_funcs.c +++ /dev/null @@ -1,149 +0,0 @@ -#define _GNU_SOURCE - -#include "mallocfail.h" - -#include -#include -#include -#include -#include -#include -#include - -static int (*libc_ioctl)(int fd, unsigned long request, ...); -static int (*libc_bind)(int sockfd, const struct sockaddr *addr, - socklen_t addrlen); -static int (*libc_getsockopt)(int sockfd, int level, int optname, - void *optval, socklen_t *optlen); -static int (*libc_setsockopt)(int sockfd, int level, int optname, - const void *optval, socklen_t optlen); -static ssize_t (*libc_recv)(int sockfd, void *buf, size_t len, int flags); -static ssize_t (*libc_recvfrom)(int sockfd, void *buf, size_t len, int flags, - struct sockaddr *src_addr, socklen_t *addrlen); -static ssize_t (*libc_send)(int sockfd, const void *buf, size_t len, int flags); -static ssize_t(*libc_sendto)(int sockfd, const void *buf, size_t len, int flags, - const struct sockaddr *dest_addr, socklen_t addrlen); -static int (*libc_socket)(int domain, int type, int protocol); -static int (*libc_listen)(int sockfd, int backlog); - -__attribute__((__constructor__)) -static void init(void) -{ - libc_ioctl = dlsym(RTLD_NEXT, "ioctl"); - libc_bind = dlsym(RTLD_NEXT, "bind"); - libc_getsockopt = dlsym(RTLD_NEXT, "getsockopt"); - libc_setsockopt = dlsym(RTLD_NEXT, "setsockopt"); - libc_recv = dlsym(RTLD_NEXT, "recv"); - libc_recvfrom = dlsym(RTLD_NEXT, "recvfrom"); - libc_send = dlsym(RTLD_NEXT, "send"); - libc_sendto = dlsym(RTLD_NEXT, "sendto"); - libc_socket = dlsym(RTLD_NEXT, "socket"); - libc_listen = dlsym(RTLD_NEXT, "listen"); -} - -int ioctl(int fd, unsigned long request, ...) -{ - if (should_malloc_fail()) { - errno = ENOMEM; - return -1; - } - - va_list ap; - va_start(ap, request); - const int ret = libc_ioctl(fd, SIOCGIFCONF, va_arg(ap, void *)); - va_end(ap); - return ret; -} - -int bind(int sockfd, const struct sockaddr *addr, - socklen_t addrlen) -{ - // Unlike all others, if bind should fail once, it should fail always, because in toxcore we try - // many ports before giving up. If this only fails once, we'll never reach the code path where - // we give up. - static int should_fail = -1; - if (should_fail == -1) { - should_fail = should_malloc_fail(); - } - if (should_fail) { - errno = ENOMEM; - return -1; - } - return libc_bind(sockfd, addr, addrlen); -} - -int getsockopt(int sockfd, int level, int optname, - void *optval, socklen_t *optlen) -{ - if (should_malloc_fail()) { - errno = ENOMEM; - return -1; - } - return libc_getsockopt(sockfd, level, optname, optval, optlen); -} - -int setsockopt(int sockfd, int level, int optname, - const void *optval, socklen_t optlen) -{ - if (should_malloc_fail()) { - errno = ENOMEM; - return -1; - } - return libc_setsockopt(sockfd, level, optname, optval, optlen); -} - -ssize_t recv(int sockfd, void *buf, size_t len, int flags) -{ - if (should_malloc_fail()) { - errno = ENOMEM; - return -1; - } - return libc_recv(sockfd, buf, len, flags); -} - -ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags, - struct sockaddr *src_addr, socklen_t *addrlen) -{ - if (should_malloc_fail()) { - errno = ENOMEM; - return -1; - } - return libc_recvfrom(sockfd, buf, len, flags, src_addr, addrlen); -} - -ssize_t send(int sockfd, const void *buf, size_t len, int flags) -{ - if (should_malloc_fail()) { - errno = ENOMEM; - return -1; - } - return libc_send(sockfd, buf, len, flags); -} - -ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, - const struct sockaddr *dest_addr, socklen_t addrlen) -{ - if (should_malloc_fail()) { - errno = ENOMEM; - return -1; - } - return libc_sendto(sockfd, buf, len, flags, dest_addr, addrlen); -} - -int socket(int domain, int type, int protocol) -{ - if (should_malloc_fail()) { - errno = ENOMEM; - return -1; - } - return libc_socket(domain, type, protocol); -} - -int listen(int sockfd, int backlog) -{ - if (should_malloc_fail()) { - errno = ENOMEM; - return -1; - } - return libc_listen(sockfd, backlog); -} diff --git a/external/toxcore/c-toxcore/other/docker/doxygen/Dockerfile b/external/toxcore/c-toxcore/other/docker/doxygen/Dockerfile index f79fd197..e10c645d 100644 --- a/external/toxcore/c-toxcore/other/docker/doxygen/Dockerfile +++ b/external/toxcore/c-toxcore/other/docker/doxygen/Dockerfile @@ -5,7 +5,7 @@ ENV LANG=en_US.UTF-8 \ LC_CTYPE=en_US.UTF-8 \ LC_ALL=en_US.UTF-8 -RUN apk add --no-cache doxygen git graphviz \ +RUN apk add --no-cache doxygen git graphviz texlive \ && git clone --depth=1 https://github.com/jothepro/doxygen-awesome-css.git /work/doxygen-awesome-css WORKDIR /work COPY . /work/ diff --git a/external/toxcore/c-toxcore/other/docker/infer/Dockerfile b/external/toxcore/c-toxcore/other/docker/infer/Dockerfile new file mode 100644 index 00000000..b4f8ebb2 --- /dev/null +++ b/external/toxcore/c-toxcore/other/docker/infer/Dockerfile @@ -0,0 +1,41 @@ +FROM toxchat/infer:latest + +COPY toxav/ /work/c-toxcore/toxav/ +COPY toxcore/ /work/c-toxcore/toxcore/ +COPY toxencryptsave/ /work/c-toxcore/toxencryptsave/ +COPY third_party/ /work/c-toxcore/third_party/ +RUN infer capture -- clang++ -fsyntax-only \ + $(pkg-config --cflags libconfig libsodium opus vpx) \ + /work/c-toxcore/toxav/*.c \ + /work/c-toxcore/toxcore/*.c \ + /work/c-toxcore/toxcore/*/*.c \ + /work/c-toxcore/toxencryptsave/*.c +RUN ["infer", "analyze",\ + "--report-console-limit", "100",\ + "--jobs", "8",\ + "--no-bufferoverrun",\ + "--no-datalog",\ + "--print-active-checkers",\ + "--loop-hoisting",\ + "--quandary",\ + "--racerd",\ + "--starvation",\ + "--uninit",\ + "--disable-issue-type", "BUFFER_OVERRUN_L2",\ + "--disable-issue-type", "PULSE_UNNECESSARY_COPY",\ + "--enable-issue-type", "EXPENSIVE_EXECUTION_TIME",\ + "--enable-issue-type", "INVARIANT_CALL",\ + "--enable-issue-type", "PULSE_UNINITIALIZED_CONST",\ + "--enable-issue-type", "SENSITIVE_DATA_FLOW",\ + "--enable-issue-type", "UNTRUSTED_BUFFER_ACCESS",\ + "--enable-issue-type", "UNTRUSTED_HEAP_ALLOCATION",\ + "--disable-issue-type", "USE_AFTER_FREE_LATENT",\ + "--disable-issue-type", "STACK_VARIABLE_ADDRESS_ESCAPE",\ + "--disable-issue-type", "INVARIANT_CALL",\ + "--fail-on-issue"] +# In the above, the first 2 are disabled for extreme sensitivity and false +# positives, the ones at the end are probably decent, but have some false +# positives, so we can't fail-on-issue with them on. +# INVARIANT_CALL is pretty fun, but currently wrong, because it can't see +# through potential mutations via callbacks. Our code is bad and we should +# feel bad, but until that's fixed, the invariant checker doesn't work. diff --git a/external/toxcore/c-toxcore/other/docker/infer/run b/external/toxcore/c-toxcore/other/docker/infer/run new file mode 100755 index 00000000..ab1c021c --- /dev/null +++ b/external/toxcore/c-toxcore/other/docker/infer/run @@ -0,0 +1,5 @@ +#!/bin/sh + +set -eux +BUILD=infer +docker build -t "toxchat/c-toxcore:$BUILD" -f "other/docker/$BUILD/Dockerfile" . diff --git a/external/toxcore/c-toxcore/other/docker/windows/Dockerfile b/external/toxcore/c-toxcore/other/docker/windows/Dockerfile index 89dc4de7..7d4d59a4 100644 --- a/external/toxcore/c-toxcore/other/docker/windows/Dockerfile +++ b/external/toxcore/c-toxcore/other/docker/windows/Dockerfile @@ -2,7 +2,7 @@ FROM debian:bullseye-slim # Build-time environment variables ARG VERSION_MSGPACK=4.0.0 \ - VERSION_SODIUM=1.0.18 \ + VERSION_SODIUM=1.0.19 \ VERSION_OPUS=1.3.1 \ VERSION_VPX=1.11.0 \ \ diff --git a/external/toxcore/c-toxcore/other/docker/windows/build_dependencies.sh b/external/toxcore/c-toxcore/other/docker/windows/build_dependencies.sh index d46f63cf..dcba4f4d 100755 --- a/external/toxcore/c-toxcore/other/docker/windows/build_dependencies.sh +++ b/external/toxcore/c-toxcore/other/docker/windows/build_dependencies.sh @@ -40,9 +40,9 @@ build() { echo echo "=== Building Sodium $VERSION_SODIUM $ARCH ===" - curl "${CURL_OPTIONS[@]}" -O "https://download.libsodium.org/libsodium/releases/libsodium-$VERSION_SODIUM.tar.gz" + curl "${CURL_OPTIONS[@]}" -O "https://github.com/jedisct1/libsodium/releases/download/$VERSION_SODIUM-RELEASE/libsodium-$VERSION_SODIUM.tar.gz" tar -xf "libsodium-$VERSION_SODIUM.tar.gz" - cd "libsodium-$VERSION_SODIUM" + cd "libsodium-stable" ./configure --host="$WINDOWS_TOOLCHAIN" --prefix="$PREFIX_DIR" --disable-shared --enable-static make make install diff --git a/external/toxcore/c-toxcore/other/event_tooling/generate_event_c.cpp b/external/toxcore/c-toxcore/other/event_tooling/generate_event_c.cpp index 5cdbfb9e..b736b6f2 100644 --- a/external/toxcore/c-toxcore/other/event_tooling/generate_event_c.cpp +++ b/external/toxcore/c-toxcore/other/event_tooling/generate_event_c.cpp @@ -237,10 +237,11 @@ void generate_event_impl(const std::string& event_name, const std::vector" << t.name_data << " = nullptr;\n"; f << " " << event_name_l << "->" << t.name_length << " = 0;\n"; f << " }\n\n"; - f << " " << event_name_l << "->" << t.name_data << " = (uint8_t *)malloc(" << t.name_length << ");\n\n"; - f << " if (" << event_name_l << "->" << t.name_data << " == nullptr) {\n"; + f << " uint8_t *" << t.name_data << "_copy = (uint8_t *)malloc(" << t.name_length << ");\n\n"; + f << " if (" << t.name_data << "_copy == nullptr) {\n"; f << " return false;\n }\n\n"; - f << " memcpy(" << event_name_l << "->" << t.name_data << ", " << t.name_data << ", " << t.name_length << ");\n"; + f << " memcpy(" << t.name_data << "_copy, " << t.name_data << ", " << t.name_length << ");\n"; + f << " " << event_name_l << "->" << t.name_data << " = " << t.name_data << "_copy;\n"; f << " " << event_name_l << "->" << t.name_length << " = " << t.name_length << ";\n"; f << " return true;\n"; } @@ -403,28 +404,6 @@ void generate_event_impl(const std::string& event_name, const std::vector index) {\n"; - f << " return nullptr;\n }\n\n"; - f << " if (events->events[i].type == TOX_EVENT_" << str_toupper(event_name) << ") {\n"; - f << " const Tox_Event_" << event_name << " *" << event_name_l << " = events->events[i].data." << event_name_l << ";\n"; - f << " if (" << event_name_l << "_index == index) {\n"; - f << " return " << event_name_l << ";\n }\n"; - f << " ++" << event_name_l << "_index;\n }\n }\n\n return nullptr;\n}\n\n"; - - // get size - f << "uint32_t tox_events_get_" << event_name_l << "_size(const Tox_Events *events)\n{\n"; - f << " uint32_t " << event_name_l << "_size = 0;\n"; - f << " const uint32_t size = tox_events_get_size(events);\n\n"; - f << " for (uint32_t i = 0; i < size; ++i) {\n"; - f << " if (events->events[i].type == TOX_EVENT_" << str_toupper(event_name) << ") {\n"; - f << " ++" << event_name_l << "_size;\n }\n }\n\n"; - f << " return " << event_name_l << "_size;\n}\n\n"; - // unpack f << "bool tox_event_" << event_name_l << "_unpack(\n"; f << " Tox_Event_" << event_name << " **event, Bin_Unpack *bu, const Memory *mem)\n{\n"; diff --git a/external/toxcore/c-toxcore/sonar-project.properties b/external/toxcore/c-toxcore/sonar-project.properties index 4621a7de..d4be6b89 100644 --- a/external/toxcore/c-toxcore/sonar-project.properties +++ b/external/toxcore/c-toxcore/sonar-project.properties @@ -10,3 +10,6 @@ sonar.sources=. # Encoding of the source code. sonar.sourceEncoding=UTF-8 + +# More precise Python version. +sonar.python.version=3.11 diff --git a/external/toxcore/c-toxcore/testing/fuzzing/CMakeLists.txt b/external/toxcore/c-toxcore/testing/fuzzing/CMakeLists.txt index c2b88c72..d1fd7534 100644 --- a/external/toxcore/c-toxcore/testing/fuzzing/CMakeLists.txt +++ b/external/toxcore/c-toxcore/testing/fuzzing/CMakeLists.txt @@ -1,26 +1,33 @@ -# For coverage tests -target_compile_definitions(toxcore_static PUBLIC "FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION") - # Override network and random functions add_library(fuzz_support func_conversion.h fuzz_support.cc fuzz_support.h) set(LIBFUZZER_LINKER_FLAGS) -if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") +if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang") set(LIBFUZZER_LINKER_FLAGS "-fsanitize=fuzzer") else() message(SEND_ERROR "Compiler must be Clang to build fuzz targets") endif() +function(fuzz_test target source_dir) + set(${target}_CORPUS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/toktok-fuzzer/corpus/${target}_fuzz_test) + file(GLOB ${target}_fuzz_CORPUS "${${target}_CORPUS_DIR}/*") + add_executable(${target}_fuzz_test ${source_dir}/${target}_fuzz_test.cc) + target_link_libraries(${target}_fuzz_test PRIVATE toxcore_fuzz fuzz_support ${LIBFUZZER_LINKER_FLAGS}) + if(${target}_fuzz_CORPUS) + add_test(NAME ${target}_fuzz COMMAND ${CROSSCOMPILING_EMULATOR} ${target}_fuzz_test -max_total_time=10 ${${target}_fuzz_CORPUS}) + endif() +endfunction() + # Fuzzes the toxsave API add_executable(toxsave_fuzzer toxsave_harness.cc) -target_link_libraries(toxsave_fuzzer PRIVATE toxcore_static fuzz_support ${LIBFUZZER_LINKER_FLAGS}) +target_link_libraries(toxsave_fuzzer PRIVATE toxcore_fuzz fuzz_support ${LIBFUZZER_LINKER_FLAGS}) # Fuzzes the bootstrap process add_executable(bootstrap_fuzzer bootstrap_harness.cc) -target_link_libraries(bootstrap_fuzzer PRIVATE toxcore_static fuzz_support ${LIBFUZZER_LINKER_FLAGS}) +target_link_libraries(bootstrap_fuzzer PRIVATE toxcore_fuzz fuzz_support ${LIBFUZZER_LINKER_FLAGS}) -add_executable(DHT_fuzz_test ../../toxcore/DHT_fuzz_test.cc) -target_link_libraries(DHT_fuzz_test PRIVATE toxcore_static fuzz_support ${LIBFUZZER_LINKER_FLAGS}) - -add_executable(tox_events_fuzz_test ../../toxcore/tox_events_fuzz_test.cc) -target_link_libraries(tox_events_fuzz_test PRIVATE toxcore_static fuzz_support ${LIBFUZZER_LINKER_FLAGS}) +fuzz_test(DHT ../../toxcore) +fuzz_test(forwarding ../../toxcore) +fuzz_test(group_announce ../../toxcore) +fuzz_test(group_moderation ../../toxcore) +fuzz_test(tox_events ../../toxcore) diff --git a/external/toxcore/c-toxcore/testing/fuzzing/bootstrap_harness.cc b/external/toxcore/c-toxcore/testing/fuzzing/bootstrap_harness.cc index 2e9639e2..9c178f21 100644 --- a/external/toxcore/c-toxcore/testing/fuzzing/bootstrap_harness.cc +++ b/external/toxcore/c-toxcore/testing/fuzzing/bootstrap_harness.cc @@ -126,7 +126,7 @@ void TestBootstrap(Fuzz_Data &input) } }); - CONSUME1_OR_RETURN(const uint8_t proxy_type, input); + CONSUME1_OR_RETURN(const uint8_t, proxy_type, input); if (proxy_type == 0) { tox_options_set_proxy_type(opts.get(), TOX_PROXY_TYPE_NONE); } else if (proxy_type == 1) { @@ -139,7 +139,7 @@ void TestBootstrap(Fuzz_Data &input) tox_options_set_proxy_port(opts.get(), 8080); } - CONSUME1_OR_RETURN(const uint8_t tcp_relay_enabled, input); + CONSUME1_OR_RETURN(const uint8_t, tcp_relay_enabled, input); if (tcp_relay_enabled >= (UINT8_MAX / 2)) { tox_options_set_tcp_port(opts.get(), 33445); } diff --git a/external/toxcore/c-toxcore/testing/fuzzing/e2e_fuzz_test.cc b/external/toxcore/c-toxcore/testing/fuzzing/e2e_fuzz_test.cc index f5258853..d19f0d6c 100644 --- a/external/toxcore/c-toxcore/testing/fuzzing/e2e_fuzz_test.cc +++ b/external/toxcore/c-toxcore/testing/fuzzing/e2e_fuzz_test.cc @@ -8,6 +8,7 @@ #include #include +#include "../../toxcore/crypto_core.h" #include "../../toxcore/tox.h" #include "../../toxcore/tox_dispatch.h" #include "../../toxcore/tox_events.h" diff --git a/external/toxcore/c-toxcore/testing/fuzzing/fuzz_support.cc b/external/toxcore/c-toxcore/testing/fuzzing/fuzz_support.cc index 04a0bf74..59b027c7 100644 --- a/external/toxcore/c-toxcore/testing/fuzzing/fuzz_support.cc +++ b/external/toxcore/c-toxcore/testing/fuzzing/fuzz_support.cc @@ -30,6 +30,16 @@ struct Network_Addr { size_t size; }; +System::System(std::unique_ptr in_sys, std::unique_ptr in_mem, + std::unique_ptr in_ns, std::unique_ptr in_rng) + : sys(std::move(in_sys)) + , mem(std::move(in_mem)) + , ns(std::move(in_ns)) + , rng(std::move(in_rng)) +{ +} +System::System(System &&) = default; + System::~System() { } static int recv_common(Fuzz_Data &input, uint8_t *buf, size_t buf_len) @@ -67,7 +77,7 @@ static int recv_common(Fuzz_Data &input, uint8_t *buf, size_t buf_len) template static void *alloc_common(Fuzz_Data &data, F func) { - CONSUME1_OR_RETURN_VAL(const uint8_t want_alloc, data, func()); + CONSUME1_OR_RETURN_VAL(const uint8_t, want_alloc, data, func()); if (!want_alloc) { return nullptr; } diff --git a/external/toxcore/c-toxcore/testing/fuzzing/fuzz_support.h b/external/toxcore/c-toxcore/testing/fuzzing/fuzz_support.h index a20d85b4..1735d6b1 100644 --- a/external/toxcore/c-toxcore/testing/fuzzing/fuzz_support.h +++ b/external/toxcore/c-toxcore/testing/fuzzing/fuzz_support.h @@ -7,11 +7,12 @@ #include #include +#include #include #include -#include #include #include +#include #include "../../toxcore/tox.h" @@ -20,19 +21,28 @@ struct Fuzz_Data { std::size_t size; Fuzz_Data(const uint8_t *input_data, std::size_t input_size) - : data(input_data), size(input_size) - {} + : data(input_data) + , size(input_size) + { + } Fuzz_Data &operator=(const Fuzz_Data &rhs) = delete; Fuzz_Data(const Fuzz_Data &rhs) = delete; - uint8_t consume1() - { - const uint8_t val = data[0]; - ++data; - --size; - return val; - } + struct Consumer { + Fuzz_Data &fd; + + template + operator T() + { + const uint8_t *bytes = fd.consume(sizeof(T)); + T val; + std::memcpy(&val, bytes, sizeof(T)); + return val; + } + }; + + Consumer consume1() { return Consumer{*this}; } const uint8_t *consume(std::size_t count) { @@ -50,14 +60,14 @@ struct Fuzz_Data { * * @example * @code - * CONSUME1_OR_RETURN(const uint8_t one_byte, input); + * CONSUME1_OR_RETURN(const uint8_t, one_byte, input); * @endcode */ -#define CONSUME1_OR_RETURN(DECL, INPUT) \ - if (INPUT.size < 1) { \ - return; \ - } \ - DECL = INPUT.consume1() +#define CONSUME1_OR_RETURN(TYPE, NAME, INPUT) \ + if (INPUT.size < sizeof(TYPE)) { \ + return; \ + } \ + TYPE NAME = INPUT.consume1() /** @brief Consumes 1 byte of the fuzzer input or returns a value if no data * available. @@ -70,11 +80,11 @@ struct Fuzz_Data { * CONSUME1_OR_RETURN_VAL(const uint8_t one_byte, input, nullptr); * @endcode */ -#define CONSUME1_OR_RETURN_VAL(DECL, INPUT, VAL) \ - if (INPUT.size < 1) { \ - return VAL; \ - } \ - DECL = INPUT.consume1() +#define CONSUME1_OR_RETURN_VAL(TYPE, NAME, INPUT, VAL) \ + if (INPUT.size < sizeof(TYPE)) { \ + return VAL; \ + } \ + TYPE NAME = INPUT.consume1() /** @brief Consumes SIZE bytes of the fuzzer input or returns if not enough data available. * @@ -93,6 +103,12 @@ struct Fuzz_Data { } \ DECL = INPUT.consume(SIZE) +#define CONSUME_OR_RETURN_VAL(DECL, INPUT, SIZE, VAL) \ + if (INPUT.size < SIZE) { \ + return VAL; \ + } \ + DECL = INPUT.consume(SIZE) + inline void fuzz_select_target(uint8_t selector, Fuzz_Data &input) { // The selector selected no function, so we do nothing and rely on the @@ -100,7 +116,7 @@ inline void fuzz_select_target(uint8_t selector, Fuzz_Data &input) } template -void fuzz_select_target(uint8_t selector, Fuzz_Data &input, Arg &&fn, Args &&... args) +void fuzz_select_target(uint8_t selector, Fuzz_Data &input, Arg &&fn, Args &&...args) { if (selector == sizeof...(Args)) { return fn(input); @@ -109,11 +125,11 @@ void fuzz_select_target(uint8_t selector, Fuzz_Data &input, Arg &&fn, Args &&... } template -void fuzz_select_target(const uint8_t *data, std::size_t size, Args &&... args) +void fuzz_select_target(const uint8_t *data, std::size_t size, Args &&...args) { Fuzz_Data input{data, size}; - CONSUME1_OR_RETURN(uint8_t selector, input); + CONSUME1_OR_RETURN(const uint8_t, selector, input); return fuzz_select_target(selector, input, std::forward(args)...); } @@ -127,6 +143,10 @@ struct System { std::unique_ptr ns; std::unique_ptr rng; + System(std::unique_ptr sys, std::unique_ptr mem, + std::unique_ptr ns, std::unique_ptr rng); + System(System &&); + // Not inline because sizeof of the above 2 structs is not known everywhere. ~System(); diff --git a/external/toxcore/c-toxcore/testing/fuzzing/fuzz_tox.h b/external/toxcore/c-toxcore/testing/fuzzing/fuzz_tox.h index b5864e78..c19bb1bd 100644 --- a/external/toxcore/c-toxcore/testing/fuzzing/fuzz_tox.h +++ b/external/toxcore/c-toxcore/testing/fuzzing/fuzz_tox.h @@ -1,96 +1,17 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * Copyright © 2022 The TokTok team. + * Copyright © 2022-2024 The TokTok team. */ #ifndef C_TOXCORE_TESTING_FUZZING_FUZZ_TOX_H #define C_TOXCORE_TESTING_FUZZING_FUZZ_TOX_H -#include #include -#include "../../toxcore/DHT.h" -#include "../../toxcore/logger.h" #include "../../toxcore/network.h" -#include "fuzz_support.h" constexpr uint16_t SIZE_IP_PORT = SIZE_IP6 + sizeof(uint16_t); template using Ptr = std::unique_ptr; -/** @brief Construct any Tox resource using fuzzer input data. - * - * Constructs (or fails by returning) a valid object of type T and passes it to - * a function specified on the rhs of `>>`. Takes care of cleaning up the - * resource after the specified function returns. - * - * Some `with` instances require additional inputs such as the `Fuzz_Data` - * reference or a logger. - */ -template -struct with; - -/** @brief Construct a Logger without logging callback. - */ -template <> -struct with { - template - void operator>>(F &&f) - { - Ptr logger(logger_new(), logger_kill); - assert(logger != nullptr); - f(std::move(logger)); - } -}; - -/** @brief Construct an IP_Port by unpacking fuzzer input with `unpack_ip_port`. - */ -template <> -struct with { - Fuzz_Data &input_; - - template - void operator>>(F &&f) - { - CONSUME_OR_RETURN(const uint8_t *ipp_packed, input_, SIZE_IP_PORT); - IP_Port ipp; - unpack_ip_port(&ipp, ipp_packed, SIZE_IP6, true); - - f(ipp); - } -}; - -/** @brief Construct a Networking_Core object using the Network vtable passed. - * - * Use `with{} >> with{input, ns, mem} >> ...` to construct - * a logger and pass it to the Networking_Core constructor function. - */ -template <> -struct with { - Fuzz_Data &input_; - const Network *ns_; - const Memory *mem_; - Ptr logger_{nullptr, logger_kill}; - - friend with operator>>(with f, with self) - { - f >> [&self](Ptr logger) { self.logger_ = std::move(logger); }; - return self; - } - - template - void operator>>(F &&f) - { - with{input_} >> [&f, this](const IP_Port &ipp) { - Ptr net( - new_networking_ex(logger_.get(), mem_, ns_, &ipp.ip, ipp.port, ipp.port + 100, nullptr), - kill_networking); - if (net == nullptr) { - return; - } - f(std::move(net)); - }; - } -}; - #endif // C_TOXCORE_TESTING_FUZZING_FUZZ_TOX_H diff --git a/external/toxcore/c-toxcore/testing/fuzzing/protodump_reduce.cc b/external/toxcore/c-toxcore/testing/fuzzing/protodump_reduce.cc index 847ac0b2..8cc475bb 100644 --- a/external/toxcore/c-toxcore/testing/fuzzing/protodump_reduce.cc +++ b/external/toxcore/c-toxcore/testing/fuzzing/protodump_reduce.cc @@ -1,6 +1,7 @@ #include #include +#include "../../toxcore/crypto_core.h" #include "../../toxcore/tox.h" #include "../../toxcore/tox_dispatch.h" #include "../../toxcore/tox_events.h" diff --git a/external/toxcore/c-toxcore/toxav/msi.c b/external/toxcore/c-toxcore/toxav/msi.c index 6375995a..aaa8d551 100644 --- a/external/toxcore/c-toxcore/toxav/msi.c +++ b/external/toxcore/c-toxcore/toxav/msi.c @@ -587,7 +587,7 @@ static MSICall *new_call(MSISession *session, uint32_t friend_number) session->calls_tail = friend_number; session->calls_head = friend_number; } else if (session->calls_tail < friend_number) { /* Appending */ - MSICall **tmp = (MSICall **)realloc(session->calls, sizeof(MSICall *) * (friend_number + 1)); + MSICall **tmp = (MSICall **)realloc(session->calls, (friend_number + 1) * sizeof(MSICall *)); if (tmp == nullptr) { free(rc); diff --git a/external/toxcore/c-toxcore/toxav/ring_buffer.c b/external/toxcore/c-toxcore/toxav/ring_buffer.c index 703c2a5f..9fd6b4e7 100644 --- a/external/toxcore/c-toxcore/toxav/ring_buffer.c +++ b/external/toxcore/c-toxcore/toxav/ring_buffer.c @@ -106,8 +106,9 @@ uint16_t rb_size(const RingBuffer *b) uint16_t rb_data(const RingBuffer *b, void **dest) { uint16_t i; + const uint16_t size = rb_size(b); - for (i = 0; i < rb_size(b); ++i) { + for (i = 0; i < size; ++i) { dest[i] = b->data[(b->start + i) % b->size]; } diff --git a/external/toxcore/c-toxcore/toxav/toxav.c b/external/toxcore/c-toxcore/toxav/toxav.c index bdec43b5..ea220838 100644 --- a/external/toxcore/c-toxcore/toxav/toxav.c +++ b/external/toxcore/c-toxcore/toxav/toxav.c @@ -1284,7 +1284,7 @@ static ToxAVCall *call_new(ToxAV *av, uint32_t friend_number, Toxav_Err_Call *er av->calls_tail = friend_number; av->calls_head = friend_number; } else if (av->calls_tail < friend_number) { /* Appending */ - ToxAVCall **tmp = (ToxAVCall **)realloc(av->calls, sizeof(ToxAVCall *) * (friend_number + 1)); + ToxAVCall **tmp = (ToxAVCall **)realloc(av->calls, (friend_number + 1) * sizeof(ToxAVCall *)); if (tmp == nullptr) { pthread_mutex_destroy(call->toxav_call_mutex); diff --git a/external/toxcore/c-toxcore/toxcore/BUILD.bazel b/external/toxcore/c-toxcore/toxcore/BUILD.bazel index 9424debc..a4f9c88f 100644 --- a/external/toxcore/c-toxcore/toxcore/BUILD.bazel +++ b/external/toxcore/c-toxcore/toxcore/BUILD.bazel @@ -10,6 +10,23 @@ exports_files( visibility = ["//c-toxcore:__pkg__"], ) +cc_library( + name = "test_util", + srcs = ["test_util.cc"], + hdrs = ["test_util.hh"], +) + +cc_test( + name = "test_util_test", + size = "small", + srcs = ["test_util_test.cc"], + deps = [ + ":crypto_core_test_util", + "@com_google_googletest//:gtest", + "@com_google_googletest//:gtest_main", + ], +) + cc_library( name = "attributes", hdrs = ["attributes.h"], @@ -71,6 +88,7 @@ cc_test( srcs = ["util_test.cc"], deps = [ ":crypto_core", + ":crypto_core_test_util", ":util", "@com_google_googletest//:gtest", "@com_google_googletest//:gtest_main", @@ -142,6 +160,16 @@ cc_library( ], ) +cc_library( + name = "crypto_core_test_util", + srcs = ["crypto_core_test_util.cc"], + hdrs = ["crypto_core_test_util.hh"], + deps = [ + ":crypto_core", + ":test_util", + ], +) + cc_test( name = "crypto_core_test", size = "small", @@ -149,6 +177,7 @@ cc_test( flaky = True, deps = [ ":crypto_core", + ":crypto_core_test_util", ":util", "@com_google_googletest//:gtest", "@com_google_googletest//:gtest_main", @@ -261,6 +290,17 @@ cc_library( ], ) +cc_library( + name = "network_test_util", + srcs = ["network_test_util.cc"], + hdrs = ["network_test_util.hh"], + deps = [ + ":crypto_core", + ":network", + ":test_util", + ], +) + cc_test( name = "network_test", size = "small", @@ -357,13 +397,28 @@ cc_library( ], ) +cc_library( + name = "DHT_test_util", + srcs = ["DHT_test_util.cc"], + hdrs = ["DHT_test_util.hh"], + deps = [ + ":DHT", + ":crypto_core", + ":crypto_core_test_util", + ":network_test_util", + ":test_util", + ], +) + cc_test( name = "DHT_test", size = "small", srcs = ["DHT_test.cc"], deps = [ ":DHT", + ":DHT_test_util", ":crypto_core", + ":network_test_util", "@com_google_googletest//:gtest", "@com_google_googletest//:gtest_main", ], @@ -881,6 +936,7 @@ cc_fuzz_test( srcs = ["tox_events_fuzz_test.cc"], corpus = ["//tools/toktok-fuzzer/corpus:tox_events_fuzz_test"], deps = [ + ":tox_dispatch", ":tox_events", "//c-toxcore/testing/fuzzing:fuzz_support", ], diff --git a/external/toxcore/c-toxcore/toxcore/DHT.c b/external/toxcore/c-toxcore/toxcore/DHT.c index 42b89215..d83d48c9 100644 --- a/external/toxcore/c-toxcore/toxcore/DHT.c +++ b/external/toxcore/c-toxcore/toxcore/DHT.c @@ -448,8 +448,8 @@ int dht_create_packet(const Memory *mem, const Random *rng, const uint8_t *plain, size_t plain_length, uint8_t *packet, size_t length) { - uint8_t *encrypted = (uint8_t *)mem_balloc(mem, plain_length + CRYPTO_MAC_SIZE); uint8_t nonce[CRYPTO_NONCE_SIZE]; + uint8_t *encrypted = (uint8_t *)mem_balloc(mem, plain_length + CRYPTO_MAC_SIZE); if (encrypted == nullptr) { return -1; @@ -750,16 +750,21 @@ static bool client_or_ip_port_in_list(const Logger *log, const Mono_Time *mono_t return true; } -bool add_to_list(Node_format *nodes_list, uint32_t length, const uint8_t *pk, const IP_Port *ip_port, - const uint8_t *cmp_pk) +bool add_to_list( + Node_format *nodes_list, uint32_t length, const uint8_t pk[CRYPTO_PUBLIC_KEY_SIZE], + const IP_Port *ip_port, const uint8_t cmp_pk[CRYPTO_PUBLIC_KEY_SIZE]) { for (uint32_t i = 0; i < length; ++i) { - if (id_closest(cmp_pk, nodes_list[i].public_key, pk) == 2) { + Node_format *node = &nodes_list[i]; + + if (id_closest(cmp_pk, node->public_key, pk) == 2) { uint8_t pk_bak[CRYPTO_PUBLIC_KEY_SIZE]; - memcpy(pk_bak, nodes_list[i].public_key, CRYPTO_PUBLIC_KEY_SIZE); - const IP_Port ip_port_bak = nodes_list[i].ip_port; - memcpy(nodes_list[i].public_key, pk, CRYPTO_PUBLIC_KEY_SIZE); - nodes_list[i].ip_port = *ip_port; + memcpy(pk_bak, node->public_key, CRYPTO_PUBLIC_KEY_SIZE); + + const IP_Port ip_port_bak = node->ip_port; + memcpy(node->public_key, pk, CRYPTO_PUBLIC_KEY_SIZE); + + node->ip_port = *ip_port; if (i != length - 1) { add_to_list(nodes_list, length, pk_bak, &ip_port_bak, cmp_pk); @@ -776,10 +781,11 @@ bool add_to_list(Node_format *nodes_list, uint32_t length, const uint8_t *pk, co * helper for `get_close_nodes()`. argument list is a monster :D */ non_null() -static void get_close_nodes_inner(uint64_t cur_time, const uint8_t *public_key, Node_format *nodes_list, - Family sa_family, const Client_data *client_list, uint32_t client_list_length, - uint32_t *num_nodes_ptr, bool is_lan, - bool want_announce) +static void get_close_nodes_inner( + uint64_t cur_time, const uint8_t *public_key, + Node_format *nodes_list, uint32_t *num_nodes_ptr, + Family sa_family, const Client_data *client_list, uint32_t client_list_length, + bool is_lan, bool want_announce) { if (!net_family_is_ipv4(sa_family) && !net_family_is_ipv6(sa_family) && !net_family_is_unspec(sa_family)) { return; @@ -846,28 +852,44 @@ static void get_close_nodes_inner(uint64_t cur_time, const uint8_t *public_key, * want_announce: return only nodes which implement the dht announcements protocol. */ non_null() -static int get_somewhat_close_nodes(const DHT *dht, const uint8_t *public_key, Node_format *nodes_list, - Family sa_family, bool is_lan, bool want_announce) +static int get_somewhat_close_nodes( + uint64_t cur_time, const uint8_t *public_key, Node_format *nodes_list, + Family sa_family, const Client_data *close_clientlist, + const DHT_Friend *friends_list, uint16_t friends_list_size, + bool is_lan, bool want_announce) { - uint32_t num_nodes = 0; - get_close_nodes_inner(dht->cur_time, public_key, nodes_list, sa_family, - dht->close_clientlist, LCLIENT_LIST, &num_nodes, is_lan, want_announce); + memset(nodes_list, 0, MAX_SENT_NODES * sizeof(Node_format)); - for (uint32_t i = 0; i < dht->num_friends; ++i) { - get_close_nodes_inner(dht->cur_time, public_key, nodes_list, sa_family, - dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS, - &num_nodes, is_lan, want_announce); + uint32_t num_nodes = 0; + get_close_nodes_inner( + cur_time, public_key, + nodes_list, &num_nodes, + sa_family, close_clientlist, LCLIENT_LIST, + is_lan, want_announce); + + for (uint16_t i = 0; i < friends_list_size; ++i) { + const DHT_Friend *dht_friend = &friends_list[i]; + + get_close_nodes_inner( + cur_time, public_key, + nodes_list, &num_nodes, + sa_family, dht_friend->client_list, MAX_FRIEND_CLIENTS, + is_lan, want_announce); } return num_nodes; } -int get_close_nodes(const DHT *dht, const uint8_t *public_key, Node_format *nodes_list, Family sa_family, - bool is_lan, bool want_announce) +int get_close_nodes( + const DHT *dht, const uint8_t *public_key, + Node_format *nodes_list, Family sa_family, + bool is_lan, bool want_announce) { - memset(nodes_list, 0, MAX_SENT_NODES * sizeof(Node_format)); - return get_somewhat_close_nodes(dht, public_key, nodes_list, sa_family, - is_lan, want_announce); + return get_somewhat_close_nodes( + dht->cur_time, public_key, nodes_list, + sa_family, dht->close_clientlist, + dht->friends_list, dht->num_friends, + is_lan, want_announce); } typedef struct DHT_Cmp_Data { @@ -2903,23 +2925,27 @@ 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 - dht->loaded_nodes_list = (Node_format *)mem_valloc(dht->mem, MAX_SAVED_DHT_NODES, sizeof(Node_format)); - if (dht->loaded_nodes_list == nullptr) { + // Copy to loaded_clients_list + Node_format *nodes = (Node_format *)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); dht->loaded_num_nodes = 0; break; } - const int num = unpack_nodes(dht->loaded_nodes_list, MAX_SAVED_DHT_NODES, nullptr, data, length, false); + const int num = unpack_nodes(nodes, MAX_SAVED_DHT_NODES, nullptr, data, length, false); - if (num > 0) { - dht->loaded_num_nodes = num; - } else { + if (num < 0) { + // Unpack error happened, we ignore it. dht->loaded_num_nodes = 0; + } else { + dht->loaded_num_nodes = num; } + dht->loaded_nodes_list = nodes; + break; } diff --git a/external/toxcore/c-toxcore/toxcore/DHT.h b/external/toxcore/c-toxcore/toxcore/DHT.h index bc58f8e4..8dedc470 100644 --- a/external/toxcore/c-toxcore/toxcore/DHT.h +++ b/external/toxcore/c-toxcore/toxcore/DHT.h @@ -370,7 +370,8 @@ unsigned int bit_by_bit_cmp(const uint8_t *pk1, const uint8_t *pk2); */ non_null() bool add_to_list( - Node_format *nodes_list, uint32_t length, const uint8_t *pk, const IP_Port *ip_port, const uint8_t *cmp_pk); + Node_format *nodes_list, uint32_t length, const uint8_t pk[CRYPTO_PUBLIC_KEY_SIZE], + const IP_Port *ip_port, const uint8_t cmp_pk[CRYPTO_PUBLIC_KEY_SIZE]); /** Return 1 if node can be added to close list, 0 if it can't. */ non_null() @@ -383,18 +384,20 @@ void set_announce_node(DHT *dht, const uint8_t *public_key); #endif /** - * Get the (maximum MAX_SENT_NODES) closest nodes to public_key we know + * @brief Get the (maximum MAX_SENT_NODES) closest nodes to public_key we know * and put them in nodes_list (must be MAX_SENT_NODES big). * - * sa_family = family (IPv4 or IPv6) (0 if we don't care)? - * is_LAN = return some LAN ips (true or false) - * want_announce: return only nodes which implement the dht announcements protocol. + * @param sa_family family (IPv4 or IPv6) (0 if we don't care)? + * @param is_lan return some LAN ips (true or false). + * @param want_announce return only nodes which implement the dht announcements protocol. * * @return the number of nodes returned. */ non_null() -int get_close_nodes(const DHT *dht, const uint8_t *public_key, Node_format *nodes_list, Family sa_family, - bool is_lan, bool want_announce); +int get_close_nodes( + const DHT *dht, const uint8_t *public_key, + Node_format *nodes_list, Family sa_family, + bool is_lan, bool want_announce); /** @brief Put up to max_num nodes in nodes from the random friends. diff --git a/external/toxcore/c-toxcore/toxcore/DHT_fuzz_test.cc b/external/toxcore/c-toxcore/toxcore/DHT_fuzz_test.cc index a978fddd..7a9c7191 100644 --- a/external/toxcore/c-toxcore/toxcore/DHT_fuzz_test.cc +++ b/external/toxcore/c-toxcore/toxcore/DHT_fuzz_test.cc @@ -23,7 +23,7 @@ void TestHandleRequest(Fuzz_Data &input) void TestUnpackNodes(Fuzz_Data &input) { - CONSUME1_OR_RETURN(const bool tcp_enabled, input); + CONSUME1_OR_RETURN(const bool, tcp_enabled, input); const uint16_t node_count = 5; Node_format nodes[node_count]; diff --git a/external/toxcore/c-toxcore/toxcore/DHT_test.cc b/external/toxcore/c-toxcore/toxcore/DHT_test.cc index f5021613..95812582 100644 --- a/external/toxcore/c-toxcore/toxcore/DHT_test.cc +++ b/external/toxcore/c-toxcore/toxcore/DHT_test.cc @@ -1,15 +1,26 @@ #include "DHT.h" +#include #include #include #include +#include +#include +#include "DHT_test_util.hh" #include "crypto_core.h" +#include "crypto_core_test_util.hh" +#include "network_test_util.hh" namespace { -using PublicKey = std::array; +using ::testing::Each; +using ::testing::ElementsAre; +using ::testing::Eq; +using ::testing::PrintToString; +using ::testing::UnorderedElementsAre; + using SecretKey = std::array; struct KeyPair { @@ -19,72 +30,66 @@ struct KeyPair { explicit KeyPair(const Random *rng) { crypto_new_keypair(rng, pk.data(), sk.data()); } }; -template -std::array to_array(T const (&arr)[N]) +TEST(IdClosest, KeyIsClosestToItself) { - std::array stdarr; - std::copy(arr, arr + N, stdarr.begin()); - return stdarr; -} + Test_Random rng; -PublicKey random_pk(const Random *rng) -{ - PublicKey pk; - random_bytes(rng, pk.data(), pk.size()); - return pk; + PublicKey pk0 = random_pk(rng); + PublicKey pk1; + do { + // Get a random key that's not the same as pk0. + pk1 = random_pk(rng); + } while (pk0 == pk1); + + EXPECT_EQ(id_closest(pk0.data(), pk0.data(), pk1.data()), 1); } TEST(IdClosest, IdenticalKeysAreSameDistance) { - const Random *rng = system_random(); - ASSERT_NE(rng, nullptr); + Test_Random rng; PublicKey pk0 = random_pk(rng); PublicKey pk1 = random_pk(rng); - PublicKey pk2 = pk1; - EXPECT_EQ(id_closest(pk0.data(), pk1.data(), pk2.data()), 0); + EXPECT_EQ(id_closest(pk0.data(), pk1.data(), pk1.data()), 0); } TEST(IdClosest, DistanceIsCommutative) { - const Random *rng = system_random(); - ASSERT_NE(rng, nullptr); + Test_Random rng; - for (uint32_t i = 0; i < 100; ++i) { - PublicKey pk0 = random_pk(rng); - PublicKey pk1 = random_pk(rng); - PublicKey pk2 = random_pk(rng); + PublicKey pk0 = random_pk(rng); + PublicKey pk1 = random_pk(rng); + PublicKey pk2 = random_pk(rng); - ASSERT_NE(pk1, pk2); // RNG can't produce the same random key twice + ASSERT_NE(pk1, pk2); // RNG can't produce the same random key twice - // Two non-equal keys can't have the same distance from any given key. - EXPECT_NE(id_closest(pk0.data(), pk1.data(), pk2.data()), 0); + // Two non-equal keys can't have the same distance from any given key. + EXPECT_NE(id_closest(pk0.data(), pk1.data(), pk2.data()), 0); - if (id_closest(pk0.data(), pk1.data(), pk2.data()) == 1) { - EXPECT_EQ(id_closest(pk0.data(), pk2.data(), pk1.data()), 2); - } + if (id_closest(pk0.data(), pk1.data(), pk2.data()) == 1) { + EXPECT_EQ(id_closest(pk0.data(), pk2.data(), pk1.data()), 2); + } - if (id_closest(pk0.data(), pk1.data(), pk2.data()) == 2) { - EXPECT_EQ(id_closest(pk0.data(), pk2.data(), pk1.data()), 1); - } + if (id_closest(pk0.data(), pk1.data(), pk2.data()) == 2) { + EXPECT_EQ(id_closest(pk0.data(), pk2.data(), pk1.data()), 1); } } TEST(IdClosest, SmallXorDistanceIsCloser) { - PublicKey const pk0 = {{0xaa}}; - PublicKey const pk1 = {{0xa0}}; - PublicKey const pk2 = {{0x0a}}; + PublicKey const pk0 = {0xaa}; + PublicKey const pk1 = {0xa0}; + PublicKey const pk2 = {0x0a}; EXPECT_EQ(id_closest(pk0.data(), pk1.data(), pk2.data()), 1); } TEST(IdClosest, DistinctKeysCannotHaveTheSameDistance) { - PublicKey const pk0 = {{0x06}}; - PublicKey const pk1 = {{0x00}}; - PublicKey pk2 = {{0x00}}; + PublicKey const pk0 = {0x06}; + PublicKey const pk1 = {0x00}; + PublicKey pk2 = {0x00}; for (uint8_t i = 1; i < 0xff; ++i) { pk2[0] = i; @@ -94,14 +99,14 @@ TEST(IdClosest, DistinctKeysCannotHaveTheSameDistance) TEST(AddToList, OverridesKeysWithCloserKeys) { - PublicKey const self_pk = {{0xaa}}; + PublicKey const self_pk = {0xaa}; PublicKey const keys[] = { - {{0xa0}}, // closest - {{0x0a}}, // - {{0x0b}}, // - {{0x0c}}, // - {{0x0d}}, // - {{0xa1}}, // closer than the 4 keys above + {0xa0}, // closest + {0x0a}, // + {0x0b}, // + {0x0c}, // + {0x0d}, // + {0xa1}, // closer than the 4 keys above }; std::array nodes{}; @@ -128,10 +133,143 @@ TEST(AddToList, OverridesKeysWithCloserKeys) EXPECT_EQ(to_array(nodes[3].public_key), keys[2]); } +Node_format fill(Node_format v, PublicKey const &pk, IP_Port const &ip_port) +{ + std::copy(pk.begin(), pk.end(), v.public_key); + v.ip_port = ip_port; + return v; +} + +TEST(AddToList, AddsFirstKeysInOrder) +{ + Test_Random rng; + + // Make cmp_key the furthest away from 00000... as possible, so all initial inserts succeed. + PublicKey const cmp_pk{0xff, 0xff, 0xff, 0xff}; + + // Generate a bunch of other keys, sorted by distance from cmp_pk. + auto const keys + = sorted(array_of<20>(random_pk, rng), [&cmp_pk](auto const &pk1, auto const &pk2) { + return id_closest(cmp_pk.data(), pk1.data(), pk2.data()) == 1; + }); + auto const ips = array_of<20>(increasing_ip_port(0, rng)); + + std::vector nodes(4); + + // Add a bunch of nodes. + ASSERT_TRUE(add_to_list(nodes.data(), nodes.size(), keys[2].data(), &ips[2], cmp_pk.data())) + << "failed to insert\n" + << " cmp_pk = " << cmp_pk << "\n" + << " pk = " << keys[2] << "\n" + << " nodes_list = " << PrintToString(nodes); + ASSERT_TRUE(add_to_list(nodes.data(), nodes.size(), keys[5].data(), &ips[5], cmp_pk.data())) + << "failed to insert\n" + << " cmp_pk = " << cmp_pk << "\n" + << " pk = " << keys[5] << "\n" + << " nodes_list = " << PrintToString(nodes); + ASSERT_TRUE(add_to_list(nodes.data(), nodes.size(), keys[7].data(), &ips[7], cmp_pk.data())) + << "failed to insert\n" + << " cmp_pk = " << cmp_pk << "\n" + << " pk = " << keys[7] << "\n" + << " nodes_list = " << PrintToString(nodes); + ASSERT_TRUE(add_to_list(nodes.data(), nodes.size(), keys[9].data(), &ips[9], cmp_pk.data())) + << "failed to insert\n" + << " cmp_pk = " << cmp_pk << "\n" + << " pk = " << keys[9] << "\n" + << " nodes_list = " << PrintToString(nodes); + + // They should all appear in order. + EXPECT_THAT(nodes, + ElementsAre( // + fill(Node_format{}, keys[2], ips[2]), // + fill(Node_format{}, keys[5], ips[5]), // + fill(Node_format{}, keys[7], ips[7]), // + fill(Node_format{}, keys[9], ips[9]))); + + // Adding another node that's further away will not happen. + ASSERT_FALSE(add_to_list(nodes.data(), nodes.size(), keys[10].data(), &ips[10], cmp_pk.data())) + << "incorrectly inserted\n" + << " cmp_pk = " << cmp_pk << "\n" + << " pk = " << keys[10] << "\n" + << " nodes_list = " << PrintToString(nodes); + + // Now shuffle each time we add a node, which should work fine. + std::mt19937 mt_rng; + + // Adding one that's closer will happen. + std::shuffle(nodes.begin(), nodes.end(), mt_rng); + ASSERT_TRUE(add_to_list(nodes.data(), nodes.size(), keys[8].data(), &ips[8], cmp_pk.data())) + << "failed to insert\n" + << " cmp_pk = " << cmp_pk << "\n" + << " pk = " << keys[8] << "\n" + << " nodes_list = " << PrintToString(nodes); + + EXPECT_THAT(nodes, + UnorderedElementsAre( // + fill(Node_format{}, keys[2], ips[2]), // + fill(Node_format{}, keys[5], ips[5]), // + fill(Node_format{}, keys[7], ips[7]), // + fill(Node_format{}, keys[8], ips[8]))); + + // Adding one that's closer than almost all of them will happen. + std::shuffle(nodes.begin(), nodes.end(), mt_rng); + ASSERT_TRUE(add_to_list(nodes.data(), nodes.size(), keys[4].data(), &ips[4], cmp_pk.data())) + << "failed to insert\n" + << " cmp_pk = " << cmp_pk << "\n" + << " pk = " << keys[4] << "\n" + << " nodes_list = " << PrintToString(nodes); + + EXPECT_THAT(nodes, + UnorderedElementsAre( // + fill(Node_format{}, keys[2], ips[2]), // + fill(Node_format{}, keys[4], ips[4]), // + fill(Node_format{}, keys[5], ips[5]), // + fill(Node_format{}, keys[7], ips[7]))); + + // Adding one that's closer than all of them will happen. + std::shuffle(nodes.begin(), nodes.end(), mt_rng); + ASSERT_TRUE(add_to_list(nodes.data(), nodes.size(), keys[1].data(), &ips[1], cmp_pk.data())) + << "failed to insert\n" + << " cmp_pk = " << cmp_pk << "\n" + << " pk = " << keys[1] << "\n" + << " nodes_list = " << PrintToString(nodes); + + EXPECT_THAT(nodes, + UnorderedElementsAre( // + fill(Node_format{}, keys[1], ips[1]), // + fill(Node_format{}, keys[2], ips[2]), // + fill(Node_format{}, keys[4], ips[4]), // + fill(Node_format{}, keys[5], ips[5]))); +} + +TEST(AddToList, KeepsKeysInOrder) +{ + Test_Random rng; + + // Any random cmp_pk should work, as well as the smallest or (approximately) largest pk. + for (PublicKey const cmp_pk : {random_pk(rng), PublicKey{0x00}, PublicKey{0xff, 0xff}}) { + auto const by_distance = [&cmp_pk](auto const &node1, auto const &node2) { + return id_closest(cmp_pk.data(), node1.public_key, node2.public_key) == 1; + }; + + // Generate a bunch of other keys, not sorted. + auto const nodes = vector_of(16, random_node_format, rng); + + std::vector node_list(4); + + // Add all of them. + for (Node_format const &node : nodes) { + add_to_list( + node_list.data(), node_list.size(), node.public_key, &node.ip_port, cmp_pk.data()); + // Nodes should always be sorted. + EXPECT_THAT(node_list, Eq(sorted(node_list, by_distance))); + } + } +} + TEST(Request, CreateAndParse) { - const Random *rng = system_random(); - ASSERT_NE(rng, nullptr); + Test_Random rng; // Peers. const KeyPair sender(rng); @@ -187,7 +325,7 @@ TEST(Request, CreateAndParse) TEST(AnnounceNodes, SetAndTest) { - const Random *rng = system_random(); + Test_Random rng; const Network *ns = system_network(); const Memory *mem = system_memory(); @@ -195,40 +333,40 @@ TEST(AnnounceNodes, SetAndTest) ASSERT_NE(log, nullptr); Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr); ASSERT_NE(mono_time, nullptr); - Networking_Core *net = new_networking_no_udp(log, mem, ns); + Ptr net(new_networking_no_udp(log, mem, ns)); ASSERT_NE(net, nullptr); - DHT *dht = new_dht(log, mem, rng, ns, mono_time, net, true, true); + Ptr dht(new_dht(log, mem, rng, ns, mono_time, net.get(), true, true)); ASSERT_NE(dht, nullptr); uint8_t pk_data[CRYPTO_PUBLIC_KEY_SIZE]; - memcpy(pk_data, dht_get_self_public_key(dht), sizeof(pk_data)); - PublicKey self_pk = to_array(pk_data); + memcpy(pk_data, dht_get_self_public_key(dht.get()), sizeof(pk_data)); + PublicKey self_pk(to_array(pk_data)); PublicKey pk1 = random_pk(rng); ASSERT_NE(pk1, self_pk); // Test with maximally close key to self pk_data[CRYPTO_PUBLIC_KEY_SIZE - 1] = ~pk_data[CRYPTO_PUBLIC_KEY_SIZE - 1]; - PublicKey pk2 = to_array(pk_data); + PublicKey pk2(to_array(pk_data)); ASSERT_NE(pk2, pk1); IP_Port ip_port = {0}; ip_port.ip.family = net_family_ipv4(); - set_announce_node(dht, pk1.data()); - set_announce_node(dht, pk2.data()); + set_announce_node(dht.get(), pk1.data()); + set_announce_node(dht.get(), pk2.data()); - EXPECT_TRUE(addto_lists(dht, &ip_port, pk1.data())); - EXPECT_TRUE(addto_lists(dht, &ip_port, pk2.data())); + EXPECT_TRUE(addto_lists(dht.get(), &ip_port, pk1.data())); + EXPECT_TRUE(addto_lists(dht.get(), &ip_port, pk2.data())); Node_format nodes[MAX_SENT_NODES]; - EXPECT_EQ(0, get_close_nodes(dht, self_pk.data(), nodes, net_family_unspec(), true, true)); - set_announce_node(dht, pk1.data()); - set_announce_node(dht, pk2.data()); - EXPECT_EQ(2, get_close_nodes(dht, self_pk.data(), nodes, net_family_unspec(), true, true)); + EXPECT_EQ( + 0, get_close_nodes(dht.get(), self_pk.data(), nodes, net_family_unspec(), true, true)); + set_announce_node(dht.get(), pk1.data()); + set_announce_node(dht.get(), pk2.data()); + EXPECT_EQ( + 2, get_close_nodes(dht.get(), self_pk.data(), nodes, net_family_unspec(), true, true)); - kill_dht(dht); - kill_networking(net); mono_time_free(mem, mono_time); logger_kill(log); } diff --git a/external/toxcore/c-toxcore/toxcore/DHT_test_util.cc b/external/toxcore/c-toxcore/toxcore/DHT_test_util.cc new file mode 100644 index 00000000..15e679f2 --- /dev/null +++ b/external/toxcore/c-toxcore/toxcore/DHT_test_util.cc @@ -0,0 +1,29 @@ +#include "DHT_test_util.hh" + +#include +#include + +#include "crypto_core_test_util.hh" +#include "network_test_util.hh" + +Node_format random_node_format(const Random *rng) +{ + Node_format node; + auto const pk = random_pk(rng); + std::copy(pk.begin(), pk.end(), node.public_key); + node.ip_port = random_ip_port(rng); + return node; +} + +bool operator==(Node_format const &a, Node_format const &b) +{ + return std::memcmp(a.public_key, b.public_key, sizeof(a.public_key)) == 0 + && a.ip_port == b.ip_port; +} + +std::ostream &operator<<(std::ostream &out, Node_format const &v) +{ + return out << "\n Node_format{\n" + << " public_key = " << PublicKey(v.public_key) << ",\n" + << " ip_port = " << v.ip_port << " }"; +} diff --git a/external/toxcore/c-toxcore/toxcore/DHT_test_util.hh b/external/toxcore/c-toxcore/toxcore/DHT_test_util.hh new file mode 100644 index 00000000..5fa75b7f --- /dev/null +++ b/external/toxcore/c-toxcore/toxcore/DHT_test_util.hh @@ -0,0 +1,18 @@ +#ifndef C_TOXCORE_TOXCORE_DHT_TEST_UTIL_H +#define C_TOXCORE_TOXCORE_DHT_TEST_UTIL_H + +#include + +#include "DHT.h" +#include "test_util.hh" + +template <> +struct Deleter : Function_Deleter { }; + +bool operator==(Node_format const &a, Node_format const &b); + +std::ostream &operator<<(std::ostream &out, Node_format const &v); + +Node_format random_node_format(const Random *rng); + +#endif // C_TOXCORE_TOXCORE_DHT_TEST_UTIL_H diff --git a/external/toxcore/c-toxcore/toxcore/LAN_discovery.c b/external/toxcore/c-toxcore/toxcore/LAN_discovery.c index db995520..ce34551d 100644 --- a/external/toxcore/c-toxcore/toxcore/LAN_discovery.c +++ b/external/toxcore/c-toxcore/toxcore/LAN_discovery.c @@ -62,35 +62,38 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns) return nullptr; } - IP_ADAPTER_INFO *pAdapterInfo = (IP_ADAPTER_INFO *)malloc(sizeof(IP_ADAPTER_INFO)); - unsigned long ulOutBufLen = sizeof(IP_ADAPTER_INFO); + IP_ADAPTER_INFO *adapter_info = (IP_ADAPTER_INFO *)malloc(sizeof(IP_ADAPTER_INFO)); - if (pAdapterInfo == nullptr) { + if (adapter_info == nullptr) { free(broadcast); return nullptr; } - if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) { - free(pAdapterInfo); - pAdapterInfo = (IP_ADAPTER_INFO *)malloc(ulOutBufLen); + unsigned long out_buf_len = sizeof(IP_ADAPTER_INFO); - if (pAdapterInfo == nullptr) { + 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); + + if (new_adapter_info == nullptr) { free(broadcast); return nullptr; } + + adapter_info = new_adapter_info; } - const int ret = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen); + const int ret = GetAdaptersInfo(adapter_info, &out_buf_len); if (ret == NO_ERROR) { - IP_ADAPTER_INFO *pAdapter = pAdapterInfo; + IP_ADAPTER_INFO *adapter = adapter_info; - while (pAdapter != nullptr) { + while (adapter != nullptr) { IP gateway = {0}; IP subnet_mask = {0}; - if (addr_parse_ip(pAdapter->IpAddressList.IpMask.String, &subnet_mask) - && addr_parse_ip(pAdapter->GatewayList.IpAddress.String, &gateway)) { + if (addr_parse_ip(adapter->IpAddressList.IpMask.String, &subnet_mask) + && addr_parse_ip(adapter->GatewayList.IpAddress.String, &gateway)) { if (net_family_is_ipv4(gateway.family) && net_family_is_ipv4(subnet_mask.family)) { IP *ip = &broadcast->ips[broadcast->count]; ip->family = net_family_ipv4(); @@ -106,12 +109,12 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns) } } - pAdapter = pAdapter->Next; + adapter = adapter->Next; } } - if (pAdapterInfo != nullptr) { - free(pAdapterInfo); + if (adapter_info != nullptr) { + free(adapter_info); } return broadcast; diff --git a/external/toxcore/c-toxcore/toxcore/TCP_common.c b/external/toxcore/c-toxcore/toxcore/TCP_common.c index 157ec144..13c17da9 100644 --- a/external/toxcore/c-toxcore/toxcore/TCP_common.c +++ b/external/toxcore/c-toxcore/toxcore/TCP_common.c @@ -105,17 +105,19 @@ static bool add_priority(TCP_Connection *con, const uint8_t *packet, uint16_t si return false; } - new_list->next = nullptr; - new_list->size = size; - new_list->sent = sent; - new_list->data = (uint8_t *)mem_balloc(con->mem, size); + uint8_t *data = (uint8_t *)mem_balloc(con->mem, size); - if (new_list->data == nullptr) { + if (data == nullptr) { mem_delete(con->mem, new_list); return false; } - memcpy(new_list->data, packet, size); + memcpy(data, packet, size); + new_list->data = data; + new_list->size = size; + + new_list->next = nullptr; + new_list->sent = sent; if (p != nullptr) { p->next = new_list; diff --git a/external/toxcore/c-toxcore/toxcore/TCP_connection.c b/external/toxcore/c-toxcore/toxcore/TCP_connection.c index 8e54c2ab..8e379442 100644 --- a/external/toxcore/c-toxcore/toxcore/TCP_connection.c +++ b/external/toxcore/c-toxcore/toxcore/TCP_connection.c @@ -290,8 +290,9 @@ static TCP_con *get_tcp_connection(const TCP_Connections *tcp_c, int tcp_connect uint32_t tcp_connected_relays_count(const TCP_Connections *tcp_c) { uint32_t count = 0; + const uint32_t size = tcp_connections_count(tcp_c); - for (uint32_t i = 0; i < tcp_connections_count(tcp_c); ++i) { + for (uint32_t i = 0; i < size; ++i) { const TCP_con *tcp_con = get_tcp_connection(tcp_c, i); if (tcp_con == nullptr) { diff --git a/external/toxcore/c-toxcore/toxcore/TCP_server.c b/external/toxcore/c-toxcore/toxcore/TCP_server.c index 9809da13..1a3c7aff 100644 --- a/external/toxcore/c-toxcore/toxcore/TCP_server.c +++ b/external/toxcore/c-toxcore/toxcore/TCP_server.c @@ -92,6 +92,9 @@ struct TCP_Server { BS_List accepted_key_list; }; +static_assert(sizeof(TCP_Server) < 7 * 1024 * 1024, + "TCP_Server struct should not grow more; it's already 6MB"); + const uint8_t *tcp_server_public_key(const TCP_Server *tcp_server) { return tcp_server->public_key; @@ -968,20 +971,22 @@ TCP_Server *new_tcp_server(const Logger *logger, const Memory *mem, const Random temp->ns = ns; temp->rng = rng; - temp->socks_listening = (Socket *)mem_valloc(mem, num_sockets, sizeof(Socket)); + Socket *socks_listening = (Socket *)mem_valloc(mem, num_sockets, sizeof(Socket)); - if (temp->socks_listening == nullptr) { + if (socks_listening == nullptr) { LOGGER_ERROR(logger, "socket allocation failed"); mem_delete(mem, temp); return nullptr; } + temp->socks_listening = socks_listening; + #ifdef TCP_SERVER_USE_EPOLL temp->efd = epoll_create(8); if (temp->efd == -1) { LOGGER_ERROR(logger, "epoll initialisation failed"); - mem_delete(mem, temp->socks_listening); + mem_delete(mem, socks_listening); mem_delete(mem, temp); return nullptr; } diff --git a/external/toxcore/c-toxcore/toxcore/announce.c b/external/toxcore/c-toxcore/toxcore/announce.c index 19f15f41..26645b55 100644 --- a/external/toxcore/c-toxcore/toxcore/announce.c +++ b/external/toxcore/c-toxcore/toxcore/announce.c @@ -243,13 +243,15 @@ bool announce_store_data(Announcements *announce, const uint8_t *data_public_key free(entry->data); } - entry->data = (uint8_t *)malloc(length); + uint8_t *entry_data = (uint8_t *)malloc(length); - if (entry->data == nullptr) { + if (entry_data == nullptr) { + entry->data = nullptr; // TODO(iphydf): Is this necessary? return false; } - memcpy(entry->data, data, length); + memcpy(entry_data, data, length); + entry->data = entry_data; } entry->length = length; diff --git a/external/toxcore/c-toxcore/toxcore/crypto_core_test.cc b/external/toxcore/c-toxcore/toxcore/crypto_core_test.cc index c3c8a6f9..19802275 100644 --- a/external/toxcore/c-toxcore/toxcore/crypto_core_test.cc +++ b/external/toxcore/c-toxcore/toxcore/crypto_core_test.cc @@ -6,13 +6,13 @@ #include #include +#include "crypto_core_test_util.hh" #include "util.h" namespace { using HmacKey = std::array; using Hmac = std::array; -using PublicKey = std::array; using SecretKey = std::array; using ExtPublicKey = std::array; using ExtSecretKey = std::array; @@ -21,8 +21,7 @@ using Nonce = std::array; TEST(CryptoCore, EncryptLargeData) { - const Random *rng = system_random(); - ASSERT_NE(rng, nullptr); + Test_Random rng; Nonce nonce{}; PublicKey pk; @@ -71,8 +70,7 @@ TEST(CryptoCore, IncrementNonceNumber) TEST(CryptoCore, Signatures) { - const Random *rng = system_random(); - ASSERT_NE(rng, nullptr); + Test_Random rng; ExtPublicKey pk; ExtSecretKey sk; @@ -96,8 +94,7 @@ TEST(CryptoCore, Signatures) TEST(CryptoCore, Hmac) { - const Random *rng = system_random(); - ASSERT_NE(rng, nullptr); + Test_Random rng; HmacKey sk; new_hmac_key(rng, sk.data()); diff --git a/external/toxcore/c-toxcore/toxcore/crypto_core_test_util.cc b/external/toxcore/c-toxcore/toxcore/crypto_core_test_util.cc new file mode 100644 index 00000000..4d5f46c5 --- /dev/null +++ b/external/toxcore/c-toxcore/toxcore/crypto_core_test_util.cc @@ -0,0 +1,39 @@ +#include "crypto_core_test_util.hh" + +#include +#include + +Random_Funcs const Random_Class::vtable = { + Method::invoke<&Random_Class::random_bytes>, + Method::invoke<&Random_Class::random_uniform>, +}; + +Random_Class::~Random_Class() = default; + +void Test_Random::random_bytes(void *obj, uint8_t *bytes, size_t length) +{ + std::generate(bytes, &bytes[length], std::ref(lcg)); +} + +uint32_t Test_Random::random_uniform(void *obj, uint32_t upper_bound) +{ + std::uniform_int_distribution distrib(0, upper_bound); + return distrib(lcg); +} + +PublicKey random_pk(const Random *rng) +{ + PublicKey pk; + random_bytes(rng, pk.data(), pk.size()); + return pk; +} + +std::ostream &operator<<(std::ostream &out, PublicKey const &pk) +{ + out << '"'; + for (uint8_t byte : pk) { + out << std::setw(2) << std::setfill('0') << std::hex << uint32_t(byte); + } + out << '"'; + return out; +} diff --git a/external/toxcore/c-toxcore/toxcore/crypto_core_test_util.hh b/external/toxcore/c-toxcore/toxcore/crypto_core_test_util.hh new file mode 100644 index 00000000..91a9d68e --- /dev/null +++ b/external/toxcore/c-toxcore/toxcore/crypto_core_test_util.hh @@ -0,0 +1,88 @@ +#ifndef C_TOXCORE_TOXCORE_CRYPTO_CORE_TEST_UTIL_H +#define C_TOXCORE_TOXCORE_CRYPTO_CORE_TEST_UTIL_H + +#include +#include +#include +#include + +#include "crypto_core.h" +#include "test_util.hh" + +struct Random_Class { + static Random_Funcs const vtable; + Random const self; + + operator Random const *() const { return &self; } + + Random_Class(Random_Class const &) = default; + Random_Class() + : self{&vtable, this} + { + } + + virtual ~Random_Class(); + virtual crypto_random_bytes_cb random_bytes = 0; + virtual crypto_random_uniform_cb random_uniform = 0; +}; + +/** + * A very simple, fast, and deterministic PRNG just for testing. + * + * We generally don't want to use system_random(), since it's a + * cryptographically secure PRNG and we don't need that in unit tests. + */ +class Test_Random : public Random_Class { + std::minstd_rand lcg; + + void random_bytes(void *obj, uint8_t *bytes, size_t length) override; + uint32_t random_uniform(void *obj, uint32_t upper_bound) override; +}; + +struct PublicKey : private std::array { + using Base = std::array; + + using Base::begin; + using Base::data; + using Base::end; + using Base::size; + using Base::operator[]; + + PublicKey() = default; + explicit PublicKey(uint8_t const (&arr)[CRYPTO_PUBLIC_KEY_SIZE]) + : PublicKey(to_array(arr)) + { + } + explicit PublicKey(std::array const &arr) + { + std::copy(arr.begin(), arr.end(), begin()); + } + + PublicKey(std::initializer_list const &arr) + { + std::copy(arr.begin(), arr.end(), begin()); + } + + Base const &base() const { return *this; } +}; + +inline bool operator!=(PublicKey const &pk1, PublicKey const &pk2) +{ + return pk1.base() != pk2.base(); +} + +inline bool operator==(PublicKey const &pk1, PublicKey const &pk2) +{ + return pk1.base() == pk2.base(); +} + +inline bool operator==(PublicKey::Base const &pk1, PublicKey const &pk2) +{ + return pk1 == pk2.base(); +} + +std::ostream &operator<<(std::ostream &out, PublicKey const &pk); + +PublicKey random_pk(const Random *rng); + +#endif // C_TOXCORE_TOXCORE_CRYPTO_CORE_TEST_UTIL_H diff --git a/external/toxcore/c-toxcore/toxcore/events/conference_connected.c b/external/toxcore/c-toxcore/toxcore/events/conference_connected.c index 8fd64541..39169abb 100644 --- a/external/toxcore/c-toxcore/toxcore/events/conference_connected.c +++ b/external/toxcore/c-toxcore/toxcore/events/conference_connected.c @@ -118,42 +118,6 @@ static Tox_Event_Conference_Connected *tox_events_add_conference_connected(Tox_E return conference_connected; } -const Tox_Event_Conference_Connected *tox_events_get_conference_connected(const Tox_Events *events, uint32_t index) -{ - uint32_t conference_connected_index = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (conference_connected_index > index) { - return nullptr; - } - - if (events->events[i].type == TOX_EVENT_CONFERENCE_CONNECTED) { - const Tox_Event_Conference_Connected *conference_connected = events->events[i].data.conference_connected; - if (conference_connected_index == index) { - return conference_connected; - } - ++conference_connected_index; - } - } - - return nullptr; -} - -uint32_t tox_events_get_conference_connected_size(const Tox_Events *events) -{ - uint32_t conference_connected_size = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (events->events[i].type == TOX_EVENT_CONFERENCE_CONNECTED) { - ++conference_connected_size; - } - } - - return conference_connected_size; -} - bool tox_event_conference_connected_unpack( Tox_Event_Conference_Connected **event, Bin_Unpack *bu, const Memory *mem) { diff --git a/external/toxcore/c-toxcore/toxcore/events/conference_invite.c b/external/toxcore/c-toxcore/toxcore/events/conference_invite.c index e62b01dd..e48b0619 100644 --- a/external/toxcore/c-toxcore/toxcore/events/conference_invite.c +++ b/external/toxcore/c-toxcore/toxcore/events/conference_invite.c @@ -69,13 +69,14 @@ static bool tox_event_conference_invite_set_cookie(Tox_Event_Conference_Invite * conference_invite->cookie_length = 0; } - conference_invite->cookie = (uint8_t *)malloc(cookie_length); + uint8_t *cookie_copy = (uint8_t *)malloc(cookie_length); - if (conference_invite->cookie == nullptr) { + if (cookie_copy == nullptr) { return false; } - memcpy(conference_invite->cookie, cookie, cookie_length); + memcpy(cookie_copy, cookie, cookie_length); + conference_invite->cookie = cookie_copy; conference_invite->cookie_length = cookie_length; return true; } @@ -179,42 +180,6 @@ static Tox_Event_Conference_Invite *tox_events_add_conference_invite(Tox_Events return conference_invite; } -const Tox_Event_Conference_Invite *tox_events_get_conference_invite(const Tox_Events *events, uint32_t index) -{ - uint32_t conference_invite_index = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (conference_invite_index > index) { - return nullptr; - } - - if (events->events[i].type == TOX_EVENT_CONFERENCE_INVITE) { - const Tox_Event_Conference_Invite *conference_invite = events->events[i].data.conference_invite; - if (conference_invite_index == index) { - return conference_invite; - } - ++conference_invite_index; - } - } - - return nullptr; -} - -uint32_t tox_events_get_conference_invite_size(const Tox_Events *events) -{ - uint32_t conference_invite_size = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (events->events[i].type == TOX_EVENT_CONFERENCE_INVITE) { - ++conference_invite_size; - } - } - - return conference_invite_size; -} - bool tox_event_conference_invite_unpack( Tox_Event_Conference_Invite **event, Bin_Unpack *bu, const Memory *mem) { diff --git a/external/toxcore/c-toxcore/toxcore/events/conference_message.c b/external/toxcore/c-toxcore/toxcore/events/conference_message.c index 2d32166b..4939af7f 100644 --- a/external/toxcore/c-toxcore/toxcore/events/conference_message.c +++ b/external/toxcore/c-toxcore/toxcore/events/conference_message.c @@ -83,13 +83,14 @@ static bool tox_event_conference_message_set_message(Tox_Event_Conference_Messag conference_message->message_length = 0; } - conference_message->message = (uint8_t *)malloc(message_length); + uint8_t *message_copy = (uint8_t *)malloc(message_length); - if (conference_message->message == nullptr) { + if (message_copy == nullptr) { return false; } - memcpy(conference_message->message, message, message_length); + memcpy(message_copy, message, message_length); + conference_message->message = message_copy; conference_message->message_length = message_length; return true; } @@ -195,42 +196,6 @@ static Tox_Event_Conference_Message *tox_events_add_conference_message(Tox_Event return conference_message; } -const Tox_Event_Conference_Message *tox_events_get_conference_message(const Tox_Events *events, uint32_t index) -{ - uint32_t conference_message_index = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (conference_message_index > index) { - return nullptr; - } - - if (events->events[i].type == TOX_EVENT_CONFERENCE_MESSAGE) { - const Tox_Event_Conference_Message *conference_message = events->events[i].data.conference_message; - if (conference_message_index == index) { - return conference_message; - } - ++conference_message_index; - } - } - - return nullptr; -} - -uint32_t tox_events_get_conference_message_size(const Tox_Events *events) -{ - uint32_t conference_message_size = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (events->events[i].type == TOX_EVENT_CONFERENCE_MESSAGE) { - ++conference_message_size; - } - } - - return conference_message_size; -} - bool tox_event_conference_message_unpack( Tox_Event_Conference_Message **event, Bin_Unpack *bu, const Memory *mem) { diff --git a/external/toxcore/c-toxcore/toxcore/events/conference_peer_list_changed.c b/external/toxcore/c-toxcore/toxcore/events/conference_peer_list_changed.c index 1bcb685d..9c9340c6 100644 --- a/external/toxcore/c-toxcore/toxcore/events/conference_peer_list_changed.c +++ b/external/toxcore/c-toxcore/toxcore/events/conference_peer_list_changed.c @@ -118,42 +118,6 @@ static Tox_Event_Conference_Peer_List_Changed *tox_events_add_conference_peer_li return conference_peer_list_changed; } -const Tox_Event_Conference_Peer_List_Changed *tox_events_get_conference_peer_list_changed(const Tox_Events *events, uint32_t index) -{ - uint32_t conference_peer_list_changed_index = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (conference_peer_list_changed_index > index) { - return nullptr; - } - - if (events->events[i].type == TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED) { - const Tox_Event_Conference_Peer_List_Changed *conference_peer_list_changed = events->events[i].data.conference_peer_list_changed; - if (conference_peer_list_changed_index == index) { - return conference_peer_list_changed; - } - ++conference_peer_list_changed_index; - } - } - - return nullptr; -} - -uint32_t tox_events_get_conference_peer_list_changed_size(const Tox_Events *events) -{ - uint32_t conference_peer_list_changed_size = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (events->events[i].type == TOX_EVENT_CONFERENCE_PEER_LIST_CHANGED) { - ++conference_peer_list_changed_size; - } - } - - return conference_peer_list_changed_size; -} - bool tox_event_conference_peer_list_changed_unpack( Tox_Event_Conference_Peer_List_Changed **event, Bin_Unpack *bu, const Memory *mem) { diff --git a/external/toxcore/c-toxcore/toxcore/events/conference_peer_name.c b/external/toxcore/c-toxcore/toxcore/events/conference_peer_name.c index a9be07fc..ad6b9e18 100644 --- a/external/toxcore/c-toxcore/toxcore/events/conference_peer_name.c +++ b/external/toxcore/c-toxcore/toxcore/events/conference_peer_name.c @@ -68,13 +68,14 @@ static bool tox_event_conference_peer_name_set_name(Tox_Event_Conference_Peer_Na conference_peer_name->name_length = 0; } - conference_peer_name->name = (uint8_t *)malloc(name_length); + uint8_t *name_copy = (uint8_t *)malloc(name_length); - if (conference_peer_name->name == nullptr) { + if (name_copy == nullptr) { return false; } - memcpy(conference_peer_name->name, name, name_length); + memcpy(name_copy, name, name_length); + conference_peer_name->name = name_copy; conference_peer_name->name_length = name_length; return true; } @@ -178,42 +179,6 @@ static Tox_Event_Conference_Peer_Name *tox_events_add_conference_peer_name(Tox_E return conference_peer_name; } -const Tox_Event_Conference_Peer_Name *tox_events_get_conference_peer_name(const Tox_Events *events, uint32_t index) -{ - uint32_t conference_peer_name_index = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (conference_peer_name_index > index) { - return nullptr; - } - - if (events->events[i].type == TOX_EVENT_CONFERENCE_PEER_NAME) { - const Tox_Event_Conference_Peer_Name *conference_peer_name = events->events[i].data.conference_peer_name; - if (conference_peer_name_index == index) { - return conference_peer_name; - } - ++conference_peer_name_index; - } - } - - return nullptr; -} - -uint32_t tox_events_get_conference_peer_name_size(const Tox_Events *events) -{ - uint32_t conference_peer_name_size = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (events->events[i].type == TOX_EVENT_CONFERENCE_PEER_NAME) { - ++conference_peer_name_size; - } - } - - return conference_peer_name_size; -} - bool tox_event_conference_peer_name_unpack( Tox_Event_Conference_Peer_Name **event, Bin_Unpack *bu, const Memory *mem) { diff --git a/external/toxcore/c-toxcore/toxcore/events/conference_title.c b/external/toxcore/c-toxcore/toxcore/events/conference_title.c index 9bec8cf6..1532ba5d 100644 --- a/external/toxcore/c-toxcore/toxcore/events/conference_title.c +++ b/external/toxcore/c-toxcore/toxcore/events/conference_title.c @@ -68,13 +68,14 @@ static bool tox_event_conference_title_set_title(Tox_Event_Conference_Title *con conference_title->title_length = 0; } - conference_title->title = (uint8_t *)malloc(title_length); + uint8_t *title_copy = (uint8_t *)malloc(title_length); - if (conference_title->title == nullptr) { + if (title_copy == nullptr) { return false; } - memcpy(conference_title->title, title, title_length); + memcpy(title_copy, title, title_length); + conference_title->title = title_copy; conference_title->title_length = title_length; return true; } @@ -178,42 +179,6 @@ static Tox_Event_Conference_Title *tox_events_add_conference_title(Tox_Events *e return conference_title; } -const Tox_Event_Conference_Title *tox_events_get_conference_title(const Tox_Events *events, uint32_t index) -{ - uint32_t conference_title_index = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (conference_title_index > index) { - return nullptr; - } - - if (events->events[i].type == TOX_EVENT_CONFERENCE_TITLE) { - const Tox_Event_Conference_Title *conference_title = events->events[i].data.conference_title; - if (conference_title_index == index) { - return conference_title; - } - ++conference_title_index; - } - } - - return nullptr; -} - -uint32_t tox_events_get_conference_title_size(const Tox_Events *events) -{ - uint32_t conference_title_size = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (events->events[i].type == TOX_EVENT_CONFERENCE_TITLE) { - ++conference_title_size; - } - } - - return conference_title_size; -} - bool tox_event_conference_title_unpack( Tox_Event_Conference_Title **event, Bin_Unpack *bu, const Memory *mem) { diff --git a/external/toxcore/c-toxcore/toxcore/events/events_alloc.c b/external/toxcore/c-toxcore/toxcore/events/events_alloc.c index 69d5cc31..271dfc00 100644 --- a/external/toxcore/c-toxcore/toxcore/events/events_alloc.c +++ b/external/toxcore/c-toxcore/toxcore/events/events_alloc.c @@ -22,18 +22,21 @@ Tox_Events_State *tox_events_alloc(void *user_data) return state; } - state->events = (Tox_Events *)mem_alloc(state->mem, sizeof(Tox_Events)); + Tox_Events *events = (Tox_Events *)mem_alloc(state->mem, sizeof(Tox_Events)); - if (state->events == nullptr) { + if (events == nullptr) { // It's still null => allocation failed. + state->events = nullptr; state->error = TOX_ERR_EVENTS_ITERATE_MALLOC; - } else { - *state->events = (Tox_Events) { - nullptr - }; - state->events->mem = state->mem; + return state; } + *events = (Tox_Events) { + nullptr + }; + state->events = events; + state->events->mem = state->mem; + return state; } diff --git a/external/toxcore/c-toxcore/toxcore/events/file_chunk_request.c b/external/toxcore/c-toxcore/toxcore/events/file_chunk_request.c index ae8269c6..0bf58642 100644 --- a/external/toxcore/c-toxcore/toxcore/events/file_chunk_request.c +++ b/external/toxcore/c-toxcore/toxcore/events/file_chunk_request.c @@ -171,42 +171,6 @@ static Tox_Event_File_Chunk_Request *tox_events_add_file_chunk_request(Tox_Event return file_chunk_request; } -const Tox_Event_File_Chunk_Request *tox_events_get_file_chunk_request(const Tox_Events *events, uint32_t index) -{ - uint32_t file_chunk_request_index = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (file_chunk_request_index > index) { - return nullptr; - } - - if (events->events[i].type == TOX_EVENT_FILE_CHUNK_REQUEST) { - const Tox_Event_File_Chunk_Request *file_chunk_request = events->events[i].data.file_chunk_request; - if (file_chunk_request_index == index) { - return file_chunk_request; - } - ++file_chunk_request_index; - } - } - - return nullptr; -} - -uint32_t tox_events_get_file_chunk_request_size(const Tox_Events *events) -{ - uint32_t file_chunk_request_size = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (events->events[i].type == TOX_EVENT_FILE_CHUNK_REQUEST) { - ++file_chunk_request_size; - } - } - - return file_chunk_request_size; -} - bool tox_event_file_chunk_request_unpack( Tox_Event_File_Chunk_Request **event, Bin_Unpack *bu, const Memory *mem) { diff --git a/external/toxcore/c-toxcore/toxcore/events/file_recv.c b/external/toxcore/c-toxcore/toxcore/events/file_recv.c index 238550e4..25ea7039 100644 --- a/external/toxcore/c-toxcore/toxcore/events/file_recv.c +++ b/external/toxcore/c-toxcore/toxcore/events/file_recv.c @@ -96,13 +96,14 @@ static bool tox_event_file_recv_set_filename(Tox_Event_File_Recv *file_recv, file_recv->filename_length = 0; } - file_recv->filename = (uint8_t *)malloc(filename_length); + uint8_t *filename_copy = (uint8_t *)malloc(filename_length); - if (file_recv->filename == nullptr) { + if (filename_copy == nullptr) { return false; } - memcpy(file_recv->filename, filename, filename_length); + memcpy(filename_copy, filename, filename_length); + file_recv->filename = filename_copy; file_recv->filename_length = filename_length; return true; } @@ -210,42 +211,6 @@ static Tox_Event_File_Recv *tox_events_add_file_recv(Tox_Events *events, const M return file_recv; } -const Tox_Event_File_Recv *tox_events_get_file_recv(const Tox_Events *events, uint32_t index) -{ - uint32_t file_recv_index = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (file_recv_index > index) { - return nullptr; - } - - if (events->events[i].type == TOX_EVENT_FILE_RECV) { - const Tox_Event_File_Recv *file_recv = events->events[i].data.file_recv; - if (file_recv_index == index) { - return file_recv; - } - ++file_recv_index; - } - } - - return nullptr; -} - -uint32_t tox_events_get_file_recv_size(const Tox_Events *events) -{ - uint32_t file_recv_size = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (events->events[i].type == TOX_EVENT_FILE_RECV) { - ++file_recv_size; - } - } - - return file_recv_size; -} - bool tox_event_file_recv_unpack( Tox_Event_File_Recv **event, Bin_Unpack *bu, const Memory *mem) { diff --git a/external/toxcore/c-toxcore/toxcore/events/file_recv_chunk.c b/external/toxcore/c-toxcore/toxcore/events/file_recv_chunk.c index 295c37ce..eda5818a 100644 --- a/external/toxcore/c-toxcore/toxcore/events/file_recv_chunk.c +++ b/external/toxcore/c-toxcore/toxcore/events/file_recv_chunk.c @@ -82,13 +82,14 @@ static bool tox_event_file_recv_chunk_set_data(Tox_Event_File_Recv_Chunk *file_r file_recv_chunk->data_length = 0; } - file_recv_chunk->data = (uint8_t *)malloc(data_length); + uint8_t *data_copy = (uint8_t *)malloc(data_length); - if (file_recv_chunk->data == nullptr) { + if (data_copy == nullptr) { return false; } - memcpy(file_recv_chunk->data, data, data_length); + memcpy(data_copy, data, data_length); + file_recv_chunk->data = data_copy; file_recv_chunk->data_length = data_length; return true; } @@ -194,42 +195,6 @@ static Tox_Event_File_Recv_Chunk *tox_events_add_file_recv_chunk(Tox_Events *eve return file_recv_chunk; } -const Tox_Event_File_Recv_Chunk *tox_events_get_file_recv_chunk(const Tox_Events *events, uint32_t index) -{ - uint32_t file_recv_chunk_index = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (file_recv_chunk_index > index) { - return nullptr; - } - - if (events->events[i].type == TOX_EVENT_FILE_RECV_CHUNK) { - const Tox_Event_File_Recv_Chunk *file_recv_chunk = events->events[i].data.file_recv_chunk; - if (file_recv_chunk_index == index) { - return file_recv_chunk; - } - ++file_recv_chunk_index; - } - } - - return nullptr; -} - -uint32_t tox_events_get_file_recv_chunk_size(const Tox_Events *events) -{ - uint32_t file_recv_chunk_size = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (events->events[i].type == TOX_EVENT_FILE_RECV_CHUNK) { - ++file_recv_chunk_size; - } - } - - return file_recv_chunk_size; -} - bool tox_event_file_recv_chunk_unpack( Tox_Event_File_Recv_Chunk **event, Bin_Unpack *bu, const Memory *mem) { diff --git a/external/toxcore/c-toxcore/toxcore/events/file_recv_control.c b/external/toxcore/c-toxcore/toxcore/events/file_recv_control.c index bfff9a05..e9aac24d 100644 --- a/external/toxcore/c-toxcore/toxcore/events/file_recv_control.c +++ b/external/toxcore/c-toxcore/toxcore/events/file_recv_control.c @@ -156,42 +156,6 @@ static Tox_Event_File_Recv_Control *tox_events_add_file_recv_control(Tox_Events return file_recv_control; } -const Tox_Event_File_Recv_Control *tox_events_get_file_recv_control(const Tox_Events *events, uint32_t index) -{ - uint32_t file_recv_control_index = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (file_recv_control_index > index) { - return nullptr; - } - - if (events->events[i].type == TOX_EVENT_FILE_RECV_CONTROL) { - const Tox_Event_File_Recv_Control *file_recv_control = events->events[i].data.file_recv_control; - if (file_recv_control_index == index) { - return file_recv_control; - } - ++file_recv_control_index; - } - } - - return nullptr; -} - -uint32_t tox_events_get_file_recv_control_size(const Tox_Events *events) -{ - uint32_t file_recv_control_size = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (events->events[i].type == TOX_EVENT_FILE_RECV_CONTROL) { - ++file_recv_control_size; - } - } - - return file_recv_control_size; -} - bool tox_event_file_recv_control_unpack( Tox_Event_File_Recv_Control **event, Bin_Unpack *bu, const Memory *mem) { diff --git a/external/toxcore/c-toxcore/toxcore/events/friend_connection_status.c b/external/toxcore/c-toxcore/toxcore/events/friend_connection_status.c index a84ac25e..9eded8b5 100644 --- a/external/toxcore/c-toxcore/toxcore/events/friend_connection_status.c +++ b/external/toxcore/c-toxcore/toxcore/events/friend_connection_status.c @@ -140,42 +140,6 @@ static Tox_Event_Friend_Connection_Status *tox_events_add_friend_connection_stat return friend_connection_status; } -const Tox_Event_Friend_Connection_Status *tox_events_get_friend_connection_status(const Tox_Events *events, uint32_t index) -{ - uint32_t friend_connection_status_index = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (friend_connection_status_index > index) { - return nullptr; - } - - if (events->events[i].type == TOX_EVENT_FRIEND_CONNECTION_STATUS) { - const Tox_Event_Friend_Connection_Status *friend_connection_status = events->events[i].data.friend_connection_status; - if (friend_connection_status_index == index) { - return friend_connection_status; - } - ++friend_connection_status_index; - } - } - - return nullptr; -} - -uint32_t tox_events_get_friend_connection_status_size(const Tox_Events *events) -{ - uint32_t friend_connection_status_size = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (events->events[i].type == TOX_EVENT_FRIEND_CONNECTION_STATUS) { - ++friend_connection_status_size; - } - } - - return friend_connection_status_size; -} - bool tox_event_friend_connection_status_unpack( Tox_Event_Friend_Connection_Status **event, Bin_Unpack *bu, const Memory *mem) { diff --git a/external/toxcore/c-toxcore/toxcore/events/friend_lossless_packet.c b/external/toxcore/c-toxcore/toxcore/events/friend_lossless_packet.c index 1005df06..2d5da3bf 100644 --- a/external/toxcore/c-toxcore/toxcore/events/friend_lossless_packet.c +++ b/external/toxcore/c-toxcore/toxcore/events/friend_lossless_packet.c @@ -54,13 +54,14 @@ static bool tox_event_friend_lossless_packet_set_data(Tox_Event_Friend_Lossless_ friend_lossless_packet->data_length = 0; } - friend_lossless_packet->data = (uint8_t *)malloc(data_length); + uint8_t *data_copy = (uint8_t *)malloc(data_length); - if (friend_lossless_packet->data == nullptr) { + if (data_copy == nullptr) { return false; } - memcpy(friend_lossless_packet->data, data, data_length); + memcpy(data_copy, data, data_length); + friend_lossless_packet->data = data_copy; friend_lossless_packet->data_length = data_length; return true; } @@ -162,42 +163,6 @@ static Tox_Event_Friend_Lossless_Packet *tox_events_add_friend_lossless_packet(T return friend_lossless_packet; } -const Tox_Event_Friend_Lossless_Packet *tox_events_get_friend_lossless_packet(const Tox_Events *events, uint32_t index) -{ - uint32_t friend_lossless_packet_index = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (friend_lossless_packet_index > index) { - return nullptr; - } - - if (events->events[i].type == TOX_EVENT_FRIEND_LOSSLESS_PACKET) { - const Tox_Event_Friend_Lossless_Packet *friend_lossless_packet = events->events[i].data.friend_lossless_packet; - if (friend_lossless_packet_index == index) { - return friend_lossless_packet; - } - ++friend_lossless_packet_index; - } - } - - return nullptr; -} - -uint32_t tox_events_get_friend_lossless_packet_size(const Tox_Events *events) -{ - uint32_t friend_lossless_packet_size = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (events->events[i].type == TOX_EVENT_FRIEND_LOSSLESS_PACKET) { - ++friend_lossless_packet_size; - } - } - - return friend_lossless_packet_size; -} - bool tox_event_friend_lossless_packet_unpack( Tox_Event_Friend_Lossless_Packet **event, Bin_Unpack *bu, const Memory *mem) { diff --git a/external/toxcore/c-toxcore/toxcore/events/friend_lossy_packet.c b/external/toxcore/c-toxcore/toxcore/events/friend_lossy_packet.c index 3a2b80a8..d7319f53 100644 --- a/external/toxcore/c-toxcore/toxcore/events/friend_lossy_packet.c +++ b/external/toxcore/c-toxcore/toxcore/events/friend_lossy_packet.c @@ -54,13 +54,14 @@ static bool tox_event_friend_lossy_packet_set_data(Tox_Event_Friend_Lossy_Packet friend_lossy_packet->data_length = 0; } - friend_lossy_packet->data = (uint8_t *)malloc(data_length); + uint8_t *data_copy = (uint8_t *)malloc(data_length); - if (friend_lossy_packet->data == nullptr) { + if (data_copy == nullptr) { return false; } - memcpy(friend_lossy_packet->data, data, data_length); + memcpy(data_copy, data, data_length); + friend_lossy_packet->data = data_copy; friend_lossy_packet->data_length = data_length; return true; } @@ -162,42 +163,6 @@ static Tox_Event_Friend_Lossy_Packet *tox_events_add_friend_lossy_packet(Tox_Eve return friend_lossy_packet; } -const Tox_Event_Friend_Lossy_Packet *tox_events_get_friend_lossy_packet(const Tox_Events *events, uint32_t index) -{ - uint32_t friend_lossy_packet_index = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (friend_lossy_packet_index > index) { - return nullptr; - } - - if (events->events[i].type == TOX_EVENT_FRIEND_LOSSY_PACKET) { - const Tox_Event_Friend_Lossy_Packet *friend_lossy_packet = events->events[i].data.friend_lossy_packet; - if (friend_lossy_packet_index == index) { - return friend_lossy_packet; - } - ++friend_lossy_packet_index; - } - } - - return nullptr; -} - -uint32_t tox_events_get_friend_lossy_packet_size(const Tox_Events *events) -{ - uint32_t friend_lossy_packet_size = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (events->events[i].type == TOX_EVENT_FRIEND_LOSSY_PACKET) { - ++friend_lossy_packet_size; - } - } - - return friend_lossy_packet_size; -} - bool tox_event_friend_lossy_packet_unpack( Tox_Event_Friend_Lossy_Packet **event, Bin_Unpack *bu, const Memory *mem) { diff --git a/external/toxcore/c-toxcore/toxcore/events/friend_message.c b/external/toxcore/c-toxcore/toxcore/events/friend_message.c index 6a61a314..21495497 100644 --- a/external/toxcore/c-toxcore/toxcore/events/friend_message.c +++ b/external/toxcore/c-toxcore/toxcore/events/friend_message.c @@ -69,13 +69,14 @@ static bool tox_event_friend_message_set_message(Tox_Event_Friend_Message *frien friend_message->message_length = 0; } - friend_message->message = (uint8_t *)malloc(message_length); + uint8_t *message_copy = (uint8_t *)malloc(message_length); - if (friend_message->message == nullptr) { + if (message_copy == nullptr) { return false; } - memcpy(friend_message->message, message, message_length); + memcpy(message_copy, message, message_length); + friend_message->message = message_copy; friend_message->message_length = message_length; return true; } @@ -179,42 +180,6 @@ static Tox_Event_Friend_Message *tox_events_add_friend_message(Tox_Events *event return friend_message; } -const Tox_Event_Friend_Message *tox_events_get_friend_message(const Tox_Events *events, uint32_t index) -{ - uint32_t friend_message_index = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (friend_message_index > index) { - return nullptr; - } - - if (events->events[i].type == TOX_EVENT_FRIEND_MESSAGE) { - const Tox_Event_Friend_Message *friend_message = events->events[i].data.friend_message; - if (friend_message_index == index) { - return friend_message; - } - ++friend_message_index; - } - } - - return nullptr; -} - -uint32_t tox_events_get_friend_message_size(const Tox_Events *events) -{ - uint32_t friend_message_size = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (events->events[i].type == TOX_EVENT_FRIEND_MESSAGE) { - ++friend_message_size; - } - } - - return friend_message_size; -} - bool tox_event_friend_message_unpack( Tox_Event_Friend_Message **event, Bin_Unpack *bu, const Memory *mem) { diff --git a/external/toxcore/c-toxcore/toxcore/events/friend_name.c b/external/toxcore/c-toxcore/toxcore/events/friend_name.c index cad85237..53fa9d85 100644 --- a/external/toxcore/c-toxcore/toxcore/events/friend_name.c +++ b/external/toxcore/c-toxcore/toxcore/events/friend_name.c @@ -54,13 +54,14 @@ static bool tox_event_friend_name_set_name(Tox_Event_Friend_Name *friend_name, friend_name->name_length = 0; } - friend_name->name = (uint8_t *)malloc(name_length); + uint8_t *name_copy = (uint8_t *)malloc(name_length); - if (friend_name->name == nullptr) { + if (name_copy == nullptr) { return false; } - memcpy(friend_name->name, name, name_length); + memcpy(name_copy, name, name_length); + friend_name->name = name_copy; friend_name->name_length = name_length; return true; } @@ -162,42 +163,6 @@ static Tox_Event_Friend_Name *tox_events_add_friend_name(Tox_Events *events, con return friend_name; } -const Tox_Event_Friend_Name *tox_events_get_friend_name(const Tox_Events *events, uint32_t index) -{ - uint32_t friend_name_index = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (friend_name_index > index) { - return nullptr; - } - - if (events->events[i].type == TOX_EVENT_FRIEND_NAME) { - const Tox_Event_Friend_Name *friend_name = events->events[i].data.friend_name; - if (friend_name_index == index) { - return friend_name; - } - ++friend_name_index; - } - } - - return nullptr; -} - -uint32_t tox_events_get_friend_name_size(const Tox_Events *events) -{ - uint32_t friend_name_size = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (events->events[i].type == TOX_EVENT_FRIEND_NAME) { - ++friend_name_size; - } - } - - return friend_name_size; -} - bool tox_event_friend_name_unpack( Tox_Event_Friend_Name **event, Bin_Unpack *bu, const Memory *mem) { diff --git a/external/toxcore/c-toxcore/toxcore/events/friend_read_receipt.c b/external/toxcore/c-toxcore/toxcore/events/friend_read_receipt.c index bf53234f..8c88b417 100644 --- a/external/toxcore/c-toxcore/toxcore/events/friend_read_receipt.c +++ b/external/toxcore/c-toxcore/toxcore/events/friend_read_receipt.c @@ -139,42 +139,6 @@ static Tox_Event_Friend_Read_Receipt *tox_events_add_friend_read_receipt(Tox_Eve return friend_read_receipt; } -const Tox_Event_Friend_Read_Receipt *tox_events_get_friend_read_receipt(const Tox_Events *events, uint32_t index) -{ - uint32_t friend_read_receipt_index = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (friend_read_receipt_index > index) { - return nullptr; - } - - if (events->events[i].type == TOX_EVENT_FRIEND_READ_RECEIPT) { - const Tox_Event_Friend_Read_Receipt *friend_read_receipt = events->events[i].data.friend_read_receipt; - if (friend_read_receipt_index == index) { - return friend_read_receipt; - } - ++friend_read_receipt_index; - } - } - - return nullptr; -} - -uint32_t tox_events_get_friend_read_receipt_size(const Tox_Events *events) -{ - uint32_t friend_read_receipt_size = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (events->events[i].type == TOX_EVENT_FRIEND_READ_RECEIPT) { - ++friend_read_receipt_size; - } - } - - return friend_read_receipt_size; -} - bool tox_event_friend_read_receipt_unpack( Tox_Event_Friend_Read_Receipt **event, Bin_Unpack *bu, const Memory *mem) { diff --git a/external/toxcore/c-toxcore/toxcore/events/friend_request.c b/external/toxcore/c-toxcore/toxcore/events/friend_request.c index 7f68a43d..4a193608 100644 --- a/external/toxcore/c-toxcore/toxcore/events/friend_request.c +++ b/external/toxcore/c-toxcore/toxcore/events/friend_request.c @@ -55,13 +55,14 @@ static bool tox_event_friend_request_set_message(Tox_Event_Friend_Request *frien friend_request->message_length = 0; } - friend_request->message = (uint8_t *)mem_balloc(mem, message_length * sizeof(uint8_t)); + uint8_t *message_copy = (uint8_t *)mem_balloc(mem, message_length); - if (friend_request->message == nullptr) { + if (message_copy == nullptr) { return false; } - memcpy(friend_request->message, message, message_length * sizeof(uint8_t)); + memcpy(message_copy, message, message_length); + friend_request->message = message_copy; friend_request->message_length = message_length; return true; } @@ -157,44 +158,6 @@ static Tox_Event_Friend_Request *tox_events_add_friend_request(Tox_Events *event return friend_request; } -const Tox_Event_Friend_Request *tox_events_get_friend_request( - const Tox_Events *events, uint32_t index) -{ - uint32_t friend_request_index = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (friend_request_index > index) { - return nullptr; - } - - if (events->events[i].type == TOX_EVENT_FRIEND_REQUEST) { - const Tox_Event_Friend_Request *friend_request = events->events[i].data.friend_request; - if (friend_request_index == index) { - return friend_request; - } - ++friend_request_index; - } - } - - return nullptr; -} - -uint32_t tox_events_get_friend_request_size( - const Tox_Events *events) -{ - uint32_t friend_request_size = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (events->events[i].type == TOX_EVENT_FRIEND_REQUEST) { - ++friend_request_size; - } - } - - return friend_request_size; -} - bool tox_event_friend_request_unpack( Tox_Event_Friend_Request **event, Bin_Unpack *bu, const Memory *mem) { diff --git a/external/toxcore/c-toxcore/toxcore/events/friend_status.c b/external/toxcore/c-toxcore/toxcore/events/friend_status.c index 794aa668..1b0a8a41 100644 --- a/external/toxcore/c-toxcore/toxcore/events/friend_status.c +++ b/external/toxcore/c-toxcore/toxcore/events/friend_status.c @@ -140,42 +140,6 @@ static Tox_Event_Friend_Status *tox_events_add_friend_status(Tox_Events *events, return friend_status; } -const Tox_Event_Friend_Status *tox_events_get_friend_status(const Tox_Events *events, uint32_t index) -{ - uint32_t friend_status_index = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (friend_status_index > index) { - return nullptr; - } - - if (events->events[i].type == TOX_EVENT_FRIEND_STATUS) { - const Tox_Event_Friend_Status *friend_status = events->events[i].data.friend_status; - if (friend_status_index == index) { - return friend_status; - } - ++friend_status_index; - } - } - - return nullptr; -} - -uint32_t tox_events_get_friend_status_size(const Tox_Events *events) -{ - uint32_t friend_status_size = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (events->events[i].type == TOX_EVENT_FRIEND_STATUS) { - ++friend_status_size; - } - } - - return friend_status_size; -} - bool tox_event_friend_status_unpack( Tox_Event_Friend_Status **event, Bin_Unpack *bu, const Memory *mem) { diff --git a/external/toxcore/c-toxcore/toxcore/events/friend_status_message.c b/external/toxcore/c-toxcore/toxcore/events/friend_status_message.c index 99628ba6..876a9f73 100644 --- a/external/toxcore/c-toxcore/toxcore/events/friend_status_message.c +++ b/external/toxcore/c-toxcore/toxcore/events/friend_status_message.c @@ -54,13 +54,14 @@ static bool tox_event_friend_status_message_set_message(Tox_Event_Friend_Status_ friend_status_message->message_length = 0; } - friend_status_message->message = (uint8_t *)malloc(message_length); + uint8_t *message_copy = (uint8_t *)malloc(message_length); - if (friend_status_message->message == nullptr) { + if (message_copy == nullptr) { return false; } - memcpy(friend_status_message->message, message, message_length); + memcpy(message_copy, message, message_length); + friend_status_message->message = message_copy; friend_status_message->message_length = message_length; return true; } @@ -162,42 +163,6 @@ static Tox_Event_Friend_Status_Message *tox_events_add_friend_status_message(Tox return friend_status_message; } -const Tox_Event_Friend_Status_Message *tox_events_get_friend_status_message(const Tox_Events *events, uint32_t index) -{ - uint32_t friend_status_message_index = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (friend_status_message_index > index) { - return nullptr; - } - - if (events->events[i].type == TOX_EVENT_FRIEND_STATUS_MESSAGE) { - const Tox_Event_Friend_Status_Message *friend_status_message = events->events[i].data.friend_status_message; - if (friend_status_message_index == index) { - return friend_status_message; - } - ++friend_status_message_index; - } - } - - return nullptr; -} - -uint32_t tox_events_get_friend_status_message_size(const Tox_Events *events) -{ - uint32_t friend_status_message_size = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (events->events[i].type == TOX_EVENT_FRIEND_STATUS_MESSAGE) { - ++friend_status_message_size; - } - } - - return friend_status_message_size; -} - bool tox_event_friend_status_message_unpack( Tox_Event_Friend_Status_Message **event, Bin_Unpack *bu, const Memory *mem) { diff --git a/external/toxcore/c-toxcore/toxcore/events/friend_typing.c b/external/toxcore/c-toxcore/toxcore/events/friend_typing.c index 563cbbc8..80458e78 100644 --- a/external/toxcore/c-toxcore/toxcore/events/friend_typing.c +++ b/external/toxcore/c-toxcore/toxcore/events/friend_typing.c @@ -139,42 +139,6 @@ static Tox_Event_Friend_Typing *tox_events_add_friend_typing(Tox_Events *events, return friend_typing; } -const Tox_Event_Friend_Typing *tox_events_get_friend_typing(const Tox_Events *events, uint32_t index) -{ - uint32_t friend_typing_index = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (friend_typing_index > index) { - return nullptr; - } - - if (events->events[i].type == TOX_EVENT_FRIEND_TYPING) { - const Tox_Event_Friend_Typing *friend_typing = events->events[i].data.friend_typing; - if (friend_typing_index == index) { - return friend_typing; - } - ++friend_typing_index; - } - } - - return nullptr; -} - -uint32_t tox_events_get_friend_typing_size(const Tox_Events *events) -{ - uint32_t friend_typing_size = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (events->events[i].type == TOX_EVENT_FRIEND_TYPING) { - ++friend_typing_size; - } - } - - return friend_typing_size; -} - bool tox_event_friend_typing_unpack( Tox_Event_Friend_Typing **event, Bin_Unpack *bu, const Memory *mem) { diff --git a/external/toxcore/c-toxcore/toxcore/events/group_custom_packet.c b/external/toxcore/c-toxcore/toxcore/events/group_custom_packet.c index 999a5ec0..a265ee4a 100644 --- a/external/toxcore/c-toxcore/toxcore/events/group_custom_packet.c +++ b/external/toxcore/c-toxcore/toxcore/events/group_custom_packet.c @@ -68,13 +68,14 @@ static bool tox_event_group_custom_packet_set_data(Tox_Event_Group_Custom_Packet group_custom_packet->data_length = 0; } - group_custom_packet->data = (uint8_t *)malloc(data_length); + uint8_t *data_copy = (uint8_t *)malloc(data_length); - if (group_custom_packet->data == nullptr) { + if (data_copy == nullptr) { return false; } - memcpy(group_custom_packet->data, data, data_length); + memcpy(data_copy, data, data_length); + group_custom_packet->data = data_copy; group_custom_packet->data_length = data_length; return true; } @@ -178,42 +179,6 @@ static Tox_Event_Group_Custom_Packet *tox_events_add_group_custom_packet(Tox_Eve return group_custom_packet; } -const Tox_Event_Group_Custom_Packet *tox_events_get_group_custom_packet(const Tox_Events *events, uint32_t index) -{ - uint32_t group_custom_packet_index = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (group_custom_packet_index > index) { - return nullptr; - } - - if (events->events[i].type == TOX_EVENT_GROUP_CUSTOM_PACKET) { - const Tox_Event_Group_Custom_Packet *group_custom_packet = events->events[i].data.group_custom_packet; - if (group_custom_packet_index == index) { - return group_custom_packet; - } - ++group_custom_packet_index; - } - } - - return nullptr; -} - -uint32_t tox_events_get_group_custom_packet_size(const Tox_Events *events) -{ - uint32_t group_custom_packet_size = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (events->events[i].type == TOX_EVENT_GROUP_CUSTOM_PACKET) { - ++group_custom_packet_size; - } - } - - return group_custom_packet_size; -} - bool tox_event_group_custom_packet_unpack( Tox_Event_Group_Custom_Packet **event, Bin_Unpack *bu, const Memory *mem) { diff --git a/external/toxcore/c-toxcore/toxcore/events/group_custom_private_packet.c b/external/toxcore/c-toxcore/toxcore/events/group_custom_private_packet.c index e1f1e476..bcb7cb3e 100644 --- a/external/toxcore/c-toxcore/toxcore/events/group_custom_private_packet.c +++ b/external/toxcore/c-toxcore/toxcore/events/group_custom_private_packet.c @@ -68,13 +68,14 @@ static bool tox_event_group_custom_private_packet_set_data(Tox_Event_Group_Custo group_custom_private_packet->data_length = 0; } - group_custom_private_packet->data = (uint8_t *)malloc(data_length); + uint8_t *data_copy = (uint8_t *)malloc(data_length); - if (group_custom_private_packet->data == nullptr) { + if (data_copy == nullptr) { return false; } - memcpy(group_custom_private_packet->data, data, data_length); + memcpy(data_copy, data, data_length); + group_custom_private_packet->data = data_copy; group_custom_private_packet->data_length = data_length; return true; } @@ -178,42 +179,6 @@ static Tox_Event_Group_Custom_Private_Packet *tox_events_add_group_custom_privat return group_custom_private_packet; } -const Tox_Event_Group_Custom_Private_Packet *tox_events_get_group_custom_private_packet(const Tox_Events *events, uint32_t index) -{ - uint32_t group_custom_private_packet_index = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (group_custom_private_packet_index > index) { - return nullptr; - } - - if (events->events[i].type == TOX_EVENT_GROUP_CUSTOM_PRIVATE_PACKET) { - const Tox_Event_Group_Custom_Private_Packet *group_custom_private_packet = events->events[i].data.group_custom_private_packet; - if (group_custom_private_packet_index == index) { - return group_custom_private_packet; - } - ++group_custom_private_packet_index; - } - } - - return nullptr; -} - -uint32_t tox_events_get_group_custom_private_packet_size(const Tox_Events *events) -{ - uint32_t group_custom_private_packet_size = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (events->events[i].type == TOX_EVENT_GROUP_CUSTOM_PRIVATE_PACKET) { - ++group_custom_private_packet_size; - } - } - - return group_custom_private_packet_size; -} - bool tox_event_group_custom_private_packet_unpack( Tox_Event_Group_Custom_Private_Packet **event, Bin_Unpack *bu, const Memory *mem) { diff --git a/external/toxcore/c-toxcore/toxcore/events/group_invite.c b/external/toxcore/c-toxcore/toxcore/events/group_invite.c index 9eeaec43..97fad95c 100644 --- a/external/toxcore/c-toxcore/toxcore/events/group_invite.c +++ b/external/toxcore/c-toxcore/toxcore/events/group_invite.c @@ -56,13 +56,14 @@ static bool tox_event_group_invite_set_invite_data(Tox_Event_Group_Invite *group group_invite->invite_data_length = 0; } - group_invite->invite_data = (uint8_t *)malloc(invite_data_length); + uint8_t *invite_data_copy = (uint8_t *)malloc(invite_data_length); - if (group_invite->invite_data == nullptr) { + if (invite_data_copy == nullptr) { return false; } - memcpy(group_invite->invite_data, invite_data, invite_data_length); + memcpy(invite_data_copy, invite_data, invite_data_length); + group_invite->invite_data = invite_data_copy; group_invite->invite_data_length = invite_data_length; return true; } @@ -89,13 +90,14 @@ static bool tox_event_group_invite_set_group_name(Tox_Event_Group_Invite *group_ group_invite->group_name_length = 0; } - group_invite->group_name = (uint8_t *)malloc(group_name_length); + uint8_t *group_name_copy = (uint8_t *)malloc(group_name_length); - if (group_invite->group_name == nullptr) { + if (group_name_copy == nullptr) { return false; } - memcpy(group_invite->group_name, group_name, group_name_length); + memcpy(group_name_copy, group_name, group_name_length); + group_invite->group_name = group_name_copy; group_invite->group_name_length = group_name_length; return true; } @@ -200,42 +202,6 @@ static Tox_Event_Group_Invite *tox_events_add_group_invite(Tox_Events *events, c return group_invite; } -const Tox_Event_Group_Invite *tox_events_get_group_invite(const Tox_Events *events, uint32_t index) -{ - uint32_t group_invite_index = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (group_invite_index > index) { - return nullptr; - } - - if (events->events[i].type == TOX_EVENT_GROUP_INVITE) { - const Tox_Event_Group_Invite *group_invite = events->events[i].data.group_invite; - if (group_invite_index == index) { - return group_invite; - } - ++group_invite_index; - } - } - - return nullptr; -} - -uint32_t tox_events_get_group_invite_size(const Tox_Events *events) -{ - uint32_t group_invite_size = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (events->events[i].type == TOX_EVENT_GROUP_INVITE) { - ++group_invite_size; - } - } - - return group_invite_size; -} - bool tox_event_group_invite_unpack( Tox_Event_Group_Invite **event, Bin_Unpack *bu, const Memory *mem) { diff --git a/external/toxcore/c-toxcore/toxcore/events/group_join_fail.c b/external/toxcore/c-toxcore/toxcore/events/group_join_fail.c index 0e526131..10a9516d 100644 --- a/external/toxcore/c-toxcore/toxcore/events/group_join_fail.c +++ b/external/toxcore/c-toxcore/toxcore/events/group_join_fail.c @@ -140,42 +140,6 @@ static Tox_Event_Group_Join_Fail *tox_events_add_group_join_fail(Tox_Events *eve return group_join_fail; } -const Tox_Event_Group_Join_Fail *tox_events_get_group_join_fail(const Tox_Events *events, uint32_t index) -{ - uint32_t group_join_fail_index = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (group_join_fail_index > index) { - return nullptr; - } - - if (events->events[i].type == TOX_EVENT_GROUP_JOIN_FAIL) { - const Tox_Event_Group_Join_Fail *group_join_fail = events->events[i].data.group_join_fail; - if (group_join_fail_index == index) { - return group_join_fail; - } - ++group_join_fail_index; - } - } - - return nullptr; -} - -uint32_t tox_events_get_group_join_fail_size(const Tox_Events *events) -{ - uint32_t group_join_fail_size = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (events->events[i].type == TOX_EVENT_GROUP_JOIN_FAIL) { - ++group_join_fail_size; - } - } - - return group_join_fail_size; -} - bool tox_event_group_join_fail_unpack( Tox_Event_Group_Join_Fail **event, Bin_Unpack *bu, const Memory *mem) { diff --git a/external/toxcore/c-toxcore/toxcore/events/group_message.c b/external/toxcore/c-toxcore/toxcore/events/group_message.c index 7a821c9a..06a04a76 100644 --- a/external/toxcore/c-toxcore/toxcore/events/group_message.c +++ b/external/toxcore/c-toxcore/toxcore/events/group_message.c @@ -84,13 +84,14 @@ static bool tox_event_group_message_set_message(Tox_Event_Group_Message *group_m group_message->message_length = 0; } - group_message->message = (uint8_t *)malloc(message_length); + uint8_t *message_copy = (uint8_t *)malloc(message_length); - if (group_message->message == nullptr) { + if (message_copy == nullptr) { return false; } - memcpy(group_message->message, message, message_length); + memcpy(message_copy, message, message_length); + group_message->message = message_copy; group_message->message_length = message_length; return true; } @@ -211,42 +212,6 @@ static Tox_Event_Group_Message *tox_events_add_group_message(Tox_Events *events, return group_message; } -const Tox_Event_Group_Message *tox_events_get_group_message(const Tox_Events *events, uint32_t index) -{ - uint32_t group_message_index = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (group_message_index > index) { - return nullptr; - } - - if (events->events[i].type == TOX_EVENT_GROUP_MESSAGE) { - const Tox_Event_Group_Message *group_message = events->events[i].data.group_message; - if (group_message_index == index) { - return group_message; - } - ++group_message_index; - } - } - - return nullptr; -} - -uint32_t tox_events_get_group_message_size(const Tox_Events *events) -{ - uint32_t group_message_size = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (events->events[i].type == TOX_EVENT_GROUP_MESSAGE) { - ++group_message_size; - } - } - - return group_message_size; -} - bool tox_event_group_message_unpack( Tox_Event_Group_Message **event, Bin_Unpack *bu, const Memory *mem) { diff --git a/external/toxcore/c-toxcore/toxcore/events/group_moderation.c b/external/toxcore/c-toxcore/toxcore/events/group_moderation.c index 74a5e9d0..f728a270 100644 --- a/external/toxcore/c-toxcore/toxcore/events/group_moderation.c +++ b/external/toxcore/c-toxcore/toxcore/events/group_moderation.c @@ -172,42 +172,6 @@ static Tox_Event_Group_Moderation *tox_events_add_group_moderation(Tox_Events *e return group_moderation; } -const Tox_Event_Group_Moderation *tox_events_get_group_moderation(const Tox_Events *events, uint32_t index) -{ - uint32_t group_moderation_index = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (group_moderation_index > index) { - return nullptr; - } - - if (events->events[i].type == TOX_EVENT_GROUP_MODERATION) { - const Tox_Event_Group_Moderation *group_moderation = events->events[i].data.group_moderation; - if (group_moderation_index == index) { - return group_moderation; - } - ++group_moderation_index; - } - } - - return nullptr; -} - -uint32_t tox_events_get_group_moderation_size(const Tox_Events *events) -{ - uint32_t group_moderation_size = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (events->events[i].type == TOX_EVENT_GROUP_MODERATION) { - ++group_moderation_size; - } - } - - return group_moderation_size; -} - bool tox_event_group_moderation_unpack( Tox_Event_Group_Moderation **event, Bin_Unpack *bu, const Memory *mem) { diff --git a/external/toxcore/c-toxcore/toxcore/events/group_password.c b/external/toxcore/c-toxcore/toxcore/events/group_password.c index b15d0f81..6f19bd5a 100644 --- a/external/toxcore/c-toxcore/toxcore/events/group_password.c +++ b/external/toxcore/c-toxcore/toxcore/events/group_password.c @@ -54,13 +54,14 @@ static bool tox_event_group_password_set_password(Tox_Event_Group_Password *grou group_password->password_length = 0; } - group_password->password = (uint8_t *)malloc(password_length); + uint8_t *password_copy = (uint8_t *)malloc(password_length); - if (group_password->password == nullptr) { + if (password_copy == nullptr) { return false; } - memcpy(group_password->password, password, password_length); + memcpy(password_copy, password, password_length); + group_password->password = password_copy; group_password->password_length = password_length; return true; } @@ -162,42 +163,6 @@ static Tox_Event_Group_Password *tox_events_add_group_password(Tox_Events *event return group_password; } -const Tox_Event_Group_Password *tox_events_get_group_password(const Tox_Events *events, uint32_t index) -{ - uint32_t group_password_index = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (group_password_index > index) { - return nullptr; - } - - if (events->events[i].type == TOX_EVENT_GROUP_PASSWORD) { - const Tox_Event_Group_Password *group_password = events->events[i].data.group_password; - if (group_password_index == index) { - return group_password; - } - ++group_password_index; - } - } - - return nullptr; -} - -uint32_t tox_events_get_group_password_size(const Tox_Events *events) -{ - uint32_t group_password_size = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (events->events[i].type == TOX_EVENT_GROUP_PASSWORD) { - ++group_password_size; - } - } - - return group_password_size; -} - bool tox_event_group_password_unpack( Tox_Event_Group_Password **event, Bin_Unpack *bu, const Memory *mem) { diff --git a/external/toxcore/c-toxcore/toxcore/events/group_peer_exit.c b/external/toxcore/c-toxcore/toxcore/events/group_peer_exit.c index a7f54bd9..5a94cc20 100644 --- a/external/toxcore/c-toxcore/toxcore/events/group_peer_exit.c +++ b/external/toxcore/c-toxcore/toxcore/events/group_peer_exit.c @@ -85,13 +85,14 @@ static bool tox_event_group_peer_exit_set_name(Tox_Event_Group_Peer_Exit *group_ group_peer_exit->name_length = 0; } - group_peer_exit->name = (uint8_t *)malloc(name_length); + uint8_t *name_copy = (uint8_t *)malloc(name_length); - if (group_peer_exit->name == nullptr) { + if (name_copy == nullptr) { return false; } - memcpy(group_peer_exit->name, name, name_length); + memcpy(name_copy, name, name_length); + group_peer_exit->name = name_copy; group_peer_exit->name_length = name_length; return true; } @@ -118,13 +119,14 @@ static bool tox_event_group_peer_exit_set_part_message(Tox_Event_Group_Peer_Exit group_peer_exit->part_message_length = 0; } - group_peer_exit->part_message = (uint8_t *)malloc(part_message_length); + uint8_t *part_message_copy = (uint8_t *)malloc(part_message_length); - if (group_peer_exit->part_message == nullptr) { + if (part_message_copy == nullptr) { return false; } - memcpy(group_peer_exit->part_message, part_message, part_message_length); + memcpy(part_message_copy, part_message, part_message_length); + group_peer_exit->part_message = part_message_copy; group_peer_exit->part_message_length = part_message_length; return true; } @@ -233,42 +235,6 @@ static Tox_Event_Group_Peer_Exit *tox_events_add_group_peer_exit(Tox_Events *eve return group_peer_exit; } -const Tox_Event_Group_Peer_Exit *tox_events_get_group_peer_exit(const Tox_Events *events, uint32_t index) -{ - uint32_t group_peer_exit_index = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (group_peer_exit_index > index) { - return nullptr; - } - - if (events->events[i].type == TOX_EVENT_GROUP_PEER_EXIT) { - const Tox_Event_Group_Peer_Exit *group_peer_exit = events->events[i].data.group_peer_exit; - if (group_peer_exit_index == index) { - return group_peer_exit; - } - ++group_peer_exit_index; - } - } - - return nullptr; -} - -uint32_t tox_events_get_group_peer_exit_size(const Tox_Events *events) -{ - uint32_t group_peer_exit_size = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (events->events[i].type == TOX_EVENT_GROUP_PEER_EXIT) { - ++group_peer_exit_size; - } - } - - return group_peer_exit_size; -} - bool tox_event_group_peer_exit_unpack( Tox_Event_Group_Peer_Exit **event, Bin_Unpack *bu, const Memory *mem) { diff --git a/external/toxcore/c-toxcore/toxcore/events/group_peer_join.c b/external/toxcore/c-toxcore/toxcore/events/group_peer_join.c index f19ab492..ec366cfc 100644 --- a/external/toxcore/c-toxcore/toxcore/events/group_peer_join.c +++ b/external/toxcore/c-toxcore/toxcore/events/group_peer_join.c @@ -139,42 +139,6 @@ static Tox_Event_Group_Peer_Join *tox_events_add_group_peer_join(Tox_Events *eve return group_peer_join; } -const Tox_Event_Group_Peer_Join *tox_events_get_group_peer_join(const Tox_Events *events, uint32_t index) -{ - uint32_t group_peer_join_index = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (group_peer_join_index > index) { - return nullptr; - } - - if (events->events[i].type == TOX_EVENT_GROUP_PEER_JOIN) { - const Tox_Event_Group_Peer_Join *group_peer_join = events->events[i].data.group_peer_join; - if (group_peer_join_index == index) { - return group_peer_join; - } - ++group_peer_join_index; - } - } - - return nullptr; -} - -uint32_t tox_events_get_group_peer_join_size(const Tox_Events *events) -{ - uint32_t group_peer_join_size = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (events->events[i].type == TOX_EVENT_GROUP_PEER_JOIN) { - ++group_peer_join_size; - } - } - - return group_peer_join_size; -} - bool tox_event_group_peer_join_unpack( Tox_Event_Group_Peer_Join **event, Bin_Unpack *bu, const Memory *mem) { diff --git a/external/toxcore/c-toxcore/toxcore/events/group_peer_limit.c b/external/toxcore/c-toxcore/toxcore/events/group_peer_limit.c index 47cdb5b5..7e37cef3 100644 --- a/external/toxcore/c-toxcore/toxcore/events/group_peer_limit.c +++ b/external/toxcore/c-toxcore/toxcore/events/group_peer_limit.c @@ -139,42 +139,6 @@ static Tox_Event_Group_Peer_Limit *tox_events_add_group_peer_limit(Tox_Events *e return group_peer_limit; } -const Tox_Event_Group_Peer_Limit *tox_events_get_group_peer_limit(const Tox_Events *events, uint32_t index) -{ - uint32_t group_peer_limit_index = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (group_peer_limit_index > index) { - return nullptr; - } - - if (events->events[i].type == TOX_EVENT_GROUP_PEER_LIMIT) { - const Tox_Event_Group_Peer_Limit *group_peer_limit = events->events[i].data.group_peer_limit; - if (group_peer_limit_index == index) { - return group_peer_limit; - } - ++group_peer_limit_index; - } - } - - return nullptr; -} - -uint32_t tox_events_get_group_peer_limit_size(const Tox_Events *events) -{ - uint32_t group_peer_limit_size = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (events->events[i].type == TOX_EVENT_GROUP_PEER_LIMIT) { - ++group_peer_limit_size; - } - } - - return group_peer_limit_size; -} - bool tox_event_group_peer_limit_unpack( Tox_Event_Group_Peer_Limit **event, Bin_Unpack *bu, const Memory *mem) { diff --git a/external/toxcore/c-toxcore/toxcore/events/group_peer_name.c b/external/toxcore/c-toxcore/toxcore/events/group_peer_name.c index d58b2384..e81ee04c 100644 --- a/external/toxcore/c-toxcore/toxcore/events/group_peer_name.c +++ b/external/toxcore/c-toxcore/toxcore/events/group_peer_name.c @@ -68,13 +68,14 @@ static bool tox_event_group_peer_name_set_name(Tox_Event_Group_Peer_Name *group_ group_peer_name->name_length = 0; } - group_peer_name->name = (uint8_t *)malloc(name_length); + uint8_t *name_copy = (uint8_t *)malloc(name_length); - if (group_peer_name->name == nullptr) { + if (name_copy == nullptr) { return false; } - memcpy(group_peer_name->name, name, name_length); + memcpy(name_copy, name, name_length); + group_peer_name->name = name_copy; group_peer_name->name_length = name_length; return true; } @@ -178,42 +179,6 @@ static Tox_Event_Group_Peer_Name *tox_events_add_group_peer_name(Tox_Events *eve return group_peer_name; } -const Tox_Event_Group_Peer_Name *tox_events_get_group_peer_name(const Tox_Events *events, uint32_t index) -{ - uint32_t group_peer_name_index = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (group_peer_name_index > index) { - return nullptr; - } - - if (events->events[i].type == TOX_EVENT_GROUP_PEER_NAME) { - const Tox_Event_Group_Peer_Name *group_peer_name = events->events[i].data.group_peer_name; - if (group_peer_name_index == index) { - return group_peer_name; - } - ++group_peer_name_index; - } - } - - return nullptr; -} - -uint32_t tox_events_get_group_peer_name_size(const Tox_Events *events) -{ - uint32_t group_peer_name_size = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (events->events[i].type == TOX_EVENT_GROUP_PEER_NAME) { - ++group_peer_name_size; - } - } - - return group_peer_name_size; -} - bool tox_event_group_peer_name_unpack( Tox_Event_Group_Peer_Name **event, Bin_Unpack *bu, const Memory *mem) { diff --git a/external/toxcore/c-toxcore/toxcore/events/group_peer_status.c b/external/toxcore/c-toxcore/toxcore/events/group_peer_status.c index cbf5f2cb..8bf456f1 100644 --- a/external/toxcore/c-toxcore/toxcore/events/group_peer_status.c +++ b/external/toxcore/c-toxcore/toxcore/events/group_peer_status.c @@ -156,42 +156,6 @@ static Tox_Event_Group_Peer_Status *tox_events_add_group_peer_status(Tox_Events return group_peer_status; } -const Tox_Event_Group_Peer_Status *tox_events_get_group_peer_status(const Tox_Events *events, uint32_t index) -{ - uint32_t group_peer_status_index = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (group_peer_status_index > index) { - return nullptr; - } - - if (events->events[i].type == TOX_EVENT_GROUP_PEER_STATUS) { - const Tox_Event_Group_Peer_Status *group_peer_status = events->events[i].data.group_peer_status; - if (group_peer_status_index == index) { - return group_peer_status; - } - ++group_peer_status_index; - } - } - - return nullptr; -} - -uint32_t tox_events_get_group_peer_status_size(const Tox_Events *events) -{ - uint32_t group_peer_status_size = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (events->events[i].type == TOX_EVENT_GROUP_PEER_STATUS) { - ++group_peer_status_size; - } - } - - return group_peer_status_size; -} - bool tox_event_group_peer_status_unpack( Tox_Event_Group_Peer_Status **event, Bin_Unpack *bu, const Memory *mem) { diff --git a/external/toxcore/c-toxcore/toxcore/events/group_privacy_state.c b/external/toxcore/c-toxcore/toxcore/events/group_privacy_state.c index 330ed224..e82e4238 100644 --- a/external/toxcore/c-toxcore/toxcore/events/group_privacy_state.c +++ b/external/toxcore/c-toxcore/toxcore/events/group_privacy_state.c @@ -140,42 +140,6 @@ static Tox_Event_Group_Privacy_State *tox_events_add_group_privacy_state(Tox_Eve return group_privacy_state; } -const Tox_Event_Group_Privacy_State *tox_events_get_group_privacy_state(const Tox_Events *events, uint32_t index) -{ - uint32_t group_privacy_state_index = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (group_privacy_state_index > index) { - return nullptr; - } - - if (events->events[i].type == TOX_EVENT_GROUP_PRIVACY_STATE) { - const Tox_Event_Group_Privacy_State *group_privacy_state = events->events[i].data.group_privacy_state; - if (group_privacy_state_index == index) { - return group_privacy_state; - } - ++group_privacy_state_index; - } - } - - return nullptr; -} - -uint32_t tox_events_get_group_privacy_state_size(const Tox_Events *events) -{ - uint32_t group_privacy_state_size = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (events->events[i].type == TOX_EVENT_GROUP_PRIVACY_STATE) { - ++group_privacy_state_size; - } - } - - return group_privacy_state_size; -} - bool tox_event_group_privacy_state_unpack( Tox_Event_Group_Privacy_State **event, Bin_Unpack *bu, const Memory *mem) { diff --git a/external/toxcore/c-toxcore/toxcore/events/group_private_message.c b/external/toxcore/c-toxcore/toxcore/events/group_private_message.c index 14784426..efa62fbc 100644 --- a/external/toxcore/c-toxcore/toxcore/events/group_private_message.c +++ b/external/toxcore/c-toxcore/toxcore/events/group_private_message.c @@ -83,13 +83,14 @@ static bool tox_event_group_private_message_set_message(Tox_Event_Group_Private_ group_private_message->message_length = 0; } - group_private_message->message = (uint8_t *)malloc(message_length); + uint8_t *message_copy = (uint8_t *)malloc(message_length); - if (group_private_message->message == nullptr) { + if (message_copy == nullptr) { return false; } - memcpy(group_private_message->message, message, message_length); + memcpy(message_copy, message, message_length); + group_private_message->message = message_copy; group_private_message->message_length = message_length; return true; } @@ -195,42 +196,6 @@ static Tox_Event_Group_Private_Message *tox_events_add_group_private_message(Tox return group_private_message; } -const Tox_Event_Group_Private_Message *tox_events_get_group_private_message(const Tox_Events *events, uint32_t index) -{ - uint32_t group_private_message_index = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (group_private_message_index > index) { - return nullptr; - } - - if (events->events[i].type == TOX_EVENT_GROUP_PRIVATE_MESSAGE) { - const Tox_Event_Group_Private_Message *group_private_message = events->events[i].data.group_private_message; - if (group_private_message_index == index) { - return group_private_message; - } - ++group_private_message_index; - } - } - - return nullptr; -} - -uint32_t tox_events_get_group_private_message_size(const Tox_Events *events) -{ - uint32_t group_private_message_size = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (events->events[i].type == TOX_EVENT_GROUP_PRIVATE_MESSAGE) { - ++group_private_message_size; - } - } - - return group_private_message_size; -} - bool tox_event_group_private_message_unpack( Tox_Event_Group_Private_Message **event, Bin_Unpack *bu, const Memory *mem) { diff --git a/external/toxcore/c-toxcore/toxcore/events/group_self_join.c b/external/toxcore/c-toxcore/toxcore/events/group_self_join.c index 5d23cdea..ceca661f 100644 --- a/external/toxcore/c-toxcore/toxcore/events/group_self_join.c +++ b/external/toxcore/c-toxcore/toxcore/events/group_self_join.c @@ -118,42 +118,6 @@ static Tox_Event_Group_Self_Join *tox_events_add_group_self_join(Tox_Events *eve return group_self_join; } -const Tox_Event_Group_Self_Join *tox_events_get_group_self_join(const Tox_Events *events, uint32_t index) -{ - uint32_t group_self_join_index = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (group_self_join_index > index) { - return nullptr; - } - - if (events->events[i].type == TOX_EVENT_GROUP_SELF_JOIN) { - const Tox_Event_Group_Self_Join *group_self_join = events->events[i].data.group_self_join; - if (group_self_join_index == index) { - return group_self_join; - } - ++group_self_join_index; - } - } - - return nullptr; -} - -uint32_t tox_events_get_group_self_join_size(const Tox_Events *events) -{ - uint32_t group_self_join_size = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (events->events[i].type == TOX_EVENT_GROUP_SELF_JOIN) { - ++group_self_join_size; - } - } - - return group_self_join_size; -} - bool tox_event_group_self_join_unpack( Tox_Event_Group_Self_Join **event, Bin_Unpack *bu, const Memory *mem) { diff --git a/external/toxcore/c-toxcore/toxcore/events/group_topic.c b/external/toxcore/c-toxcore/toxcore/events/group_topic.c index 49f90ebf..48f5ed7c 100644 --- a/external/toxcore/c-toxcore/toxcore/events/group_topic.c +++ b/external/toxcore/c-toxcore/toxcore/events/group_topic.c @@ -68,13 +68,14 @@ static bool tox_event_group_topic_set_topic(Tox_Event_Group_Topic *group_topic, group_topic->topic_length = 0; } - group_topic->topic = (uint8_t *)malloc(topic_length); + uint8_t *topic_copy = (uint8_t *)malloc(topic_length); - if (group_topic->topic == nullptr) { + if (topic_copy == nullptr) { return false; } - memcpy(group_topic->topic, topic, topic_length); + memcpy(topic_copy, topic, topic_length); + group_topic->topic = topic_copy; group_topic->topic_length = topic_length; return true; } @@ -178,42 +179,6 @@ static Tox_Event_Group_Topic *tox_events_add_group_topic(Tox_Events *events, con return group_topic; } -const Tox_Event_Group_Topic *tox_events_get_group_topic(const Tox_Events *events, uint32_t index) -{ - uint32_t group_topic_index = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (group_topic_index > index) { - return nullptr; - } - - if (events->events[i].type == TOX_EVENT_GROUP_TOPIC) { - const Tox_Event_Group_Topic *group_topic = events->events[i].data.group_topic; - if (group_topic_index == index) { - return group_topic; - } - ++group_topic_index; - } - } - - return nullptr; -} - -uint32_t tox_events_get_group_topic_size(const Tox_Events *events) -{ - uint32_t group_topic_size = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (events->events[i].type == TOX_EVENT_GROUP_TOPIC) { - ++group_topic_size; - } - } - - return group_topic_size; -} - bool tox_event_group_topic_unpack( Tox_Event_Group_Topic **event, Bin_Unpack *bu, const Memory *mem) { diff --git a/external/toxcore/c-toxcore/toxcore/events/group_topic_lock.c b/external/toxcore/c-toxcore/toxcore/events/group_topic_lock.c index 1f301667..17d71028 100644 --- a/external/toxcore/c-toxcore/toxcore/events/group_topic_lock.c +++ b/external/toxcore/c-toxcore/toxcore/events/group_topic_lock.c @@ -140,42 +140,6 @@ static Tox_Event_Group_Topic_Lock *tox_events_add_group_topic_lock(Tox_Events *e return group_topic_lock; } -const Tox_Event_Group_Topic_Lock *tox_events_get_group_topic_lock(const Tox_Events *events, uint32_t index) -{ - uint32_t group_topic_lock_index = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (group_topic_lock_index > index) { - return nullptr; - } - - if (events->events[i].type == TOX_EVENT_GROUP_TOPIC_LOCK) { - const Tox_Event_Group_Topic_Lock *group_topic_lock = events->events[i].data.group_topic_lock; - if (group_topic_lock_index == index) { - return group_topic_lock; - } - ++group_topic_lock_index; - } - } - - return nullptr; -} - -uint32_t tox_events_get_group_topic_lock_size(const Tox_Events *events) -{ - uint32_t group_topic_lock_size = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (events->events[i].type == TOX_EVENT_GROUP_TOPIC_LOCK) { - ++group_topic_lock_size; - } - } - - return group_topic_lock_size; -} - bool tox_event_group_topic_lock_unpack( Tox_Event_Group_Topic_Lock **event, Bin_Unpack *bu, const Memory *mem) { diff --git a/external/toxcore/c-toxcore/toxcore/events/group_voice_state.c b/external/toxcore/c-toxcore/toxcore/events/group_voice_state.c index 8ed19827..053c9ed8 100644 --- a/external/toxcore/c-toxcore/toxcore/events/group_voice_state.c +++ b/external/toxcore/c-toxcore/toxcore/events/group_voice_state.c @@ -140,42 +140,6 @@ static Tox_Event_Group_Voice_State *tox_events_add_group_voice_state(Tox_Events return group_voice_state; } -const Tox_Event_Group_Voice_State *tox_events_get_group_voice_state(const Tox_Events *events, uint32_t index) -{ - uint32_t group_voice_state_index = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (group_voice_state_index > index) { - return nullptr; - } - - if (events->events[i].type == TOX_EVENT_GROUP_VOICE_STATE) { - const Tox_Event_Group_Voice_State *group_voice_state = events->events[i].data.group_voice_state; - if (group_voice_state_index == index) { - return group_voice_state; - } - ++group_voice_state_index; - } - } - - return nullptr; -} - -uint32_t tox_events_get_group_voice_state_size(const Tox_Events *events) -{ - uint32_t group_voice_state_size = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (events->events[i].type == TOX_EVENT_GROUP_VOICE_STATE) { - ++group_voice_state_size; - } - } - - return group_voice_state_size; -} - bool tox_event_group_voice_state_unpack( Tox_Event_Group_Voice_State **event, Bin_Unpack *bu, const Memory *mem) { diff --git a/external/toxcore/c-toxcore/toxcore/events/self_connection_status.c b/external/toxcore/c-toxcore/toxcore/events/self_connection_status.c index 60043a4b..945f4bb6 100644 --- a/external/toxcore/c-toxcore/toxcore/events/self_connection_status.c +++ b/external/toxcore/c-toxcore/toxcore/events/self_connection_status.c @@ -119,42 +119,6 @@ static Tox_Event_Self_Connection_Status *tox_events_add_self_connection_status(T return self_connection_status; } -const Tox_Event_Self_Connection_Status *tox_events_get_self_connection_status(const Tox_Events *events, uint32_t index) -{ - uint32_t self_connection_status_index = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (self_connection_status_index > index) { - return nullptr; - } - - if (events->events[i].type == TOX_EVENT_SELF_CONNECTION_STATUS) { - const Tox_Event_Self_Connection_Status *self_connection_status = events->events[i].data.self_connection_status; - if (self_connection_status_index == index) { - return self_connection_status; - } - ++self_connection_status_index; - } - } - - return nullptr; -} - -uint32_t tox_events_get_self_connection_status_size(const Tox_Events *events) -{ - uint32_t self_connection_status_size = 0; - const uint32_t size = tox_events_get_size(events); - - for (uint32_t i = 0; i < size; ++i) { - if (events->events[i].type == TOX_EVENT_SELF_CONNECTION_STATUS) { - ++self_connection_status_size; - } - } - - return self_connection_status_size; -} - bool tox_event_self_connection_status_unpack( Tox_Event_Self_Connection_Status **event, Bin_Unpack *bu, const Memory *mem) { diff --git a/external/toxcore/c-toxcore/toxcore/forwarding_fuzz_test.cc b/external/toxcore/c-toxcore/toxcore/forwarding_fuzz_test.cc index ce263f1e..c4fc8a2f 100644 --- a/external/toxcore/c-toxcore/toxcore/forwarding_fuzz_test.cc +++ b/external/toxcore/c-toxcore/toxcore/forwarding_fuzz_test.cc @@ -1,47 +1,87 @@ #include "forwarding.h" #include +#include #include +#include #include "../testing/fuzzing/fuzz_support.h" #include "../testing/fuzzing/fuzz_tox.h" namespace { +std::optional> prepare(Fuzz_Data &input) +{ + CONSUME_OR_RETURN_VAL(const uint8_t *ipp_packed, input, SIZE_IP_PORT, std::nullopt); + IP_Port ipp; + unpack_ip_port(&ipp, ipp_packed, SIZE_IP6, true); + + CONSUME_OR_RETURN_VAL(const uint8_t *forwarder_packed, input, SIZE_IP_PORT, std::nullopt); + IP_Port forwarder; + unpack_ip_port(&forwarder, forwarder_packed, SIZE_IP6, true); + + // 2 bytes: size of the request + CONSUME_OR_RETURN_VAL(const uint8_t *data_size_bytes, input, sizeof(uint16_t), std::nullopt); + uint16_t data_size; + std::memcpy(&data_size, data_size_bytes, sizeof(uint16_t)); + + // data bytes (max 64K) + CONSUME_OR_RETURN_VAL(const uint8_t *data, input, data_size, std::nullopt); + + return {{ipp, forwarder, data, data_size}}; +} + void TestSendForwardRequest(Fuzz_Data &input) { - const Network *ns = system_network(); // TODO(iphydf): fuzz_network - assert(ns != nullptr); - const Memory *mem = system_memory(); // TODO(iphydf): fuzz_memory - assert(mem != nullptr); + CONSUME1_OR_RETURN(const uint16_t, chain_length, input); + const uint16_t chain_keys_size = chain_length * CRYPTO_PUBLIC_KEY_SIZE; + CONSUME_OR_RETURN(const uint8_t *chain_keys, input, chain_keys_size); - with{} >> with{input, ns, mem} >> [&input](Ptr net) { - with{input} >> [net = std::move(net), &input](const IP_Port &forwarder) { - CONSUME1_OR_RETURN(const uint16_t chain_length, input); - const uint16_t chain_keys_size = chain_length * CRYPTO_PUBLIC_KEY_SIZE; - CONSUME_OR_RETURN(const uint8_t *chain_keys, input, chain_keys_size); + auto prep = prepare(input); + if (!prep.has_value()) { + return; + } + auto [ipp, forwarder, data, data_size] = prep.value(); - send_forward_request( - net.get(), &forwarder, chain_keys, chain_length, input.data, input.size); - }; - }; + // rest of the fuzz data is input for malloc and network + Fuzz_System sys(input); + + Ptr logger(logger_new(), logger_kill); + + Ptr net(new_networking_ex(logger.get(), sys.mem.get(), sys.ns.get(), &ipp.ip, + ipp.port, ipp.port + 100, nullptr), + kill_networking); + if (net == nullptr) { + return; + } + + send_forward_request(net.get(), &forwarder, chain_keys, chain_length, data, data_size); } void TestForwardReply(Fuzz_Data &input) { - const Network *ns = system_network(); // TODO(iphydf): fuzz_network - assert(ns != nullptr); - const Memory *mem = system_memory(); // TODO(iphydf): fuzz_memory - assert(mem != nullptr); + CONSUME1_OR_RETURN(const uint16_t, sendback_length, input); + CONSUME_OR_RETURN(const uint8_t *sendback, input, sendback_length); - with{} >> with{input, ns, mem} >> [&input](Ptr net) { - with{input} >> [net = std::move(net), &input](const IP_Port &forwarder) { - CONSUME1_OR_RETURN(const uint16_t sendback_length, input); - CONSUME_OR_RETURN(const uint8_t *sendback, input, sendback_length); + auto prep = prepare(input); + if (!prep.has_value()) { + return; + } + auto [ipp, forwarder, data, data_size] = prep.value(); - forward_reply(net.get(), &forwarder, sendback, sendback_length, input.data, input.size); - }; - }; + // rest of the fuzz data is input for malloc and network + Fuzz_System sys(input); + + Ptr logger(logger_new(), logger_kill); + + Ptr net(new_networking_ex(logger.get(), sys.mem.get(), sys.ns.get(), &ipp.ip, + ipp.port, ipp.port + 100, nullptr), + kill_networking); + if (net == nullptr) { + return; + } + + forward_reply(net.get(), &forwarder, sendback, sendback_length, data, data_size); } } // namespace diff --git a/external/toxcore/c-toxcore/toxcore/group.c b/external/toxcore/c-toxcore/toxcore/group.c index 0086a17e..fea80e40 100644 --- a/external/toxcore/c-toxcore/toxcore/group.c +++ b/external/toxcore/c-toxcore/toxcore/group.c @@ -684,7 +684,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, sizeof(Group_Peer) * g->numfrozen); + Group_Peer *const frozen_temp = (Group_Peer *)realloc(g->frozen, g->numfrozen * sizeof(Group_Peer)); if (frozen_temp == nullptr) { return false; @@ -725,7 +725,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, sizeof(Group_Peer) * (g->numpeers + 1)); + Group_Peer *temp = (Group_Peer *)realloc(g->group, (g->numpeers + 1) * sizeof(Group_Peer)); if (temp == nullptr) { return -1; @@ -838,7 +838,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, sizeof(Group_Peer) * (g->numpeers + 1)); + Group_Peer *temp = (Group_Peer *)realloc(g->group, (g->numpeers + 1) * sizeof(Group_Peer)); if (temp == nullptr) { return -1; @@ -936,7 +936,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, sizeof(Group_Peer) * g->numpeers); + Group_Peer *temp = (Group_Peer *)realloc(g->group, g->numpeers * sizeof(Group_Peer)); if (temp == nullptr) { return false; @@ -995,7 +995,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, sizeof(Group_Peer) * g->maxfrozen); + Group_Peer *temp = (Group_Peer *)realloc(g->frozen, g->maxfrozen * sizeof(Group_Peer)); if (temp == nullptr) { return false; @@ -1020,7 +1020,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, sizeof(Group_Peer) * (g->numfrozen + 1)); + Group_Peer *temp = (Group_Peer *)realloc(g->frozen, (g->numfrozen + 1) * sizeof(Group_Peer)); if (temp == nullptr) { return false; diff --git a/external/toxcore/c-toxcore/toxcore/group_announce.c b/external/toxcore/c-toxcore/toxcore/group_announce.c index 3308ecbd..0aa704ae 100644 --- a/external/toxcore/c-toxcore/toxcore/group_announce.c +++ b/external/toxcore/c-toxcore/toxcore/group_announce.c @@ -339,6 +339,31 @@ int gca_unpack_announces_list(const Logger *log, const uint8_t *data, uint16_t l return announces_count; } +non_null() +static GC_Announces *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)); + + if (announces == nullptr) { + return nullptr; + } + + announces->index = 0; + announces->prev_announce = nullptr; + + if (gc_announces_list->root_announces != nullptr) { + gc_announces_list->root_announces->prev_announce = announces; + } + + announces->next_announce = gc_announces_list->root_announces; + gc_announces_list->root_announces = announces; + memcpy(announces->chat_id, public_announce->chat_public_key, CHAT_ID_SIZE); + + return announces; +} + GC_Peer_Announce *gca_add_announce(const Mono_Time *mono_time, GC_Announces_List *gc_announces_list, const GC_Public_Announce *public_announce) { @@ -350,22 +375,11 @@ GC_Peer_Announce *gca_add_announce(const Mono_Time *mono_time, GC_Announces_List // No entry for this chat_id exists so we create one if (announces == nullptr) { - announces = (GC_Announces *)calloc(1, sizeof(GC_Announces)); + announces = gca_new_announces(gc_announces_list, public_announce); if (announces == nullptr) { return nullptr; } - - announces->index = 0; - announces->prev_announce = nullptr; - - if (gc_announces_list->root_announces != nullptr) { - gc_announces_list->root_announces->prev_announce = announces; - } - - announces->next_announce = gc_announces_list->root_announces; - gc_announces_list->root_announces = announces; - memcpy(announces->chat_id, public_announce->chat_public_key, CHAT_ID_SIZE); } const uint64_t cur_time = mono_time_get(mono_time); @@ -396,8 +410,7 @@ bool gca_is_valid_announce(const GC_Announce *announce) GC_Announces_List *new_gca_list(void) { - GC_Announces_List *announces_list = (GC_Announces_List *)calloc(1, sizeof(GC_Announces_List)); - return announces_list; + return (GC_Announces_List *)calloc(1, sizeof(GC_Announces_List)); } void kill_gca(GC_Announces_List *announces_list) diff --git a/external/toxcore/c-toxcore/toxcore/group_announce_fuzz_test.cc b/external/toxcore/c-toxcore/toxcore/group_announce_fuzz_test.cc index 6728e712..be9d06ec 100644 --- a/external/toxcore/c-toxcore/toxcore/group_announce_fuzz_test.cc +++ b/external/toxcore/c-toxcore/toxcore/group_announce_fuzz_test.cc @@ -11,19 +11,21 @@ namespace { void TestUnpackAnnouncesList(Fuzz_Data &input) { - CONSUME1_OR_RETURN(const uint8_t max_count, input); - std::vector announces(max_count); + CONSUME1_OR_RETURN(const uint8_t, max_count, input); + // Always allocate at least something to avoid passing nullptr to functions below. + std::vector announces(max_count + 1); // TODO(iphydf): How do we know the packed size? - CONSUME1_OR_RETURN(const uint16_t packed_size, input); + CONSUME1_OR_RETURN(const uint16_t, packed_size, input); Logger *logger = logger_new(); if (gca_unpack_announces_list(logger, input.data, input.size, announces.data(), max_count) != -1) { - std::vector packed(packed_size); + // Always allocate at least something to avoid passing nullptr to functions below. + std::vector packed(packed_size + 1); size_t processed; gca_pack_announces_list( - logger, packed.data(), packed.size(), announces.data(), announces.size(), &processed); + logger, packed.data(), packed_size, announces.data(), max_count, &processed); } logger_kill(logger); } @@ -33,12 +35,13 @@ void TestUnpackPublicAnnounce(Fuzz_Data &input) GC_Public_Announce public_announce; // TODO(iphydf): How do we know the packed size? - CONSUME1_OR_RETURN(const uint16_t packed_size, input); + CONSUME1_OR_RETURN(const uint16_t, packed_size, input); Logger *logger = logger_new(); if (gca_unpack_public_announce(logger, input.data, input.size, &public_announce) != -1) { - std::vector packed(packed_size); - gca_pack_public_announce(logger, packed.data(), packed.size(), &public_announce); + // Always allocate at least something to avoid passing nullptr to functions below. + std::vector packed(packed_size + 1); + gca_pack_public_announce(logger, packed.data(), packed_size, &public_announce); } logger_kill(logger); } @@ -58,11 +61,11 @@ void TestDoGca(Fuzz_Data &input) assert(gca != nullptr); while (input.size > 0) { - CONSUME1_OR_RETURN(const uint8_t choice, input); + CONSUME1_OR_RETURN(const uint8_t, choice, input); switch (choice) { case 0: { // Add an announce. - CONSUME1_OR_RETURN(const uint16_t length, input); + CONSUME1_OR_RETURN(const uint16_t, length, input); CONSUME_OR_RETURN(const uint8_t *data, input, length); GC_Public_Announce public_announce; if (gca_unpack_public_announce(logger.get(), data, length, &public_announce) != -1) { @@ -72,7 +75,7 @@ void TestDoGca(Fuzz_Data &input) } case 1: { // Advance the time by a number of tox_iteration_intervals. - CONSUME1_OR_RETURN(const uint8_t iterations, input); + CONSUME1_OR_RETURN(const uint8_t, iterations, input); clock += iterations * 20; // Do an iteration. do_gca(mono_time.get(), gca.get()); @@ -80,7 +83,7 @@ void TestDoGca(Fuzz_Data &input) } case 2: { // Get announces. - CONSUME1_OR_RETURN(const uint8_t max_nodes, input); + CONSUME1_OR_RETURN(const uint8_t, max_nodes, input); std::vector gc_announces(max_nodes); CONSUME_OR_RETURN(const uint8_t *chat_id, input, CHAT_ID_SIZE); CONSUME_OR_RETURN(const uint8_t *except_public_key, input, ENC_PUBLIC_KEY_SIZE); diff --git a/external/toxcore/c-toxcore/toxcore/group_chats.c b/external/toxcore/c-toxcore/toxcore/group_chats.c index e8f21371..4157b52a 100644 --- a/external/toxcore/c-toxcore/toxcore/group_chats.c +++ b/external/toxcore/c-toxcore/toxcore/group_chats.c @@ -2107,16 +2107,17 @@ static int handle_gc_tcp_relays(GC_Chat *chat, GC_Connection *gconn, const uint8 non_null() static bool send_gc_invite_request(const GC_Chat *chat, GC_Connection *gconn) { - uint16_t length = 0; + if (!chat_is_password_protected(chat)) { + return send_lossless_group_packet(chat, gconn, nullptr, 0, GP_INVITE_REQUEST); + } + uint8_t data[sizeof(uint16_t) + MAX_GC_PASSWORD_SIZE]; - if (chat_is_password_protected(chat)) { - net_pack_u16(data, chat->shared_state.password_length); - length += sizeof(uint16_t); + net_pack_u16(data, chat->shared_state.password_length); + uint16_t length = sizeof(uint16_t); - memcpy(data + length, chat->shared_state.password, MAX_GC_PASSWORD_SIZE); - length += MAX_GC_PASSWORD_SIZE; - } + memcpy(data + length, chat->shared_state.password, MAX_GC_PASSWORD_SIZE); + length += MAX_GC_PASSWORD_SIZE; return send_lossless_group_packet(chat, gconn, data, length, GP_INVITE_REQUEST); } @@ -2261,34 +2262,64 @@ FAILED_INVITE: return ret; } -/** @brief Sends a lossless packet of type and length to all confirmed peers. */ +/** @brief Sends a lossless packet of type and length to all confirmed peers. + * + * Return true if packet is successfully sent to at least one peer or the + * group is empty. + */ non_null() -static void send_gc_lossless_packet_all_peers(const GC_Chat *chat, const uint8_t *data, uint16_t length, uint8_t type) +static bool send_gc_lossless_packet_all_peers(const GC_Chat *chat, const uint8_t *data, uint16_t length, uint8_t type) { + uint32_t sent = 0; + uint32_t confirmed_peers = 0; + for (uint32_t i = 1; i < chat->numpeers; ++i) { GC_Connection *gconn = get_gc_connection(chat, i); assert(gconn != nullptr); - if (gconn->confirmed) { - send_lossless_group_packet(chat, gconn, data, length, type); + if (!gconn->confirmed) { + continue; + } + + ++confirmed_peers; + + if (send_lossless_group_packet(chat, gconn, data, length, type)) { + ++sent; } } + + return sent > 0 || confirmed_peers == 0; } -/** @brief Sends a lossy packet of type and length to all confirmed peers. */ +/** @brief Sends a lossy packet of type and length to all confirmed peers. + * + * Return true if packet is successfully sent to at least one peer or the + * group is empty. + */ non_null() -static void send_gc_lossy_packet_all_peers(const GC_Chat *chat, const uint8_t *data, uint16_t length, uint8_t type) +static bool send_gc_lossy_packet_all_peers(const GC_Chat *chat, const uint8_t *data, uint16_t length, uint8_t type) { + uint32_t sent = 0; + uint32_t confirmed_peers = 0; + for (uint32_t i = 1; i < chat->numpeers; ++i) { const GC_Connection *gconn = get_gc_connection(chat, i); assert(gconn != nullptr); - if (gconn->confirmed) { - send_lossy_group_packet(chat, gconn, data, length, type); + if (!gconn->confirmed) { + continue; + } + + ++confirmed_peers; + + if (send_lossy_group_packet(chat, gconn, data, length, type)) { + ++sent; } } + + return sent > 0 || confirmed_peers == 0; } /** @brief Creates packet with broadcast header info followed by data of length. @@ -2328,11 +2359,11 @@ static bool send_gc_broadcast_message(const GC_Chat *chat, const uint8_t *data, const uint16_t packet_len = make_gc_broadcast_header(data, length, packet, bc_type); - send_gc_lossless_packet_all_peers(chat, packet, packet_len, GP_BROADCAST); + const bool ret = send_gc_lossless_packet_all_peers(chat, packet, packet_len, GP_BROADCAST); free(packet); - return true; + return ret; } non_null() @@ -2786,9 +2817,7 @@ static bool broadcast_gc_shared_state(const GC_Chat *chat) return false; } - send_gc_lossless_packet_all_peers(chat, packet, (uint16_t)packet_len, GP_SHARED_STATE); - - return true; + return send_gc_lossless_packet_all_peers(chat, packet, (uint16_t)packet_len, GP_SHARED_STATE); } /** @brief Helper function for `do_gc_shared_state_changes()`. @@ -3309,11 +3338,11 @@ static bool broadcast_gc_sanctions_list(const GC_Chat *chat) return false; } - send_gc_lossless_packet_all_peers(chat, packet, (uint16_t)packet_len, GP_SANCTIONS_LIST); + const bool ret = send_gc_lossless_packet_all_peers(chat, packet, (uint16_t)packet_len, GP_SANCTIONS_LIST); free(packet); - return true; + return ret; } /** @brief Re-signs all sanctions list entries signed by public_sig_key and broadcasts @@ -3355,11 +3384,11 @@ static bool broadcast_gc_mod_list(const GC_Chat *chat) return false; } - send_gc_lossless_packet_all_peers(chat, packet, length, GP_MOD_LIST); + const bool ret = send_gc_lossless_packet_all_peers(chat, packet, length, GP_MOD_LIST); free(packet); - return true; + return ret; } /** @brief Sends a parting signal to the group. @@ -3716,11 +3745,11 @@ static bool broadcast_gc_topic(const GC_Chat *chat) return false; } - send_gc_lossless_packet_all_peers(chat, packet, packet_buf_size, GP_TOPIC); + const bool ret = send_gc_lossless_packet_all_peers(chat, packet, packet_buf_size, GP_TOPIC); free(packet); - return true; + return ret; } int gc_set_topic(GC_Chat *chat, const uint8_t *topic, uint16_t length) @@ -5071,13 +5100,15 @@ int gc_send_custom_packet(const GC_Chat *chat, bool lossless, const uint8_t *dat return -3; } + bool success; + if (lossless) { - send_gc_lossless_packet_all_peers(chat, data, length, GP_CUSTOM_PACKET); + success = send_gc_lossless_packet_all_peers(chat, data, length, GP_CUSTOM_PACKET); } else { - send_gc_lossy_packet_all_peers(chat, data, length, GP_CUSTOM_PACKET); + success = send_gc_lossy_packet_all_peers(chat, data, length, GP_CUSTOM_PACKET); } - return 0; + return success ? 0 : -4; } /** @brief Handles a custom packet. @@ -5295,7 +5326,7 @@ static int handle_gc_message_ack(const GC_Chat *chat, GC_Connection *gconn, cons if (gcc_encrypt_and_send_lossless_packet(chat, gconn, gconn->send_array[idx].data, gconn->send_array[idx].data_length, gconn->send_array[idx].message_id, - gconn->send_array[idx].packet_type)) { + gconn->send_array[idx].packet_type) == 0) { gconn->send_array[idx].last_send_try = tm; LOGGER_DEBUG(chat->log, "Re-sent requested packet %llu", (unsigned long long)message_id); } else { @@ -6211,6 +6242,39 @@ static bool handle_gc_lossless_packet(const GC_Session *c, GC_Chat *chat, const return true; } +non_null(1, 2, 3, 4, 6) nullable(8) +static int handle_gc_lossy_packet_decoded( + const GC_Session *c, GC_Chat *chat, GC_Connection *gconn, const GC_Peer *peer, + uint8_t packet_type, const uint8_t *data, uint16_t payload_len, void *userdata) +{ + switch (packet_type) { + case GP_MESSAGE_ACK: { + return handle_gc_message_ack(chat, gconn, data, payload_len); + } + + case GP_PING: { + return handle_gc_ping(chat, gconn, data, payload_len); + } + + case GP_INVITE_RESPONSE_REJECT: { + return handle_gc_invite_response_reject(c, chat, data, payload_len, userdata); + } + + case GP_CUSTOM_PACKET: { + return handle_gc_custom_packet(c, chat, peer, data, payload_len, false, userdata); + } + + case GP_CUSTOM_PRIVATE_PACKET: { + return handle_gc_custom_private_packet(c, chat, peer, data, payload_len, false, userdata); + } + + default: { + LOGGER_WARNING(chat->log, "Warning: handling invalid lossy group packet type 0x%02x", packet_type); + return -1; + } + } +} + /** @brief Handles lossy groupchat message packets. * * This function assumes the length has already been validated. @@ -6259,41 +6323,7 @@ static bool handle_gc_lossy_packet(const GC_Session *c, GC_Chat *chat, const uin return false; } - int ret = -1; - const uint16_t payload_len = (uint16_t)len; - - switch (packet_type) { - case GP_MESSAGE_ACK: { - ret = handle_gc_message_ack(chat, gconn, data, payload_len); - break; - } - - case GP_PING: { - ret = handle_gc_ping(chat, gconn, data, payload_len); - break; - } - - case GP_INVITE_RESPONSE_REJECT: { - ret = handle_gc_invite_response_reject(c, chat, data, payload_len, userdata); - break; - } - - case GP_CUSTOM_PACKET: { - ret = handle_gc_custom_packet(c, chat, peer, data, payload_len, false, userdata); - break; - } - - case GP_CUSTOM_PRIVATE_PACKET: { - ret = handle_gc_custom_private_packet(c, chat, peer, data, payload_len, false, userdata); - break; - } - - default: { - LOGGER_WARNING(chat->log, "Warning: handling invalid lossy group packet type 0x%02x", packet_type); - free(data); - return false; - } - } + const int ret = handle_gc_lossy_packet_decoded(c, chat, gconn, peer, packet_type, data, (uint16_t)len, userdata); free(data); @@ -7729,7 +7759,9 @@ bool gc_disconnect_from_group(const GC_Session *c, GC_Chat *chat) chat->connection_state = CS_DISCONNECTED; - send_gc_broadcast_message(chat, nullptr, 0, GM_PEER_EXIT); + if (!send_gc_broadcast_message(chat, nullptr, 0, GM_PEER_EXIT)) { + LOGGER_DEBUG(chat->log, "Failed to broadcast group exit packet"); + } for (uint32_t i = 1; i < chat->numpeers; ++i) { GC_Connection *gconn = get_gc_connection(chat, i); diff --git a/external/toxcore/c-toxcore/toxcore/group_chats.h b/external/toxcore/c-toxcore/toxcore/group_chats.h index 55fc199b..57ebe60b 100644 --- a/external/toxcore/c-toxcore/toxcore/group_chats.h +++ b/external/toxcore/c-toxcore/toxcore/group_chats.h @@ -190,6 +190,7 @@ int gc_send_private_message(const GC_Chat *chat, uint32_t peer_id, uint8_t type, * Returns -1 if the message is too long. * Returns -2 if the message pointer is NULL or length is zero. * Returns -3 if the sender has the observer role. + * Returns -4 if the packet did not successfully send to any peer. */ non_null() int gc_send_custom_packet(const GC_Chat *chat, bool lossless, const uint8_t *data, uint16_t length); diff --git a/external/toxcore/c-toxcore/toxcore/group_connection.c b/external/toxcore/c-toxcore/toxcore/group_connection.c index 2a8045a8..75663906 100644 --- a/external/toxcore/c-toxcore/toxcore/group_connection.c +++ b/external/toxcore/c-toxcore/toxcore/group_connection.c @@ -85,30 +85,48 @@ void gcc_set_recv_message_id(GC_Connection *gconn, uint64_t id) } /** @brief Puts packet data in array_entry. + * + * Requires an empty array entry to be passed, and must not modify the passed + * array entry on error. * * Return true on success. */ -non_null(1, 2) nullable(3) -static bool create_array_entry(const Mono_Time *mono_time, GC_Message_Array_Entry *array_entry, const uint8_t *data, - uint16_t length, uint8_t packet_type, uint64_t message_id) +non_null(1, 2, 3) nullable(4) +static bool create_array_entry(const Logger *log, const Mono_Time *mono_time, GC_Message_Array_Entry *array_entry, + const uint8_t *data, uint16_t length, uint8_t packet_type, uint64_t message_id) { - if (length > 0) { + if (!array_entry_is_empty(array_entry)) { + LOGGER_WARNING(log, "Failed to create array entry; entry is not empty."); + return false; + } + + if (length == 0) { + if (data != nullptr) { + LOGGER_FATAL(log, "Got non-null data with zero length (type %d)", packet_type); // should never happen + return false; + } + + array_entry->data = nullptr; + array_entry->data_length = 0; + } else { if (data == nullptr) { + LOGGER_FATAL(log, "Got null data with non-zero length (type %u)", packet_type); // should never happen return false; } - array_entry->data = (uint8_t *)malloc(sizeof(uint8_t) * length); + uint8_t *entry_data = (uint8_t *)malloc(length); - if (array_entry->data == nullptr) { + if (entry_data == nullptr) { return false; } - memcpy(array_entry->data, data, length); + memcpy(entry_data, data, length); + array_entry->data = entry_data; + array_entry->data_length = length; } const uint64_t tm = mono_time_get(mono_time); - array_entry->data_length = length; array_entry->packet_type = packet_type; array_entry->message_id = message_id; array_entry->time_added = tm; @@ -119,7 +137,7 @@ static bool create_array_entry(const Mono_Time *mono_time, GC_Message_Array_Entr /** @brief Adds data of length to gconn's send_array. * - * Returns true on success and increments gconn's send_message_id. + * Returns true and increments gconn's send_message_id on success. */ non_null(1, 2, 3) nullable(4) static bool add_to_send_array(const Logger *log, const Mono_Time *mono_time, GC_Connection *gconn, const uint8_t *data, @@ -134,13 +152,7 @@ static bool add_to_send_array(const Logger *log, const Mono_Time *mono_time, GC_ const uint16_t idx = gcc_get_array_index(gconn->send_message_id); GC_Message_Array_Entry *array_entry = &gconn->send_array[idx]; - if (!array_entry_is_empty(array_entry)) { - LOGGER_DEBUG(log, "Send array entry isn't empty"); - return false; - } - - if (!create_array_entry(mono_time, array_entry, data, length, packet_type, gconn->send_message_id)) { - LOGGER_WARNING(log, "Failed to create array entry"); + if (!create_array_entry(log, mono_time, array_entry, data, length, packet_type, gconn->send_message_id)) { return false; } @@ -159,8 +171,15 @@ int gcc_send_lossless_packet(const GC_Chat *chat, GC_Connection *gconn, const ui return -1; } - if (!gcc_encrypt_and_send_lossless_packet(chat, gconn, data, length, message_id, packet_type)) { - LOGGER_DEBUG(chat->log, "Failed to send payload: (type: 0x%02x, length: %d)", packet_type, length); + // If the packet fails to wrap/encrypt, we remove it from the send array, since trying to-resend + // the same bad packet probably won't help much. Otherwise we don't care if it doesn't successfully + // send through the wire as it will keep retrying until the connection times out. + if (gcc_encrypt_and_send_lossless_packet(chat, gconn, data, length, message_id, packet_type) == -1) { + const uint16_t idx = gcc_get_array_index(message_id); + GC_Message_Array_Entry *array_entry = &gconn->send_array[idx]; + clear_array_entry(array_entry); + gconn->send_message_id = message_id; + LOGGER_ERROR(chat->log, "Failed to encrypt payload: (type: 0x%02x, length: %d)", packet_type, length); return -2; } @@ -331,17 +350,7 @@ static bool store_in_recv_array(const Logger *log, const Mono_Time *mono_time, G const uint16_t idx = gcc_get_array_index(message_id); GC_Message_Array_Entry *ary_entry = &gconn->recv_array[idx]; - if (!array_entry_is_empty(ary_entry)) { - LOGGER_DEBUG(log, "Recv array is not empty"); - return false; - } - - if (!create_array_entry(mono_time, ary_entry, data, length, packet_type, message_id)) { - LOGGER_WARNING(log, "Failed to create array entry"); - return false; - } - - return true; + return create_array_entry(log, mono_time, ary_entry, data, length, packet_type, message_id); } /** @@ -392,10 +401,9 @@ static uint16_t reassemble_packet(const Logger *log, GC_Connection *gconn, uint8 return 0; } - assert(*payload == nullptr); - *payload = (uint8_t *)malloc(packet_length); + uint8_t *tmp_payload = (uint8_t *)malloc(packet_length); - if (*payload == nullptr) { + if (tmp_payload == nullptr) { LOGGER_ERROR(log, "Failed to allocate %u bytes for payload buffer", packet_length); return 0; } @@ -409,12 +417,15 @@ static uint16_t reassemble_packet(const Logger *log, GC_Connection *gconn, uint8 entry = &gconn->recv_array[i]; assert(processed + entry->data_length <= packet_length); - memcpy(*payload + processed, entry->data, entry->data_length); + memcpy(tmp_payload + processed, entry->data, entry->data_length); processed += entry->data_length; clear_array_entry(entry); } + assert(*payload == nullptr); + *payload = tmp_payload; + return processed; } @@ -612,7 +623,7 @@ bool gcc_send_packet(const GC_Chat *chat, const GC_Connection *gconn, const uint return ret == 0 || direct_send_attempt; } -bool gcc_encrypt_and_send_lossless_packet(const GC_Chat *chat, const GC_Connection *gconn, const uint8_t *data, +int gcc_encrypt_and_send_lossless_packet(const GC_Chat *chat, const GC_Connection *gconn, const uint8_t *data, 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); @@ -620,7 +631,7 @@ bool gcc_encrypt_and_send_lossless_packet(const GC_Chat *chat, const GC_Connecti if (packet == nullptr) { LOGGER_ERROR(chat->log, "Failed to allocate memory for packet buffer"); - return false; + return -1; } const int enc_len = group_packet_wrap( @@ -630,18 +641,18 @@ bool gcc_encrypt_and_send_lossless_packet(const GC_Chat *chat, const GC_Connecti if (enc_len < 0) { LOGGER_ERROR(chat->log, "Failed to wrap packet (type: 0x%02x, error: %d)", packet_type, enc_len); free(packet); - return false; + return -1; } if (!gcc_send_packet(chat, gconn, packet, (uint16_t)enc_len)) { LOGGER_DEBUG(chat->log, "Failed to send packet (type: 0x%02x, enc_len: %d)", packet_type, enc_len); free(packet); - return false; + return -2; } free(packet); - return true; + return 0; } void gcc_make_session_shared_key(GC_Connection *gconn, const uint8_t *sender_pk) diff --git a/external/toxcore/c-toxcore/toxcore/group_connection.h b/external/toxcore/c-toxcore/toxcore/group_connection.h index 5987f010..fa7a3c94 100644 --- a/external/toxcore/c-toxcore/toxcore/group_connection.h +++ b/external/toxcore/c-toxcore/toxcore/group_connection.h @@ -143,11 +143,11 @@ bool gcc_send_packet(const GC_Chat *chat, const GC_Connection *gconn, const uint /** @brief Sends a lossless packet to `gconn` comprised of `data` of size `length`. * * This function will add the packet to the lossless send array, encrypt/wrap it using the - * shared key associated with `gconn`, and send it over the wire. + * shared key associated with `gconn`, and try to send it over the wire. * - * Return 0 on success. + * Return 0 if the packet was successfully encrypted and added to the send array. * Return -1 if the packet couldn't be added to the send array. - * Return -2 if the packet failed to be encrypted or failed to send. + * Return -2 if the packet failed to be wrapped or encrypted. */ non_null(1, 2) nullable(3) int gcc_send_lossless_packet(const GC_Chat *chat, GC_Connection *gconn, const uint8_t *data, uint16_t length, @@ -172,11 +172,13 @@ bool gcc_send_lossless_packet_fragments(const GC_Chat *chat, GC_Connection *gcon * * This function does not add the packet to the send array. * - * Return true on success. + * Return 0 on success. + * Return -1 if packet wrapping and encryption fails. + * Return -2 if the packet fails to send. */ non_null(1, 2) nullable(3) -bool gcc_encrypt_and_send_lossless_packet(const GC_Chat *chat, const GC_Connection *gconn, const uint8_t *data, - uint16_t length, uint64_t message_id, uint8_t packet_type); +int gcc_encrypt_and_send_lossless_packet(const GC_Chat *chat, const GC_Connection *gconn, const uint8_t *data, + uint16_t length, uint64_t message_id, uint8_t packet_type); /** @brief Called when a peer leaves the group. */ non_null() diff --git a/external/toxcore/c-toxcore/toxcore/group_moderation.c b/external/toxcore/c-toxcore/toxcore/group_moderation.c index 90a95e17..0da113ec 100644 --- a/external/toxcore/c-toxcore/toxcore/group_moderation.c +++ b/external/toxcore/c-toxcore/toxcore/group_moderation.c @@ -59,14 +59,16 @@ 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) { - tmp_list[i] = (uint8_t *)malloc(sizeof(uint8_t) * MOD_LIST_ENTRY_SIZE); + uint8_t *entry = (uint8_t *)malloc(MOD_LIST_ENTRY_SIZE); - if (tmp_list[i] == nullptr) { + if (entry == nullptr) { free_uint8_t_pointer_array(moderation->mem, tmp_list, i); return -1; } - memcpy(tmp_list[i], &data[i * MOD_LIST_ENTRY_SIZE], MOD_LIST_ENTRY_SIZE); + memcpy(entry, &data[i * MOD_LIST_ENTRY_SIZE], MOD_LIST_ENTRY_SIZE); + tmp_list[i] = entry; + unpacked_len += MOD_LIST_ENTRY_SIZE; } @@ -208,13 +210,15 @@ bool mod_list_add_entry(Moderation *moderation, const uint8_t *mod_data) moderation->mod_list = tmp_list; - tmp_list[moderation->num_mods] = (uint8_t *)malloc(sizeof(uint8_t) * MOD_LIST_ENTRY_SIZE); + uint8_t *entry = (uint8_t *)malloc(MOD_LIST_ENTRY_SIZE); - if (tmp_list[moderation->num_mods] == nullptr) { + if (entry == nullptr) { return false; } - memcpy(tmp_list[moderation->num_mods], mod_data, MOD_LIST_ENTRY_SIZE); + memcpy(entry, mod_data, MOD_LIST_ENTRY_SIZE); + + tmp_list[moderation->num_mods] = entry; ++moderation->num_mods; return true; diff --git a/external/toxcore/c-toxcore/toxcore/group_moderation_fuzz_test.cc b/external/toxcore/c-toxcore/toxcore/group_moderation_fuzz_test.cc index c5f46f34..6adfd9ab 100644 --- a/external/toxcore/c-toxcore/toxcore/group_moderation_fuzz_test.cc +++ b/external/toxcore/c-toxcore/toxcore/group_moderation_fuzz_test.cc @@ -6,7 +6,7 @@ namespace { void TestModListUnpack(Fuzz_Data &input) { - CONSUME1_OR_RETURN(const uint16_t num_mods, input); + CONSUME1_OR_RETURN(const uint16_t, num_mods, input); Moderation mods{system_memory()}; mod_list_unpack(&mods, input.data, input.size, num_mods); mod_list_cleanup(&mods); diff --git a/external/toxcore/c-toxcore/toxcore/list.c b/external/toxcore/c-toxcore/toxcore/list.c index 3f6ad228..1096befc 100644 --- a/external/toxcore/c-toxcore/toxcore/list.c +++ b/external/toxcore/c-toxcore/toxcore/list.c @@ -123,7 +123,7 @@ static bool resize(BS_List *list, uint32_t new_size) list->data = data; - int *ids = (int *)realloc(list->ids, sizeof(int) * new_size); + int *ids = (int *)realloc(list->ids, new_size * sizeof(int)); if (ids == nullptr) { return false; diff --git a/external/toxcore/c-toxcore/toxcore/mono_time.c b/external/toxcore/c-toxcore/toxcore/mono_time.c index a3e5baa0..cf2044af 100644 --- a/external/toxcore/c-toxcore/toxcore/mono_time.c +++ b/external/toxcore/c-toxcore/toxcore/mono_time.c @@ -119,18 +119,20 @@ Mono_Time *mono_time_new(const Memory *mem, mono_time_current_time_cb *current_t } #ifndef ESP_PLATFORM - mono_time->time_update_lock = (pthread_rwlock_t *)mem_alloc(mem, sizeof(pthread_rwlock_t)); + pthread_rwlock_t *rwlock = (pthread_rwlock_t *)mem_alloc(mem, sizeof(pthread_rwlock_t)); - if (mono_time->time_update_lock == nullptr) { + if (rwlock == nullptr) { mem_delete(mem, mono_time); return nullptr; } - if (pthread_rwlock_init(mono_time->time_update_lock, nullptr) != 0) { - mem_delete(mem, mono_time->time_update_lock); + if (pthread_rwlock_init(rwlock, nullptr) != 0) { + mem_delete(mem, rwlock); mem_delete(mem, mono_time); return nullptr; } + + mono_time->time_update_lock = rwlock; #endif mono_time_set_current_time_callback(mono_time, current_time_callback, user_data); diff --git a/external/toxcore/c-toxcore/toxcore/net_crypto.c b/external/toxcore/c-toxcore/toxcore/net_crypto.c index 52bc2e84..1f53e696 100644 --- a/external/toxcore/c-toxcore/toxcore/net_crypto.c +++ b/external/toxcore/c-toxcore/toxcore/net_crypto.c @@ -1868,25 +1868,26 @@ 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)); + + if (mutex == nullptr) { + pthread_mutex_unlock(&c->connections_mutex); + return -1; + } + + if (pthread_mutex_init(mutex, nullptr) != 0) { + mem_delete(c->mem, mutex); + pthread_mutex_unlock(&c->connections_mutex); + return -1; + } + // Memsetting float/double to 0 is non-portable, so we explicitly set them to 0 - c->crypto_connections[id].packet_recv_rate = 0; - c->crypto_connections[id].packet_send_rate = 0; - c->crypto_connections[id].last_packets_left_rem = 0; - c->crypto_connections[id].packet_send_rate_requested = 0; - c->crypto_connections[id].last_packets_left_requested_rem = 0; - c->crypto_connections[id].mutex = (pthread_mutex_t *)mem_alloc(c->mem, sizeof(pthread_mutex_t)); - - if (c->crypto_connections[id].mutex == nullptr) { - pthread_mutex_unlock(&c->connections_mutex); - return -1; - } - - if (pthread_mutex_init(c->crypto_connections[id].mutex, nullptr) != 0) { - mem_delete(c->mem, c->crypto_connections[id].mutex); - pthread_mutex_unlock(&c->connections_mutex); - return -1; - } - + c->crypto_connections[id].packet_recv_rate = 0.0; + c->crypto_connections[id].packet_send_rate = 0.0; + c->crypto_connections[id].last_packets_left_rem = 0.0; + c->crypto_connections[id].packet_send_rate_requested = 0.0; + c->crypto_connections[id].last_packets_left_requested_rem = 0.0; + c->crypto_connections[id].mutex = mutex; c->crypto_connections[id].status = CRYPTO_CONN_NO_CONNECTION; } @@ -2022,13 +2023,14 @@ 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) { - New_Connection n_c; - n_c.cookie = (uint8_t *)mem_balloc(c->mem, COOKIE_LENGTH); + uint8_t *cookie = (uint8_t *)mem_balloc(c->mem, COOKIE_LENGTH); - if (n_c.cookie == nullptr) { + if (cookie == nullptr) { return -1; } + New_Connection n_c = {{{{0}}}}; + n_c.cookie = cookie; n_c.source = *source; n_c.cookie_length = COOKIE_LENGTH; diff --git a/external/toxcore/c-toxcore/toxcore/network.c b/external/toxcore/c-toxcore/toxcore/network.c index b583fd2f..748f1c78 100644 --- a/external/toxcore/c-toxcore/toxcore/network.c +++ b/external/toxcore/c-toxcore/toxcore/network.c @@ -1796,12 +1796,14 @@ int32_t net_getipport(const Memory *mem, const char *node, IP_Port **res, int to #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION if ((true)) { - *res = (IP_Port *)mem_alloc(mem, sizeof(IP_Port)); - assert(*res != nullptr); - IP_Port *ip_port = *res; + IP_Port *ip_port = (IP_Port *)mem_alloc(mem, sizeof(IP_Port)); + if (ip_port == nullptr) { + abort(); + } ip_port->ip.ip.v4.uint32 = net_htonl(0x7F000003); // 127.0.0.3 ip_port->ip.family = *make_tox_family(AF_INET); + *res = ip_port; return 1; } #endif @@ -1838,14 +1840,15 @@ int32_t net_getipport(const Memory *mem, const char *node, IP_Port **res, int to return 0; } - *res = (IP_Port *)mem_valloc(mem, count, sizeof(IP_Port)); + IP_Port *ip_port = (IP_Port *)mem_valloc(mem, count, sizeof(IP_Port)); - if (*res == nullptr) { + if (ip_port == nullptr) { freeaddrinfo(infos); + *res = nullptr; return -1; } - IP_Port *ip_port = *res; + *res = ip_port; for (struct addrinfo *cur = infos; cur != nullptr; cur = cur->ai_next) { if (cur->ai_socktype && type > 0 && cur->ai_socktype != type) { diff --git a/external/toxcore/c-toxcore/toxcore/network_test_util.cc b/external/toxcore/c-toxcore/toxcore/network_test_util.cc new file mode 100644 index 00000000..82f3b872 --- /dev/null +++ b/external/toxcore/c-toxcore/toxcore/network_test_util.cc @@ -0,0 +1,66 @@ +#include "network_test_util.hh" + +#include + +IP_Port increasing_ip_port::operator()() +{ + IP_Port ip_port; + ip_port.ip.family = net_family_ipv4(); + ip_port.ip.ip.v4.uint8[0] = 192; + ip_port.ip.ip.v4.uint8[1] = 168; + ip_port.ip.ip.v4.uint8[2] = 0; + ip_port.ip.ip.v4.uint8[3] = start_; + ip_port.port = random_u16(rng_); + ++start_; + return ip_port; +} + +IP_Port random_ip_port(const Random *rng) +{ + IP_Port ip_port; + ip_port.ip.family = net_family_ipv4(); + ip_port.ip.ip.v4.uint8[0] = 192; + ip_port.ip.ip.v4.uint8[1] = 168; + ip_port.ip.ip.v4.uint8[2] = 0; + ip_port.ip.ip.v4.uint8[3] = random_u08(rng); + ip_port.port = random_u16(rng); + return ip_port; +} + +bool operator==(Family const &a, Family const &b) { return a.value == b.value; } + +bool operator==(IP4 const &a, IP4 const &b) { return a.uint32 == b.uint32; } + +bool operator==(IP6 const &a, IP6 const &b) +{ + return a.uint64[0] == b.uint64[0] && a.uint64[1] == b.uint64[1]; +} + +bool operator==(IP const &a, IP const &b) +{ + if (!(a.family == b.family)) { + return false; + } + + if (net_family_is_ipv4(a.family)) { + return a.ip.v4 == b.ip.v4; + } else { + return a.ip.v6 == b.ip.v6; + } +} + +bool operator==(IP_Port const &a, IP_Port const &b) { return a.ip == b.ip && a.port == b.port; } + +std::ostream &operator<<(std::ostream &out, IP const &v) +{ + Ip_Ntoa ip_str; + out << '"' << net_ip_ntoa(&v, &ip_str) << '"'; + return out; +} + +std::ostream &operator<<(std::ostream &out, IP_Port const &v) +{ + return out << "IP_Port{\n" + << " ip = " << v.ip << ",\n" + << " port = " << std::dec << std::setw(0) << v.port << " }"; +} diff --git a/external/toxcore/c-toxcore/toxcore/network_test_util.hh b/external/toxcore/c-toxcore/toxcore/network_test_util.hh new file mode 100644 index 00000000..86663251 --- /dev/null +++ b/external/toxcore/c-toxcore/toxcore/network_test_util.hh @@ -0,0 +1,39 @@ +#ifndef C_TOXCORE_TOXCORE_NETWORK_TEST_UTIL_H +#define C_TOXCORE_TOXCORE_NETWORK_TEST_UTIL_H + +#include + +#include "crypto_core.h" +#include "network.h" +#include "test_util.hh" + +template <> +struct Deleter : Function_Deleter { }; + +IP_Port random_ip_port(const Random *rng); + +class increasing_ip_port { + uint8_t start_; + const Random *rng_; + +public: + explicit increasing_ip_port(uint8_t start, const Random *rng) + : start_(start) + , rng_(rng) + { + } + + IP_Port operator()(); +}; + +bool operator==(Family const &a, Family const &b); + +bool operator==(IP4 const &a, IP4 const &b); +bool operator==(IP6 const &a, IP6 const &b); +bool operator==(IP const &a, IP const &b); +bool operator==(IP_Port const &a, IP_Port const &b); + +std::ostream &operator<<(std::ostream &out, IP const &v); +std::ostream &operator<<(std::ostream &out, IP_Port const &v); + +#endif // C_TOXCORE_TOXCORE_NETWORK_TEST_UTIL_H diff --git a/external/toxcore/c-toxcore/toxcore/ping_array.c b/external/toxcore/c-toxcore/toxcore/ping_array.c index 7c0ce87e..6ba1cf68 100644 --- a/external/toxcore/c-toxcore/toxcore/ping_array.c +++ b/external/toxcore/c-toxcore/toxcore/ping_array.c @@ -49,14 +49,15 @@ Ping_Array *ping_array_new(const Memory *mem, uint32_t size, uint32_t timeout) return nullptr; } - empty_array->mem = mem; - empty_array->entries = (Ping_Array_Entry *)mem_valloc(mem, size, sizeof(Ping_Array_Entry)); + Ping_Array_Entry *entries = (Ping_Array_Entry *)mem_valloc(mem, size, sizeof(Ping_Array_Entry)); - if (empty_array->entries == nullptr) { + if (entries == nullptr) { mem_delete(mem, empty_array); return nullptr; } + empty_array->mem = mem; + empty_array->entries = entries; empty_array->last_deleted = 0; empty_array->last_added = 0; empty_array->total_size = size; @@ -115,13 +116,16 @@ uint64_t ping_array_add(Ping_Array *array, const Mono_Time *mono_time, const Ran clear_entry(array, index); } - array->entries[index].data = (uint8_t *)mem_balloc(array->mem, length); + uint8_t *entry_data = (uint8_t *)mem_balloc(array->mem, length); - if (array->entries[index].data == nullptr) { + if (entry_data == nullptr) { + array->entries[index].data = nullptr; return 0; } - memcpy(array->entries[index].data, data, length); + memcpy(entry_data, data, length); + + array->entries[index].data = entry_data; array->entries[index].length = length; array->entries[index].ping_time = mono_time_get(mono_time); ++array->last_added; diff --git a/external/toxcore/c-toxcore/toxcore/shared_key_cache.c b/external/toxcore/c-toxcore/toxcore/shared_key_cache.c index 8f8f6c1b..b52869b7 100644 --- a/external/toxcore/c-toxcore/toxcore/shared_key_cache.c +++ b/external/toxcore/c-toxcore/toxcore/shared_key_cache.c @@ -68,16 +68,19 @@ Shared_Key_Cache *shared_key_cache_new(const Logger *log, const Mono_Time *mono_ res->mem = mem; res->log = log; res->keys_per_slot = keys_per_slot; + // 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; - res->keys = (Shared_Key *)mem_valloc(mem, cache_size, sizeof(Shared_Key)); + Shared_Key *keys = (Shared_Key *)mem_valloc(mem, cache_size, sizeof(Shared_Key)); - if (res->keys == nullptr) { + if (keys == nullptr) { mem_delete(mem, res); return nullptr; } - crypto_memlock(res->keys, cache_size * sizeof(Shared_Key)); + crypto_memlock(keys, cache_size * sizeof(Shared_Key)); + + res->keys = keys; return res; } diff --git a/external/toxcore/c-toxcore/toxcore/test_util.cc b/external/toxcore/c-toxcore/toxcore/test_util.cc new file mode 100644 index 00000000..5566ced4 --- /dev/null +++ b/external/toxcore/c-toxcore/toxcore/test_util.cc @@ -0,0 +1 @@ +#include "test_util.hh" diff --git a/external/toxcore/c-toxcore/toxcore/test_util.hh b/external/toxcore/c-toxcore/toxcore/test_util.hh new file mode 100644 index 00000000..9bd35fac --- /dev/null +++ b/external/toxcore/c-toxcore/toxcore/test_util.hh @@ -0,0 +1,68 @@ +#ifndef C_TOXCORE_TOXCORE_TEST_UTIL_H +#define C_TOXCORE_TOXCORE_TEST_UTIL_H + +#include +#include +#include +#include + +template +struct Function_Deleter { + void operator()(T *ptr) const { Delete(ptr); } +}; + +// No default deleter, because we want to catch when we forget to specialise this one. +template +struct Deleter; + +template +using Ptr = std::unique_ptr>; + +template +struct Method; + +template +struct Method { + template + static R invoke(void *self, Args... args) + { + return (static_cast(self)->*M)(self, args...); + } +}; + +template +std::array to_array(T const (&arr)[N]) +{ + std::array stdarr; + std::copy(arr, arr + N, stdarr.begin()); + return stdarr; +} + +template +auto array_of(T &&make, Args... args) +{ + std::array::type, N> arr; + for (auto &elem : arr) { + elem = make(args...); + } + return arr; +} + +template +auto vector_of(std::size_t n, T &&make, Args... args) +{ + std::vector::type> vec; + for (std::size_t i = 0; i < n; ++i) { + vec.push_back(make(args...)); + } + return vec; +} + +template +Container sorted(Container arr, Less less) +{ + std::sort(arr.begin(), arr.end(), less); + return arr; +} + +#endif // C_TOXCORE_TOXCORE_TEST_UTIL_H diff --git a/external/toxcore/c-toxcore/toxcore/test_util_test.cc b/external/toxcore/c-toxcore/toxcore/test_util_test.cc new file mode 100644 index 00000000..2ef2e944 --- /dev/null +++ b/external/toxcore/c-toxcore/toxcore/test_util_test.cc @@ -0,0 +1,64 @@ +#include "test_util.hh" + +#include +#include + +#include + +#include "crypto_core_test_util.hh" + +namespace { + +using ::testing::Each; +using ::testing::Eq; + +TEST(CryptoCoreTestUtil, RandomBytesDoesNotTouchZeroSizeArray) +{ + const Test_Random rng; + std::array bytes{}; + for (uint32_t i = 0; i < 100; ++i) { + random_bytes(rng, bytes.data(), 0); + ASSERT_THAT(bytes, Each(Eq(0x00))); + } +} + +TEST(CryptoCoreTestUtil, RandomBytesFillsEntireArray) +{ + const Test_Random rng; + std::array bytes{}; + + for (uint32_t size = 1; size < bytes.size(); ++size) { + bool const success = [&]() { + // Try a few times. There ought to be a non-zero byte in our randomness at + // some point. + for (uint32_t i = 0; i < 100; ++i) { + random_bytes(rng, bytes.data(), bytes.size()); + if (bytes[size - 1] != 0x00) { + return true; + } + } + return false; + }(); + ASSERT_TRUE(success); + } +} + +TEST(CryptoCoreTestUtil, RandomBytesDoesNotBufferOverrun) +{ + const Test_Random rng; + + std::array bytes{}; + + // Try a few times. It should never overrun. + for (uint32_t i = 0; i < 100; ++i) { + for (uint32_t diff = 1; diff < sizeof(uint64_t); ++diff) { + bytes = {}; + random_bytes(rng, bytes.data(), bytes.size() - diff); + // All bytes not in the range we want to write should be 0. + ASSERT_THAT(std::vector(bytes.begin() + (bytes.size() - diff), bytes.end()), + Each(Eq(0x00))); + } + } +} + +} // namespace diff --git a/external/toxcore/c-toxcore/toxcore/tox.c b/external/toxcore/c-toxcore/toxcore/tox.c index 00914ec1..4c6665d3 100644 --- a/external/toxcore/c-toxcore/toxcore/tox.c +++ b/external/toxcore/c-toxcore/toxcore/tox.c @@ -796,21 +796,21 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error) } if (tox_options_get_experimental_thread_safety(opts)) { - tox->mutex = (pthread_mutex_t *)mem_alloc(sys->mem, sizeof(pthread_mutex_t)); + pthread_mutex_t *mutex = (pthread_mutex_t *)mem_alloc(sys->mem, sizeof(pthread_mutex_t)); - if (tox->mutex == nullptr) { + if (mutex == nullptr) { SET_ERROR_PARAMETER(error, TOX_ERR_NEW_MALLOC); mem_delete(sys->mem, tox); tox_options_free(default_options); return nullptr; } - pthread_mutexattr_t attr; - pthread_mutexattr_init(&attr); pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); - pthread_mutex_init(tox->mutex, &attr); + pthread_mutex_init(mutex, &attr); + + tox->mutex = mutex; } else { tox->mutex = nullptr; } @@ -4021,6 +4021,11 @@ bool tox_group_send_custom_packet(const Tox *tox, uint32_t group_number, bool lo SET_ERROR_PARAMETER(error, TOX_ERR_GROUP_SEND_CUSTOM_PACKET_PERMISSIONS); return false; } + + case -4: { + SET_ERROR_PARAMETER(error, TOX_ERR_GROUP_SEND_CUSTOM_PACKET_FAIL_SEND); + return false; + } } /* can't happen */ diff --git a/external/toxcore/c-toxcore/toxcore/tox.h b/external/toxcore/c-toxcore/toxcore/tox.h index 566bd576..183ec1b0 100644 --- a/external/toxcore/c-toxcore/toxcore/tox.h +++ b/external/toxcore/c-toxcore/toxcore/tox.h @@ -4736,6 +4736,12 @@ typedef enum Tox_Err_Group_Send_Custom_Packet { */ TOX_ERR_GROUP_SEND_CUSTOM_PACKET_DISCONNECTED, + /** + * The packet did not successfully send to any peer. This often indicates + * a connection issue on the sender's side. + */ + TOX_ERR_GROUP_SEND_CUSTOM_PACKET_FAIL_SEND, + } Tox_Err_Group_Send_Custom_Packet; const char *tox_err_group_send_custom_packet_to_string(Tox_Err_Group_Send_Custom_Packet value); diff --git a/external/toxcore/c-toxcore/toxcore/tox_api.c b/external/toxcore/c-toxcore/toxcore/tox_api.c index d92c98b0..bf5b4a09 100644 --- a/external/toxcore/c-toxcore/toxcore/tox_api.c +++ b/external/toxcore/c-toxcore/toxcore/tox_api.c @@ -1259,6 +1259,9 @@ const char *tox_err_group_send_custom_packet_to_string(Tox_Err_Group_Send_Custom case TOX_ERR_GROUP_SEND_CUSTOM_PACKET_DISCONNECTED: return "TOX_ERR_GROUP_SEND_CUSTOM_PACKET_DISCONNECTED"; + + case TOX_ERR_GROUP_SEND_CUSTOM_PACKET_FAIL_SEND: + return "TOX_ERR_GROUP_SEND_CUSTOM_PACKET_FAIL_SEND"; } return ""; diff --git a/external/toxcore/c-toxcore/toxcore/tox_dispatch.c b/external/toxcore/c-toxcore/toxcore/tox_dispatch.c index 63b4eb02..bbf09493 100644 --- a/external/toxcore/c-toxcore/toxcore/tox_dispatch.c +++ b/external/toxcore/c-toxcore/toxcore/tox_dispatch.c @@ -278,7 +278,7 @@ void tox_events_callback_group_moderation( dispatch->group_moderation_callback = callback; } -non_null(1, 2, 3) nullable(4) +non_null(1, 2) nullable(3, 4) static void tox_dispatch_invoke_event(const Tox_Dispatch *dispatch, const Tox_Event *event, Tox *tox, void *user_data) { switch (event->type) { diff --git a/external/toxcore/c-toxcore/toxcore/tox_events.h b/external/toxcore/c-toxcore/toxcore/tox_events.h index 40108489..05edd4f4 100644 --- a/external/toxcore/c-toxcore/toxcore/tox_events.h +++ b/external/toxcore/c-toxcore/toxcore/tox_events.h @@ -497,125 +497,6 @@ typedef struct Tox_Events Tox_Events; uint32_t tox_events_get_size(const Tox_Events *events); const Tox_Event *tox_events_get(const Tox_Events *events, uint32_t index); -uint32_t tox_events_get_conference_connected_size(const Tox_Events *events); -uint32_t tox_events_get_conference_invite_size(const Tox_Events *events); -uint32_t tox_events_get_conference_message_size(const Tox_Events *events); -uint32_t tox_events_get_conference_peer_list_changed_size(const Tox_Events *events); -uint32_t tox_events_get_conference_peer_name_size(const Tox_Events *events); -uint32_t tox_events_get_conference_title_size(const Tox_Events *events); -uint32_t tox_events_get_file_chunk_request_size(const Tox_Events *events); -uint32_t tox_events_get_file_recv_chunk_size(const Tox_Events *events); -uint32_t tox_events_get_file_recv_control_size(const Tox_Events *events); -uint32_t tox_events_get_file_recv_size(const Tox_Events *events); -uint32_t tox_events_get_friend_connection_status_size(const Tox_Events *events); -uint32_t tox_events_get_friend_lossless_packet_size(const Tox_Events *events); -uint32_t tox_events_get_friend_lossy_packet_size(const Tox_Events *events); -uint32_t tox_events_get_friend_message_size(const Tox_Events *events); -uint32_t tox_events_get_friend_name_size(const Tox_Events *events); -uint32_t tox_events_get_friend_read_receipt_size(const Tox_Events *events); -uint32_t tox_events_get_friend_request_size(const Tox_Events *events); -uint32_t tox_events_get_friend_status_message_size(const Tox_Events *events); -uint32_t tox_events_get_friend_status_size(const Tox_Events *events); -uint32_t tox_events_get_friend_typing_size(const Tox_Events *events); -uint32_t tox_events_get_self_connection_status_size(const Tox_Events *events); -uint32_t tox_events_get_group_peer_name_size(const Tox_Events *events); -uint32_t tox_events_get_group_peer_status_size(const Tox_Events *events); -uint32_t tox_events_get_group_topic_size(const Tox_Events *events); -uint32_t tox_events_get_group_privacy_state_size(const Tox_Events *events); -uint32_t tox_events_get_group_voice_state_size(const Tox_Events *events); -uint32_t tox_events_get_group_topic_lock_size(const Tox_Events *events); -uint32_t tox_events_get_group_peer_limit_size(const Tox_Events *events); -uint32_t tox_events_get_group_password_size(const Tox_Events *events); -uint32_t tox_events_get_group_message_size(const Tox_Events *events); -uint32_t tox_events_get_group_private_message_size(const Tox_Events *events); -uint32_t tox_events_get_group_custom_packet_size(const Tox_Events *events); -uint32_t tox_events_get_group_custom_private_packet_size(const Tox_Events *events); -uint32_t tox_events_get_group_invite_size(const Tox_Events *events); -uint32_t tox_events_get_group_peer_join_size(const Tox_Events *events); -uint32_t tox_events_get_group_peer_exit_size(const Tox_Events *events); -uint32_t tox_events_get_group_self_join_size(const Tox_Events *events); -uint32_t tox_events_get_group_join_fail_size(const Tox_Events *events); -uint32_t tox_events_get_group_moderation_size(const Tox_Events *events); - -const Tox_Event_Conference_Connected *tox_events_get_conference_connected( - const Tox_Events *events, uint32_t index); -const Tox_Event_Conference_Invite *tox_events_get_conference_invite( - const Tox_Events *events, uint32_t index); -const Tox_Event_Conference_Message *tox_events_get_conference_message( - const Tox_Events *events, uint32_t index); -const Tox_Event_Conference_Peer_List_Changed *tox_events_get_conference_peer_list_changed( - const Tox_Events *events, uint32_t index); -const Tox_Event_Conference_Peer_Name *tox_events_get_conference_peer_name( - const Tox_Events *events, uint32_t index); -const Tox_Event_Conference_Title *tox_events_get_conference_title( - const Tox_Events *events, uint32_t index); -const Tox_Event_File_Chunk_Request *tox_events_get_file_chunk_request( - const Tox_Events *events, uint32_t index); -const Tox_Event_File_Recv_Chunk *tox_events_get_file_recv_chunk( - const Tox_Events *events, uint32_t index); -const Tox_Event_File_Recv_Control *tox_events_get_file_recv_control( - const Tox_Events *events, uint32_t index); -const Tox_Event_File_Recv *tox_events_get_file_recv( - const Tox_Events *events, uint32_t index); -const Tox_Event_Friend_Connection_Status *tox_events_get_friend_connection_status( - const Tox_Events *events, uint32_t index); -const Tox_Event_Friend_Lossless_Packet *tox_events_get_friend_lossless_packet( - const Tox_Events *events, uint32_t index); -const Tox_Event_Friend_Lossy_Packet *tox_events_get_friend_lossy_packet( - const Tox_Events *events, uint32_t index); -const Tox_Event_Friend_Message *tox_events_get_friend_message( - const Tox_Events *events, uint32_t index); -const Tox_Event_Friend_Name *tox_events_get_friend_name( - const Tox_Events *events, uint32_t index); -const Tox_Event_Friend_Read_Receipt *tox_events_get_friend_read_receipt( - const Tox_Events *events, uint32_t index); -const Tox_Event_Friend_Request *tox_events_get_friend_request( - const Tox_Events *events, uint32_t index); -const Tox_Event_Friend_Status_Message *tox_events_get_friend_status_message( - const Tox_Events *events, uint32_t index); -const Tox_Event_Friend_Status *tox_events_get_friend_status( - const Tox_Events *events, uint32_t index); -const Tox_Event_Friend_Typing *tox_events_get_friend_typing( - const Tox_Events *events, uint32_t index); -const Tox_Event_Self_Connection_Status *tox_events_get_self_connection_status( - const Tox_Events *events, uint32_t index); -const Tox_Event_Group_Peer_Name *tox_events_get_group_peer_name( - const Tox_Events *events, uint32_t index); -const Tox_Event_Group_Peer_Status *tox_events_get_group_peer_status( - const Tox_Events *events, uint32_t index); -const Tox_Event_Group_Topic *tox_events_get_group_topic( - const Tox_Events *events, uint32_t index); -const Tox_Event_Group_Privacy_State *tox_events_get_group_privacy_state( - const Tox_Events *events, uint32_t index); -const Tox_Event_Group_Voice_State *tox_events_get_group_voice_state( - const Tox_Events *events, uint32_t index); -const Tox_Event_Group_Topic_Lock *tox_events_get_group_topic_lock( - const Tox_Events *events, uint32_t index); -const Tox_Event_Group_Peer_Limit *tox_events_get_group_peer_limit( - const Tox_Events *events, uint32_t index); -const Tox_Event_Group_Password *tox_events_get_group_password( - const Tox_Events *events, uint32_t index); -const Tox_Event_Group_Message *tox_events_get_group_message( - const Tox_Events *events, uint32_t index); -const Tox_Event_Group_Private_Message *tox_events_get_group_private_message( - const Tox_Events *events, uint32_t index); -const Tox_Event_Group_Custom_Packet *tox_events_get_group_custom_packet( - const Tox_Events *events, uint32_t index); -const Tox_Event_Group_Custom_Private_Packet *tox_events_get_group_custom_private_packet( - const Tox_Events *events, uint32_t index); -const Tox_Event_Group_Invite *tox_events_get_group_invite( - const Tox_Events *events, uint32_t index); -const Tox_Event_Group_Peer_Join *tox_events_get_group_peer_join( - const Tox_Events *events, uint32_t index); -const Tox_Event_Group_Peer_Exit *tox_events_get_group_peer_exit( - const Tox_Events *events, uint32_t index); -const Tox_Event_Group_Self_Join *tox_events_get_group_self_join( - const Tox_Events *events, uint32_t index); -const Tox_Event_Group_Join_Fail *tox_events_get_group_join_fail( - const Tox_Events *events, uint32_t index); -const Tox_Event_Group_Moderation *tox_events_get_group_moderation( - const Tox_Events *events, uint32_t index); - /** * Initialise the events recording system. * diff --git a/external/toxcore/c-toxcore/toxcore/tox_events_fuzz_test.cc b/external/toxcore/c-toxcore/toxcore/tox_events_fuzz_test.cc index ab1acb64..993cf812 100644 --- a/external/toxcore/c-toxcore/toxcore/tox_events_fuzz_test.cc +++ b/external/toxcore/c-toxcore/toxcore/tox_events_fuzz_test.cc @@ -1,10 +1,12 @@ #include "tox_events.h" +#include #include #include #include #include "../testing/fuzzing/fuzz_support.h" +#include "tox_dispatch.h" namespace { @@ -27,12 +29,59 @@ void TestUnpack(Fuzz_Data data) // rest of the fuzz data is input for malloc Fuzz_System sys{data}; + Tox_Dispatch *dispatch = tox_dispatch_new(nullptr); + assert(dispatch != nullptr); + + auto ignore = [](Tox *tox, auto *event, void *user_data) {}; + tox_events_callback_conference_connected(dispatch, ignore); + tox_events_callback_conference_invite(dispatch, ignore); + tox_events_callback_conference_message(dispatch, ignore); + tox_events_callback_conference_peer_list_changed(dispatch, ignore); + tox_events_callback_conference_peer_name(dispatch, ignore); + tox_events_callback_conference_title(dispatch, ignore); + tox_events_callback_file_chunk_request(dispatch, ignore); + tox_events_callback_file_recv(dispatch, ignore); + tox_events_callback_file_recv_chunk(dispatch, ignore); + tox_events_callback_file_recv_control(dispatch, ignore); + tox_events_callback_friend_connection_status(dispatch, ignore); + tox_events_callback_friend_lossless_packet(dispatch, ignore); + tox_events_callback_friend_lossy_packet(dispatch, ignore); + tox_events_callback_friend_message(dispatch, ignore); + tox_events_callback_friend_name(dispatch, ignore); + tox_events_callback_friend_read_receipt(dispatch, ignore); + tox_events_callback_friend_request(dispatch, ignore); + tox_events_callback_friend_status(dispatch, ignore); + tox_events_callback_friend_status_message(dispatch, ignore); + tox_events_callback_friend_typing(dispatch, ignore); + tox_events_callback_self_connection_status(dispatch, ignore); + tox_events_callback_group_peer_name(dispatch, ignore); + tox_events_callback_group_peer_status(dispatch, ignore); + tox_events_callback_group_topic(dispatch, ignore); + tox_events_callback_group_privacy_state(dispatch, ignore); + tox_events_callback_group_voice_state(dispatch, ignore); + tox_events_callback_group_topic_lock(dispatch, ignore); + tox_events_callback_group_peer_limit(dispatch, ignore); + tox_events_callback_group_password(dispatch, ignore); + tox_events_callback_group_message(dispatch, ignore); + tox_events_callback_group_private_message(dispatch, ignore); + tox_events_callback_group_custom_packet(dispatch, ignore); + tox_events_callback_group_custom_private_packet(dispatch, ignore); + tox_events_callback_group_invite(dispatch, ignore); + tox_events_callback_group_peer_join(dispatch, ignore); + tox_events_callback_group_peer_exit(dispatch, ignore); + tox_events_callback_group_self_join(dispatch, ignore); + tox_events_callback_group_join_fail(dispatch, ignore); + tox_events_callback_group_moderation(dispatch, ignore); + Tox_Events *events = tox_events_load(sys.sys.get(), events_data, events_size); if (events) { std::vector packed(tox_events_bytes_size(events)); tox_events_get_bytes(events, packed.data()); + + tox_dispatch_invoke(dispatch, events, nullptr, nullptr); } tox_events_free(events); + tox_dispatch_free(dispatch); } } // namespace diff --git a/external/toxcore/c-toxcore/toxcore/tox_events_test.cc b/external/toxcore/c-toxcore/toxcore/tox_events_test.cc index 5de29b5e..749d6768 100644 --- a/external/toxcore/c-toxcore/toxcore/tox_events_test.cc +++ b/external/toxcore/c-toxcore/toxcore/tox_events_test.cc @@ -33,7 +33,7 @@ TEST(ToxEvents, UnpackEmptyArrayCreatesEmptyEvents) std::array data{0x90}; // empty msgpack array Tox_Events *events = tox_events_load(&sys, data.data(), data.size()); ASSERT_NE(events, nullptr); - EXPECT_EQ(tox_events_get_conference_connected_size(events), 0); + EXPECT_EQ(tox_events_get_size(events), 0); tox_events_free(events); } diff --git a/external/toxcore/c-toxcore/toxcore/util_test.cc b/external/toxcore/c-toxcore/toxcore/util_test.cc index 47bf2587..aca278e3 100644 --- a/external/toxcore/c-toxcore/toxcore/util_test.cc +++ b/external/toxcore/c-toxcore/toxcore/util_test.cc @@ -3,13 +3,13 @@ #include #include "crypto_core.h" +#include "crypto_core_test_util.hh" namespace { TEST(Util, TwoRandomIdsAreNotEqual) { - const Random *rng = system_random(); - ASSERT_NE(rng, nullptr); + Test_Random rng; uint8_t pk1[CRYPTO_PUBLIC_KEY_SIZE]; uint8_t sk1[CRYPTO_SECRET_KEY_SIZE]; uint8_t pk2[CRYPTO_PUBLIC_KEY_SIZE]; @@ -23,8 +23,7 @@ TEST(Util, TwoRandomIdsAreNotEqual) TEST(Util, IdCopyMakesKeysEqual) { - const Random *rng = system_random(); - ASSERT_NE(rng, nullptr); + Test_Random rng; uint8_t pk1[CRYPTO_PUBLIC_KEY_SIZE]; uint8_t sk1[CRYPTO_SECRET_KEY_SIZE]; uint8_t pk2[CRYPTO_PUBLIC_KEY_SIZE] = {0};