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. --- auto_tests/file_saving_test.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'auto_tests/file_saving_test.c') 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) -- cgit v1.2.3