Skip to content

Commit 425216d

Browse files
committed
fix: Correct a use-after-free and fix some memory leaks.
Also: use `find_package` to find gtest. This fixes the coverage build to include unit tests.
1 parent 4b1cfa3 commit 425216d

24 files changed

+143
-156
lines changed

.github/scripts/flags-clang.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ add_cxx_flag -Wno-c++98-compat-pedantic
6565
add_cxx_flag -Wno-c99-extensions
6666
# We're C-compatible, so use C style casts.
6767
add_cxx_flag -Wno-old-style-cast
68+
# GTest does this.
69+
add_cxx_flag -Wno-global-constructors
6870

6971
# Downgrade to warning so we still see it.
7072
add_flag -Wno-error=unreachable-code

.github/scripts/flags-coverage.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ add_flag --coverage
1414
add_c_flag -fno-inline -fno-omit-frame-pointer
1515

1616
# Show useful stack traces on crash.
17-
add_flag -fsanitize=undefined -fno-sanitize-recover=all
17+
add_flag -fsanitize=undefined -fno-sanitize-recover=all -D_DEBUG
1818

1919
# In test code (_test.cc and libgtest), throw away all debug information.
2020
# We only care about stack frames inside toxcore (which is C). Without this,

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ jobs:
133133
with:
134134
submodules: recursive
135135
- name: Build, test, and upload coverage
136-
run: .github/scripts/coverage-linux
136+
run: other/docker/coverage/run
137137

138138
build-android:
139139
runs-on: ubuntu-latest

CMakeLists.txt

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
#
1515
################################################################################
1616

17-
cmake_minimum_required(VERSION 3.5)
18-
cmake_policy(VERSION 3.5)
17+
cmake_minimum_required(VERSION 3.16)
18+
cmake_policy(VERSION 3.16)
1919
project(toxcore)
2020

2121
list(APPEND CMAKE_MODULE_PATH ${toxcore_SOURCE_DIR}/cmake)
@@ -76,6 +76,7 @@ if(APPLE)
7676
endif()
7777

7878
enable_testing()
79+
find_package(GTest)
7980

8081
set(CMAKE_MACOSX_RPATH ON)
8182

@@ -419,23 +420,32 @@ install_module(toxcore DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/tox)
419420
#
420421
################################################################################
421422

422-
include(CompileGTest)
423+
function(unit_test subdir target)
424+
add_executable(unit_${target}_test ${subdir}/${target}_test.cc)
425+
target_link_modules(unit_${target}_test toxcore)
426+
target_link_libraries(unit_${target}_test GTest::GTest GTest::Main)
427+
set_target_properties(unit_${target}_test PROPERTIES COMPILE_FLAGS "${TEST_CXX_FLAGS}")
428+
add_test(NAME ${target} COMMAND ${CROSSCOMPILING_EMULATOR} unit_${target}_test)
429+
set_property(TEST ${target} PROPERTY ENVIRONMENT "LLVM_PROFILE_FILE=${target}.profraw")
430+
endfunction()
423431

424432
# The actual unit tests follow.
425433
#
426-
unit_test(toxav ring_buffer)
427-
unit_test(toxav rtp)
428-
unit_test(toxcore DHT)
429-
unit_test(toxcore bin_pack)
430-
unit_test(toxcore crypto_core)
431-
unit_test(toxcore group_announce)
432-
unit_test(toxcore group_moderation)
433-
unit_test(toxcore list)
434-
unit_test(toxcore mem)
435-
unit_test(toxcore mono_time)
436-
unit_test(toxcore ping_array)
437-
unit_test(toxcore tox)
438-
unit_test(toxcore util)
434+
if(GTEST_FOUND)
435+
unit_test(toxav ring_buffer)
436+
unit_test(toxav rtp)
437+
unit_test(toxcore DHT)
438+
unit_test(toxcore bin_pack)
439+
unit_test(toxcore crypto_core)
440+
unit_test(toxcore group_announce)
441+
unit_test(toxcore group_moderation)
442+
unit_test(toxcore list)
443+
unit_test(toxcore mem)
444+
unit_test(toxcore mono_time)
445+
unit_test(toxcore ping_array)
446+
unit_test(toxcore tox)
447+
unit_test(toxcore util)
448+
endif()
439449

440450
add_subdirectory(testing)
441451

auto_tests/announce_test.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,13 @@ static void test_store_data(void)
6161
ck_assert(log != nullptr);
6262
logger_callback_log(log, print_debug_logger, nullptr, nullptr);
6363
Mono_Time *mono_time = mono_time_new(mem, nullptr, nullptr);
64+
ck_assert(mono_time != nullptr);
6465
Networking_Core *net = new_networking_no_udp(log, mem, ns);
66+
ck_assert(net != nullptr);
6567
DHT *dht = new_dht(log, mem, rng, ns, mono_time, net, true, true);
68+
ck_assert(dht != nullptr);
6669
Forwarding *forwarding = new_forwarding(log, rng, mono_time, dht);
70+
ck_assert(forwarding != nullptr);
6771
Announcements *announce = new_announcements(log, mem, rng, mono_time, forwarding);
6872
ck_assert(announce != nullptr);
6973

auto_tests/invalid_tcp_proxy_test.c

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,8 @@
77
#include "auto_test_support.h"
88
#include "check_compat.h"
99

10-
static uint8_t const key[] = {
11-
0x15, 0xE9, 0xC3, 0x09, 0xCF, 0xCB, 0x79, 0xFD,
12-
0xDF, 0x0E, 0xBA, 0x05, 0x7D, 0xAB, 0xB4, 0x9F,
13-
0xE1, 0x5F, 0x38, 0x03, 0xB1, 0xBF, 0xF0, 0x65,
14-
0x36, 0xAE, 0x2E, 0x5B, 0xA5, 0xE4, 0x69, 0x0E,
15-
};
16-
17-
// Try to bootstrap for 30 seconds.
18-
#define NUM_ITERATIONS (unsigned)(30.0 / (ITERATION_INTERVAL / 1000.0))
10+
// Try to bootstrap for 20 seconds.
11+
#define NUM_ITERATIONS (unsigned)(20.0 / (ITERATION_INTERVAL / 1000.0))
1912

2013
int main(void)
2114
{
@@ -24,13 +17,12 @@ int main(void)
2417
struct Tox_Options *opts = tox_options_new(nullptr);
2518
tox_options_set_udp_enabled(opts, false);
2619
tox_options_set_proxy_type(opts, TOX_PROXY_TYPE_SOCKS5);
27-
tox_options_set_proxy_host(opts, "localhost");
20+
tox_options_set_proxy_host(opts, "127.0.0.1");
2821
tox_options_set_proxy_port(opts, 51724);
2922
Tox *tox = tox_new_log(opts, nullptr, nullptr);
3023
tox_options_free(opts);
3124

32-
tox_add_tcp_relay(tox, "tox.ngc.zone", 33445, key, nullptr);
33-
tox_bootstrap(tox, "tox.ngc.zone", 33445, key, nullptr);
25+
bootstrap_tox_live_network(tox, true);
3426

3527
printf("Waiting for connection...\n");
3628

auto_tests/invalid_udp_proxy_test.c

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,8 @@
77
#include "auto_test_support.h"
88
#include "check_compat.h"
99

10-
static uint8_t const key[] = {
11-
0x15, 0xE9, 0xC3, 0x09, 0xCF, 0xCB, 0x79, 0xFD,
12-
0xDF, 0x0E, 0xBA, 0x05, 0x7D, 0xAB, 0xB4, 0x9F,
13-
0xE1, 0x5F, 0x38, 0x03, 0xB1, 0xBF, 0xF0, 0x65,
14-
0x36, 0xAE, 0x2E, 0x5B, 0xA5, 0xE4, 0x69, 0x0E,
15-
};
16-
17-
// Try to bootstrap for 30 seconds.
18-
#define NUM_ITERATIONS (unsigned)(30.0 / (ITERATION_INTERVAL / 1000.0))
10+
// Try to bootstrap for 20 seconds.
11+
#define NUM_ITERATIONS (unsigned)(20.0 / (ITERATION_INTERVAL / 1000.0))
1912

2013
int main(void)
2114
{
@@ -24,13 +17,12 @@ int main(void)
2417
struct Tox_Options *opts = tox_options_new(nullptr);
2518
tox_options_set_udp_enabled(opts, true);
2619
tox_options_set_proxy_type(opts, TOX_PROXY_TYPE_SOCKS5);
27-
tox_options_set_proxy_host(opts, "localhost");
20+
tox_options_set_proxy_host(opts, "127.0.0.1");
2821
tox_options_set_proxy_port(opts, 51724);
2922
Tox *tox = tox_new_log(opts, nullptr, nullptr);
3023
tox_options_free(opts);
3124

32-
tox_add_tcp_relay(tox, "tox.ngc.zone", 33445, key, nullptr);
33-
tox_bootstrap(tox, "tox.ngc.zone", 33445, key, nullptr);
25+
bootstrap_tox_live_network(tox, true);
3426

3527
printf("Waiting for connection...");
3628

auto_tests/send_message_test.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ static void send_message_test(AutoTox *autotoxes)
4242

4343
const size_t msgs_len = tox_max_message_length() + 1;
4444
uint8_t *msgs = (uint8_t *)malloc(msgs_len);
45+
ck_assert(msgs != nullptr);
4546
memset(msgs, MESSAGE_FILLER, msgs_len);
4647

4748
Tox_Err_Friend_Send_Message errm;

cmake/CompileGTest.cmake

Lines changed: 0 additions & 62 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
f117aa0e2cf3f1b42f7e38beec55cb970025692c5753b1159aa22c6e52281393 /usr/local/bin/tox-bootstrapd
1+
7923791feaca748f570f81dd79556c7763fa2f7611e3790129fe44ffa95cf916 /usr/local/bin/tox-bootstrapd

0 commit comments

Comments
 (0)