Squashed 'external/toxcore/c-toxcore/' changes from 8f0d505f9a..6d634674a9

6d634674a9 cleanup: Remove old type-ordered event getters.
d1d48d1dfc feat: add ngc events
994ffecc6b refactor: Make event dispatch ordered by receive time.
812f931d5f fix: Make sure there's enough space for CONSUME1 in fuzzers.
50f1b30fa9 test: Add fuzz tests to the coverage run.
df76f5cf47 chore: Move from gcov to llvm source-based coverage.
072e3beb3f fix: issues with packet broadcast error reporting
6b6718e4d2 cleanup: Make group packet entry creation less error-prone
5b9c420ce1 refactor: packet broadcast functions now return errors
af4cb31028 refactor: Use `operator==` for equality tests of `Node_format`.
9592d590cf refactor(test): Slightly nicer C++ interface to tox Random.
c66e10fb7a refactor: Minor refactoring of get_close_nodes functions.
ebc9643862 fix: don't pass garbage data buffer to packet send functions
32b68cffca cleanup: Some more test cleanups, removing overly smart code.
0426624dcb refactor: Assign malloc return to a local variable first.
afc38f2458 test: Add more unit tests for `add_to_list`.
05ce5c1ab9 test: Add "infer" CI check to github, remove from circle.
REVERT: 8f0d505f9a feat: add ngc events
REVERT: 9b8216e70c refactor: Make event dispatch ordered by receive time.

git-subtree-dir: external/toxcore/c-toxcore
git-subtree-split: 6d634674a929edb0ab70689dcbcb195b3547be13
This commit is contained in:
Green Sky 2024-01-12 21:30:48 +01:00
parent 9ace11a0e2
commit 8eb4892b49
126 changed files with 1556 additions and 2484 deletions

View File

@ -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:

View File

@ -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"

View File

@ -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

View File

@ -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

1
.gitignore vendored
View File

@ -93,4 +93,5 @@ cscope.files
# rpm
tox.spec
/infer
.idea/

View File

@ -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()

View File

@ -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)

View File

@ -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

View File

@ -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;
}

View File

@ -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

View File

@ -1,4 +0,0 @@
---
build:
docker:
web: other/bootstrap_daemon/websocket/Dockerfile

View File

@ -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.

View File

@ -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")

View File

@ -10,7 +10,7 @@ run() {
"${CPPFLAGS[@]}" \
"${LDFLAGS[@]}" \
"$@" \
-std=c++11 \
-std=c++17 \
-Werror \
-Weverything \
-Wno-alloca \

View File

@ -9,7 +9,7 @@ run() {
clang++ --analyze amalgamation.cc \
"${CPPFLAGS[@]}" \
"$@" \
-std=c++11
-std=c++20
}
. other/analysis/variants.sh

View File

@ -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 '$*'"

View File

@ -11,7 +11,7 @@ run() {
"${CPPFLAGS[@]}" \
"${LDFLAGS[@]}" \
"$@" \
-std=c++11 \
-std=c++17 \
-fdiagnostics-color=always \
-Wall \
-Wextra \

View File

@ -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"

View File

@ -1 +1 @@
8fadc6fd894bb8f60fd53d51dcd51ff24e62b41ae404d3921e0247b9337c9a30 /usr/local/bin/tox-bootstrapd
08fe68095b88c142e0c7f3369c5f297f1377e219d0e0f0ff6f30ca94d76fca98 /usr/local/bin/tox-bootstrapd

View File

@ -7,6 +7,7 @@ RUN apt-get update && \
clang \
cmake \
libconfig-dev \
libgmock-dev \
libgtest-dev \
libopus-dev \
libsodium-dev \

View File

@ -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

View File

@ -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/

View File

@ -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

View File

@ -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")

View File

@ -1,149 +0,0 @@
#define _GNU_SOURCE
#include "mallocfail.h"
#include <dlfcn.h>
#include <errno.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
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);
}

View File

@ -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/

View File

@ -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.

5
other/docker/infer/run Executable file
View File

@ -0,0 +1,5 @@
#!/bin/sh
set -eux
BUILD=infer
docker build -t "toxchat/c-toxcore:$BUILD" -f "other/docker/$BUILD/Dockerfile" .

View File

@ -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 \
\

View File

@ -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

View File

@ -237,10 +237,11 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT
f << " " << event_name_l << "->" << 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<EventT
f << " tox_events_add(events, &event);\n";
f << " return " << event_name_l << ";\n}\n\n";
// get
f << "const Tox_Event_" << event_name << " *tox_events_get_" << event_name_l << "(const Tox_Events *events, uint32_t index)\n{\n";
f << " uint32_t " << event_name_l << "_index = 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 (" << event_name_l << "_index > 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";

View File

@ -10,3 +10,6 @@ sonar.sources=.
# Encoding of the source code.
sonar.sourceEncoding=UTF-8
# More precise Python version.
sonar.python.version=3.11

View File

@ -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)

View File

@ -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);
}

View File

@ -8,6 +8,7 @@
#include <fstream>
#include <vector>
#include "../../toxcore/crypto_core.h"
#include "../../toxcore/tox.h"
#include "../../toxcore/tox_dispatch.h"
#include "../../toxcore/tox_events.h"

View File

@ -30,6 +30,16 @@ struct Network_Addr {
size_t size;
};
System::System(std::unique_ptr<Tox_System> in_sys, std::unique_ptr<Memory> in_mem,
std::unique_ptr<Network> in_ns, std::unique_ptr<Random> 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 <typename F>
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;
}

View File

@ -7,11 +7,12 @@
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <memory>
#include <vector>
#include <unordered_map>
#include <utility>
#include <vector>
#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()
struct Consumer {
Fuzz_Data &fd;
template <typename T>
operator T()
{
const uint8_t val = data[0];
++data;
--size;
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) { \
#define CONSUME1_OR_RETURN(TYPE, NAME, INPUT) \
if (INPUT.size < sizeof(TYPE)) { \
return; \
} \
DECL = INPUT.consume1()
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) { \
#define CONSUME1_OR_RETURN_VAL(TYPE, NAME, INPUT, VAL) \
if (INPUT.size < sizeof(TYPE)) { \
return VAL; \
} \
DECL = INPUT.consume1()
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 <typename Arg, typename... Args>
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 <typename... Args>
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>(args)...);
}
@ -127,6 +143,10 @@ struct System {
std::unique_ptr<Network> ns;
std::unique_ptr<Random> rng;
System(std::unique_ptr<Tox_System> sys, std::unique_ptr<Memory> mem,
std::unique_ptr<Network> ns, std::unique_ptr<Random> rng);
System(System &&);
// Not inline because sizeof of the above 2 structs is not known everywhere.
~System();

View File

@ -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 <cassert>
#include <memory>
#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 <typename T>
using Ptr = std::unique_ptr<T, void (*)(T *)>;
/** @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 <typename T>
struct with;
/** @brief Construct a Logger without logging callback.
*/
template <>
struct with<Logger> {
template <typename F>
void operator>>(F &&f)
{
Ptr<Logger> 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<IP_Port> {
Fuzz_Data &input_;
template <typename F>
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<Logger>{} >> with<Networking_Core>{input, ns, mem} >> ...` to construct
* a logger and pass it to the Networking_Core constructor function.
*/
template <>
struct with<Networking_Core> {
Fuzz_Data &input_;
const Network *ns_;
const Memory *mem_;
Ptr<Logger> logger_{nullptr, logger_kill};
friend with operator>>(with<Logger> f, with self)
{
f >> [&self](Ptr<Logger> logger) { self.logger_ = std::move(logger); };
return self;
}
template <typename F>
void operator>>(F &&f)
{
with<IP_Port>{input_} >> [&f, this](const IP_Port &ipp) {
Ptr<Networking_Core> 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

View File

@ -1,6 +1,7 @@
#include <cassert>
#include <cstdio>
#include "../../toxcore/crypto_core.h"
#include "../../toxcore/tox.h"
#include "../../toxcore/tox_dispatch.h"
#include "../../toxcore/tox_events.h"

View File

@ -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);

View File

@ -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];
}

View File

@ -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);

View File

@ -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",
],

View File

@ -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,
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,
uint32_t *num_nodes_ptr, bool is_lan,
bool want_announce)
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,27 +852,43 @@ 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,
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,
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);
}
@ -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;
}

View File

@ -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,17 +384,19 @@ 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,
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);

View File

@ -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];

View File

@ -1,15 +1,26 @@
#include "DHT.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <algorithm>
#include <array>
#include <cstring>
#include <random>
#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<uint8_t, CRYPTO_PUBLIC_KEY_SIZE>;
using ::testing::Each;
using ::testing::ElementsAre;
using ::testing::Eq;
using ::testing::PrintToString;
using ::testing::UnorderedElementsAre;
using SecretKey = std::array<uint8_t, CRYPTO_SECRET_KEY_SIZE>;
struct KeyPair {
@ -19,39 +30,34 @@ struct KeyPair {
explicit KeyPair(const Random *rng) { crypto_new_keypair(rng, pk.data(), sk.data()); }
};
template <typename T, size_t N>
std::array<T, N> to_array(T const (&arr)[N])
TEST(IdClosest, KeyIsClosestToItself)
{
std::array<T, N> 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);
@ -68,23 +74,22 @@ TEST(IdClosest, DistanceIsCommutative)
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<Node_format, 4> 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<Node_format> 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_format> 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<Networking_Core> 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> 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);
}

29
toxcore/DHT_test_util.cc Normal file
View File

@ -0,0 +1,29 @@
#include "DHT_test_util.hh"
#include <cstring>
#include <iomanip>
#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 << " }";
}

18
toxcore/DHT_test_util.hh Normal file
View File

@ -0,0 +1,18 @@
#ifndef C_TOXCORE_TOXCORE_DHT_TEST_UTIL_H
#define C_TOXCORE_TOXCORE_DHT_TEST_UTIL_H
#include <iosfwd>
#include "DHT.h"
#include "test_util.hh"
template <>
struct Deleter<DHT> : Function_Deleter<DHT, kill_dht> { };
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

View File

@ -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;

View File

@ -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;

View File

@ -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) {

View File

@ -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;
}

View File

@ -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;

View File

@ -6,13 +6,13 @@
#include <array>
#include <vector>
#include "crypto_core_test_util.hh"
#include "util.h"
namespace {
using HmacKey = std::array<uint8_t, CRYPTO_HMAC_KEY_SIZE>;
using Hmac = std::array<uint8_t, CRYPTO_HMAC_SIZE>;
using PublicKey = std::array<uint8_t, CRYPTO_PUBLIC_KEY_SIZE>;
using SecretKey = std::array<uint8_t, CRYPTO_SECRET_KEY_SIZE>;
using ExtPublicKey = std::array<uint8_t, EXT_PUBLIC_KEY_SIZE>;
using ExtSecretKey = std::array<uint8_t, EXT_SECRET_KEY_SIZE>;
@ -21,8 +21,7 @@ using Nonce = std::array<uint8_t, CRYPTO_NONCE_SIZE>;
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());

View File

@ -0,0 +1,39 @@
#include "crypto_core_test_util.hh"
#include <cstring>
#include <iomanip>
Random_Funcs const Random_Class::vtable = {
Method<crypto_random_bytes_cb, Random_Class>::invoke<&Random_Class::random_bytes>,
Method<crypto_random_uniform_cb, Random_Class>::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<uint32_t> 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;
}

View File

@ -0,0 +1,88 @@
#ifndef C_TOXCORE_TOXCORE_CRYPTO_CORE_TEST_UTIL_H
#define C_TOXCORE_TOXCORE_CRYPTO_CORE_TEST_UTIL_H
#include <algorithm>
#include <array>
#include <iosfwd>
#include <random>
#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<uint8_t, CRYPTO_PUBLIC_KEY_SIZE> {
using Base = std::array<uint8_t, CRYPTO_PUBLIC_KEY_SIZE>;
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<uint8_t, CRYPTO_PUBLIC_KEY_SIZE> const &arr)
{
std::copy(arr.begin(), arr.end(), begin());
}
PublicKey(std::initializer_list<uint8_t> 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

View File

@ -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)
{

View File

@ -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)
{

View File

@ -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)
{

View File

@ -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)
{

View File

@ -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)
{

View File

@ -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)
{

View File

@ -22,17 +22,20 @@ 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) {
return state;
}
*events = (Tox_Events) {
nullptr
};
state->events = events;
state->events->mem = state->mem;
}
return state;
}

View File

@ -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)
{

View File

@ -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)
{

View File

@ -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)
{

View File

@ -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)
{

View File

@ -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)
{

View File

@ -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)
{

View File

@ -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)
{

View File

@ -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)
{

View File

@ -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)
{

View File

@ -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)
{

View File

@ -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)
{

View File

@ -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)
{

View File

@ -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)
{

View File

@ -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)
{

View File

@ -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)
{

View File

@ -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)
{

View File

@ -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)
{

View File

@ -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)
{

View File

@ -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)
{

View File

@ -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)
{

View File

@ -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)
{

View File

@ -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)
{

View File

@ -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)
{

View File

@ -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)
{

View File

@ -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)
{

View File

@ -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)
{

View File

@ -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)
{

View File

@ -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)
{

View File

@ -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)
{

View File

@ -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)
{

View File

@ -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)
{

View File

@ -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)
{

View File

@ -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)
{

View File

@ -1,47 +1,87 @@
#include "forwarding.h"
#include <cassert>
#include <cstring>
#include <memory>
#include <optional>
#include "../testing/fuzzing/fuzz_support.h"
#include "../testing/fuzzing/fuzz_tox.h"
namespace {
std::optional<std::tuple<IP_Port, IP_Port, const uint8_t *, size_t>> 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);
with<Logger>{} >> with<Networking_Core>{input, ns, mem} >> [&input](Ptr<Networking_Core> net) {
with<IP_Port>{input} >> [net = std::move(net), &input](const IP_Port &forwarder) {
CONSUME1_OR_RETURN(const uint16_t chain_length, input);
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);
send_forward_request(
net.get(), &forwarder, chain_keys, chain_length, input.data, input.size);
};
};
auto prep = prepare(input);
if (!prep.has_value()) {
return;
}
auto [ipp, forwarder, data, data_size] = prep.value();
// rest of the fuzz data is input for malloc and network
Fuzz_System sys(input);
Ptr<Logger> logger(logger_new(), logger_kill);
Ptr<Networking_Core> 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);
with<Logger>{} >> with<Networking_Core>{input, ns, mem} >> [&input](Ptr<Networking_Core> net) {
with<IP_Port>{input} >> [net = std::move(net), &input](const IP_Port &forwarder) {
CONSUME1_OR_RETURN(const uint16_t sendback_length, input);
CONSUME1_OR_RETURN(const uint16_t, sendback_length, input);
CONSUME_OR_RETURN(const uint8_t *sendback, input, sendback_length);
forward_reply(net.get(), &forwarder, sendback, sendback_length, input.data, input.size);
};
};
auto prep = prepare(input);
if (!prep.has_value()) {
return;
}
auto [ipp, forwarder, data, data_size] = prep.value();
// rest of the fuzz data is input for malloc and network
Fuzz_System sys(input);
Ptr<Logger> logger(logger_new(), logger_kill);
Ptr<Networking_Core> 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

View File

@ -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;

View File

@ -339,18 +339,12 @@ int gca_unpack_announces_list(const Logger *log, const uint8_t *data, uint16_t l
return announces_count;
}
GC_Peer_Announce *gca_add_announce(const Mono_Time *mono_time, GC_Announces_List *gc_announces_list,
non_null()
static GC_Announces *gca_new_announces(
GC_Announces_List *gc_announces_list,
const GC_Public_Announce *public_announce)
{
if (gc_announces_list == nullptr || public_announce == nullptr) {
return nullptr;
}
GC_Announces *announces = get_announces_by_chat_id(gc_announces_list, public_announce->chat_public_key);
// No entry for this chat_id exists so we create one
if (announces == nullptr) {
announces = (GC_Announces *)calloc(1, sizeof(GC_Announces));
GC_Announces *announces = (GC_Announces *)calloc(1, sizeof(GC_Announces));
if (announces == nullptr) {
return nullptr;
@ -366,6 +360,26 @@ GC_Peer_Announce *gca_add_announce(const Mono_Time *mono_time, GC_Announces_List
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)
{
if (gc_announces_list == nullptr || public_announce == nullptr) {
return nullptr;
}
GC_Announces *announces = get_announces_by_chat_id(gc_announces_list, public_announce->chat_public_key);
// No entry for this chat_id exists so we create one
if (announces == nullptr) {
announces = gca_new_announces(gc_announces_list, public_announce);
if (announces == nullptr) {
return nullptr;
}
}
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)

Some files were not shown because too many files have changed in this diff Show More