From 88b90c82259f86470cf6eba8684e8d9b4cd61bc3 Mon Sep 17 00:00:00 2001 From: iphydf Date: Sun, 3 May 2020 01:09:06 +0100 Subject: Fix a bug in savedata loading when malloc fails. Also added a bunch of asserts to tests where they don't check allocs. --- .restyled.yaml | 5 +++++ auto_tests/TCP_test.c | 1 + auto_tests/conference_invite_merge_test.c | 2 ++ auto_tests/conference_peer_nick_test.c | 1 + auto_tests/conference_test.c | 2 ++ auto_tests/encryptsave_test.c | 1 + auto_tests/file_saving_test.c | 8 ++++++-- other/astyle/format-source | 8 ++++---- other/bootstrap_daemon/docker/tox-bootstrapd.sha256 | 2 +- toxav/toxav.c | 8 ++++---- toxcore/DHT.c | 6 ++++++ 11 files changed, 33 insertions(+), 11 deletions(-) create mode 100644 .restyled.yaml diff --git a/.restyled.yaml b/.restyled.yaml new file mode 100644 index 00000000..722ff510 --- /dev/null +++ b/.restyled.yaml @@ -0,0 +1,5 @@ +--- +restylers: + - astyle: + arguments: ["--options=other/astyle/astylerc"] + - prettier-yaml diff --git a/auto_tests/TCP_test.c b/auto_tests/TCP_test.c index 9206e265..8a109317 100644 --- a/auto_tests/TCP_test.c +++ b/auto_tests/TCP_test.c @@ -185,6 +185,7 @@ struct sec_TCP_con { static struct sec_TCP_con *new_TCP_con(TCP_Server *tcp_s, Mono_Time *mono_time) { struct sec_TCP_con *sec_c = (struct sec_TCP_con *)malloc(sizeof(struct sec_TCP_con)); + ck_assert(sec_c != nullptr); Socket sock = net_socket(net_family_ipv6, TOX_SOCK_STREAM, TOX_PROTO_TCP); IP_Port ip_port_loopback; diff --git a/auto_tests/conference_invite_merge_test.c b/auto_tests/conference_invite_merge_test.c index e7ec499c..21445525 100644 --- a/auto_tests/conference_invite_merge_test.c +++ b/auto_tests/conference_invite_merge_test.c @@ -91,9 +91,11 @@ static void reload(Tox **toxes, State *state, uint32_t n) ck_assert(state[n].save_state != nullptr); struct Tox_Options *const options = tox_options_new(nullptr); + ck_assert(options != nullptr); tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE); tox_options_set_savedata_data(options, state[n].save_state, state[n].save_size); toxes[n] = tox_new_log(options, nullptr, &state[n].index); + ck_assert(toxes[n] != nullptr); tox_options_free(options); set_mono_time_callback(toxes[n], &state[n]); diff --git a/auto_tests/conference_peer_nick_test.c b/auto_tests/conference_peer_nick_test.c index 9d10a8a9..749f9098 100644 --- a/auto_tests/conference_peer_nick_test.c +++ b/auto_tests/conference_peer_nick_test.c @@ -68,6 +68,7 @@ static void rebuild_peer_list(Tox *tox) "failed to get conference peer %u's name size (conference = %u): err = %d", peer_number, conference_number, err); uint8_t *const name = (uint8_t *)malloc(size); + ck_assert(name != nullptr); tox_conference_peer_get_name(tox, conference_number, peer_number, name, &err); ck_assert_msg(err == TOX_ERR_CONFERENCE_PEER_QUERY_OK, "failed to get conference peer %u's name (conference = %u): err = %d", peer_number, conference_number, err); diff --git a/auto_tests/conference_test.c b/auto_tests/conference_test.c index ec88c82a..11a7bb06 100644 --- a/auto_tests/conference_test.c +++ b/auto_tests/conference_test.c @@ -250,9 +250,11 @@ static void run_conference_tests(Tox **toxes, State *state) for (uint32_t i = 0; i < NUM_GROUP_TOX; ++i) { if (restarting[i]) { struct Tox_Options *const options = tox_options_new(nullptr); + ck_assert(options != nullptr); tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE); tox_options_set_savedata_data(options, save[i], save_size[i]); toxes[i] = tox_new_log(options, nullptr, &state[i].index); + ck_assert(toxes[i] != nullptr); tox_options_free(options); free(save[i]); diff --git a/auto_tests/encryptsave_test.c b/auto_tests/encryptsave_test.c index 19574d16..ccb1ee88 100644 --- a/auto_tests/encryptsave_test.c +++ b/auto_tests/encryptsave_test.c @@ -76,6 +76,7 @@ static void test_save_friend(void) ck_assert_msg(tox_is_data_encrypted(enc_data), "magic number missing"); struct Tox_Options *options = tox_options_new(nullptr); + ck_assert(options != nullptr); tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE); tox_options_set_savedata_data(options, enc_data, size2); diff --git a/auto_tests/file_saving_test.c b/auto_tests/file_saving_test.c index 7faa6b20..cdcce95f 100644 --- a/auto_tests/file_saving_test.c +++ b/auto_tests/file_saving_test.c @@ -64,6 +64,7 @@ static void save_data_encrypted(void) static void load_data_decrypted(void) { FILE *f = fopen(savefile, "r"); + ck_assert(f != nullptr); fseek(f, 0, SEEK_END); int64_t size = ftell(f); fseek(f, 0, SEEK_SET); @@ -71,7 +72,9 @@ static void load_data_decrypted(void) ck_assert_msg(0 <= size && size <= UINT_MAX, "file size out of range"); uint8_t *cipher = (uint8_t *)malloc(size); + ck_assert(cipher != nullptr); uint8_t *clear = (uint8_t *)malloc(size - TOX_PASS_ENCRYPTION_EXTRA_LENGTH); + ck_assert(clear != nullptr); size_t read_value = fread(cipher, sizeof(*cipher), size, f); printf("Read read_value = %u of %u\n", (unsigned)read_value, (unsigned)size); @@ -81,6 +84,7 @@ static void load_data_decrypted(void) "Could not decrypt, error code %d.", derr); struct Tox_Options *options = tox_options_new(nullptr); + ck_assert(options != nullptr); tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE); @@ -101,10 +105,10 @@ static void load_data_decrypted(void) ck_assert_msg(strcmp((const char *)readname, name) == 0, "name returned by tox_self_get_name does not match expected result"); - free(cipher); + tox_kill(t); free(clear); + free(cipher); fclose(f); - tox_kill(t); } int main(void) diff --git a/other/astyle/format-source b/other/astyle/format-source index 711823d8..193e335c 100755 --- a/other/astyle/format-source +++ b/other/astyle/format-source @@ -49,7 +49,7 @@ apidsl_request() { } apidsl_curl() { - echo "apidsl_curl $@" >&2 + echo "apidsl_curl $*" >&2 apidsl_request "c" <( apidsl_request "parse" <( perl -0777 -pe "$TO_JSON" $1)) | perl -0777 -pe "$FROM_JSON" @@ -68,12 +68,12 @@ set -x wait; wait; wait; wait; wait; wait; wait -if grep '' */*.h; then +if grep '' ./*/*.h; then echo "error: some apidsl references were unresolved" exit 1 fi -CC_SOURCES=`find . '(' -name '*.cc' ')'` +CC_SOURCES=$(find . '(' -name '*.cc' ')') CC_SOURCES="$CC_SOURCES toxcore/crypto_core.c" CC_SOURCES="$CC_SOURCES toxcore/ping_array.c" @@ -91,7 +91,7 @@ FIND="$FIND -and -not -wholename './super_donators/*'" FIND="$FIND -and -not -wholename './third_party/*'" FIND="$FIND -and -not -wholename './toxencryptsave/crypto_pwhash*'" -C_SOURCES=`eval "$FIND"` +C_SOURCES=$(eval "$FIND") $ASTYLE -n --options=other/astyle/astylerc $C_SOURCES diff --git a/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 b/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 index 4d02d38f..82043e0d 100644 --- a/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 +++ b/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 @@ -1 +1 @@ -89df21d6a19a1b6652db5b7e6e9f65ad5f8be8abdca9f58645548b9e0c9383e3 /usr/local/bin/tox-bootstrapd +79c4f7416951d024f95d08e7fc48e127181b7465935cc75b9b3e0ab001bd43b4 /usr/local/bin/tox-bootstrapd diff --git a/toxav/toxav.c b/toxav/toxav.c index 3dbe2cff..0ecf8259 100644 --- a/toxav/toxav.c +++ b/toxav/toxav.c @@ -755,7 +755,7 @@ bool toxav_audio_send_frame(ToxAV *av, uint32_t friend_number, const int16_t *pc goto RETURN; } - { /* Encode and send */ + { /* Encode and send */ if (ac_reconfigure_encoder(call->audio, call->audio_bit_rate * 1000, sampling_rate, channels) != 0) { pthread_mutex_unlock(call->mutex_audio); rc = TOXAV_ERR_SEND_FRAME_INVALID; @@ -896,7 +896,7 @@ bool toxav_video_send_frame(ToxAV *av, uint32_t friend_number, uint16_t width, u // we start with I-frames (full frames) and then switch to normal mode later - { /* Encode */ + { /* Encode */ vpx_image_t img; img.w = 0; img.h = 0; @@ -1323,7 +1323,7 @@ static bool call_prepare_transmission(ToxAVCall *call) /* Prepare bwc */ call->bwc = bwc_new(av->m, av->tox, call->friend_number, callback_bwc, call, av->toxav_mono_time); - { /* Prepare audio */ + { /* Prepare audio */ call->audio = ac_new(av->toxav_mono_time, av->m->log, av, call->friend_number, av->acb, av->acb_user_data); if (!call->audio) { @@ -1339,7 +1339,7 @@ static bool call_prepare_transmission(ToxAVCall *call) goto FAILURE; } } - { /* Prepare video */ + { /* Prepare video */ call->video = vc_new(av->toxav_mono_time, av->m->log, av, call->friend_number, av->vcb, av->vcb_user_data); if (!call->video) { diff --git a/toxcore/DHT.c b/toxcore/DHT.c index b3017259..7c9263cd 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -2917,6 +2917,12 @@ static State_Load_Status dht_load_state_callback(void *outer, const uint8_t *dat // Copy to loaded_clients_list dht->loaded_nodes_list = (Node_format *)calloc(MAX_SAVED_DHT_NODES, sizeof(Node_format)); + if (dht->loaded_nodes_list == 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, 0); if (num > 0) { -- cgit v1.2.3