From 6ae33c16cf9e37fda85d70c78b3c2779eb8ca21a Mon Sep 17 00:00:00 2001 From: iphydf Date: Fri, 20 Jan 2017 21:16:55 +0000 Subject: Add VLA compatibility macro for C89-ish compilers. --- auto_tests/TCP_test.c | 6 +++--- auto_tests/encryptsave_test.c | 14 ++++++++------ auto_tests/messenger_test.c | 10 +++++----- auto_tests/save_friend_test.c | 7 ++++--- auto_tests/tox_one_test.c | 3 ++- auto_tests/tox_test.c | 9 +++++---- 6 files changed, 27 insertions(+), 22 deletions(-) (limited to 'auto_tests') diff --git a/auto_tests/TCP_test.c b/auto_tests/TCP_test.c index a1de4874..a5a70c4b 100644 --- a/auto_tests/TCP_test.c +++ b/auto_tests/TCP_test.c @@ -188,19 +188,19 @@ static void kill_TCP_con(struct sec_TCP_con *con) static int write_packet_TCP_secure_connection(struct sec_TCP_con *con, uint8_t *data, uint16_t length) { - uint8_t packet[sizeof(uint16_t) + length + CRYPTO_MAC_SIZE]; + VLA(uint8_t, packet, sizeof(uint16_t) + length + CRYPTO_MAC_SIZE); uint16_t c_length = htons(length + CRYPTO_MAC_SIZE); memcpy(packet, &c_length, sizeof(uint16_t)); int len = encrypt_data_symmetric(con->shared_key, con->sent_nonce, data, length, packet + sizeof(uint16_t)); - if ((unsigned int)len != (sizeof(packet) - sizeof(uint16_t))) { + if ((unsigned int)len != (SIZEOF_VLA(packet) - sizeof(uint16_t))) { return -1; } increment_nonce(con->sent_nonce); - ck_assert_msg(send(con->sock, (const char *)packet, sizeof(packet), 0) == sizeof(packet), "send failed"); + ck_assert_msg(send(con->sock, (const char *)packet, SIZEOF_VLA(packet), 0) == SIZEOF_VLA(packet), "send failed"); return 0; } diff --git a/auto_tests/encryptsave_test.c b/auto_tests/encryptsave_test.c index 22c585ed..749767a0 100644 --- a/auto_tests/encryptsave_test.c +++ b/auto_tests/encryptsave_test.c @@ -13,6 +13,7 @@ #include "../toxcore/tox.h" +#include "../toxcore/ccompat.h" #include "../toxcore/crypto_core.h" #include "../toxencryptsave/toxencryptsave.h" #ifdef VANILLA_NACL @@ -68,10 +69,10 @@ START_TEST(test_save_friend) ck_assert_msg(test != UINT32_MAX, "Failed to add friend"); size_t size = tox_get_savedata_size(tox1); - uint8_t data[size]; + VLA(uint8_t, data, size); tox_get_savedata(tox1, data); size_t size2 = size + TOX_PASS_ENCRYPTION_EXTRA_LENGTH; - uint8_t enc_data[size2]; + VLA(uint8_t, enc_data, size2); TOX_ERR_ENCRYPTION error1; bool ret = tox_pass_encrypt(data, size, (const uint8_t *)"correcthorsebatterystaple", 25, enc_data, &error1); ck_assert_msg(ret, "failed to encrypted save: %u", error1); @@ -86,7 +87,7 @@ START_TEST(test_save_friend) ck_assert_msg(err2 == TOX_ERR_NEW_LOAD_ENCRYPTED, "wrong error! %u. should fail with %u", err2, TOX_ERR_NEW_LOAD_ENCRYPTED); ck_assert_msg(tox3 == NULL, "tox_new with error should return NULL"); - uint8_t dec_data[size]; + VLA(uint8_t, dec_data, size); TOX_ERR_DECRYPTION err3; ret = tox_pass_decrypt(enc_data, size2, (const uint8_t *)"correcthorsebatterystaple", 25, dec_data, &err3); ck_assert_msg(ret, "failed to decrypt save: %u", err3); @@ -99,19 +100,20 @@ START_TEST(test_save_friend) ck_assert_msg(memcmp(address, address2, TOX_PUBLIC_KEY_SIZE) == 0, "addresses don't match!"); size = tox_get_savedata_size(tox3); - uint8_t data2[size]; + VLA(uint8_t, data2, size); tox_get_savedata(tox3, data2); Tox_Pass_Key *key = tox_pass_key_new(); ck_assert_msg(key != NULL, "pass key allocation failure"); memcpy((uint8_t *)key, test_salt, TOX_PASS_SALT_LENGTH); memcpy((uint8_t *)key + TOX_PASS_SALT_LENGTH, known_key2, TOX_PASS_KEY_LENGTH); size2 = size + TOX_PASS_ENCRYPTION_EXTRA_LENGTH; - uint8_t encdata2[size2]; + VLA(uint8_t, encdata2, size2); ret = tox_pass_key_encrypt(key, data2, size, encdata2, &error1); ck_assert_msg(ret, "failed to key encrypt %u", error1); ck_assert_msg(tox_is_data_encrypted(encdata2), "magic number the second missing"); - uint8_t out1[size], out2[size]; + VLA(uint8_t, out1, size); + VLA(uint8_t, out2, size); ret = tox_pass_decrypt(encdata2, size2, (const uint8_t *)pw, pwlen, out1, &err3); ck_assert_msg(ret, "failed to pw decrypt %u", err3); ret = tox_pass_key_decrypt(key, encdata2, size2, out2, &err3); diff --git a/auto_tests/messenger_test.c b/auto_tests/messenger_test.c index efe2963a..bd524014 100644 --- a/auto_tests/messenger_test.c +++ b/auto_tests/messenger_test.c @@ -184,7 +184,7 @@ START_TEST(test_getself_name) { const char *nickname = "testGallop"; int len = strlen(nickname); - char nick_check[len]; + VLA(char, nick_check, len); setname(m, (const uint8_t *)nickname, len); getself_name(m, (uint8_t *)nick_check); @@ -237,7 +237,7 @@ START_TEST(test_dht_state_saveloadsave) * d) the second save() is of equal content */ size_t i, extra = 64; size_t size = DHT_size(m->dht); - uint8_t buffer[size + 2 * extra]; + VLA(uint8_t, buffer, size + 2 * extra); memset(buffer, 0xCD, extra); memset(buffer + extra + size, 0xCD, extra); DHT_save(m->dht, buffer + extra); @@ -263,7 +263,7 @@ START_TEST(test_dht_state_saveloadsave) size_t size2 = DHT_size(m->dht); ck_assert_msg(size == size2, "Messenger \"grew\" in size from a store/load cycle: %u -> %u", size, size2); - uint8_t buffer2[size2]; + VLA(uint8_t, buffer2, size2); DHT_save(m->dht, buffer2); ck_assert_msg(!memcmp(buffer + extra, buffer2, size), "DHT state changed by store/load/store cycle"); @@ -279,7 +279,7 @@ START_TEST(test_messenger_state_saveloadsave) * d) the second save() is of equal content */ size_t i, extra = 64; size_t size = messenger_size(m); - uint8_t buffer[size + 2 * extra]; + VLA(uint8_t, buffer, size + 2 * extra); memset(buffer, 0xCD, extra); memset(buffer + extra + size, 0xCD, extra); messenger_save(m, buffer + extra); @@ -305,7 +305,7 @@ START_TEST(test_messenger_state_saveloadsave) size_t size2 = messenger_size(m); ck_assert_msg(size == size2, "Messenger \"grew\" in size from a store/load cycle: %u -> %u", size, size2); - uint8_t buffer2[size2]; + VLA(uint8_t, buffer2, size2); messenger_save(m, buffer2); ck_assert_msg(!memcmp(buffer + extra, buffer2, size), "Messenger state changed by store/load/store cycle"); diff --git a/auto_tests/save_friend_test.c b/auto_tests/save_friend_test.c index 448160c0..6d0640c3 100644 --- a/auto_tests/save_friend_test.c +++ b/auto_tests/save_friend_test.c @@ -4,6 +4,7 @@ #define _XOPEN_SOURCE 600 #include "helpers.h" +#include "../toxcore/ccompat.h" #include "../toxcore/tox.h" #include @@ -28,14 +29,14 @@ struct test_data { static void set_random(Tox *m, bool (*setter)(Tox *, const uint8_t *, size_t, TOX_ERR_SET_INFO *), size_t length) { - uint8_t text[length]; + VLA(uint8_t, text, length); uint32_t i; for (i = 0; i < length; ++i) { text[i] = rand(); } - setter(m, text, sizeof(text), 0); + setter(m, text, SIZEOF_VLA(text), 0); } void namechange_callback(Tox *tox, uint32_t friend_number, const uint8_t *name, size_t length, void *user_data) @@ -106,7 +107,7 @@ int main(int argc, char *argv[]) } size_t save_size = tox_get_savedata_size(tox1); - uint8_t savedata[save_size]; + VLA(uint8_t, savedata, save_size); tox_get_savedata(tox1, savedata); struct Tox_Options *options = tox_options_new(NULL); diff --git a/auto_tests/tox_one_test.c b/auto_tests/tox_one_test.c index 8726d5c5..ce698486 100644 --- a/auto_tests/tox_one_test.c +++ b/auto_tests/tox_one_test.c @@ -9,6 +9,7 @@ #include #include +#include "../toxcore/ccompat.h" #include "../toxcore/tox.h" #include "../toxcore/util.h" @@ -77,7 +78,7 @@ START_TEST(test_one) tox_self_get_address(tox1, address); size_t save_size = tox_get_savedata_size(tox1); - uint8_t data[save_size]; + VLA(uint8_t, data, save_size); tox_get_savedata(tox1, data); tox_kill(tox2); diff --git a/auto_tests/tox_test.c b/auto_tests/tox_test.c index ab2202d1..d9074ac0 100644 --- a/auto_tests/tox_test.c +++ b/auto_tests/tox_test.c @@ -21,6 +21,7 @@ #include #include +#include "../toxcore/ccompat.h" #include "../toxcore/tox.h" #include "../toxcore/util.h" @@ -125,7 +126,7 @@ static void handle_custom_packet(Tox *m, uint32_t friend_num, const uint8_t *dat return; } - uint8_t f_data[len]; + VLA(uint8_t, f_data, len); memset(f_data, number, len); if (memcmp(f_data, data, len) == 0) { @@ -260,7 +261,7 @@ static void tox_file_chunk_request(Tox *tox, uint32_t friend_number, uint32_t fi } TOX_ERR_FILE_SEND_CHUNK error; - uint8_t f_data[length]; + VLA(uint8_t, f_data, length); memset(f_data, sending_num, length); if (tox_file_send_chunk(tox, friend_number, file_number, position, f_data, length, &error)) { @@ -294,7 +295,7 @@ static void write_file(Tox *tox, uint32_t friendnumber, uint32_t filenumber, uin return; } - uint8_t f_data[length]; + VLA(uint8_t, f_data, length); memset(f_data, num, length); ++num; @@ -416,7 +417,7 @@ START_TEST(test_few_clients) unsigned int save_size1 = tox_get_savedata_size(tox2); ck_assert_msg(save_size1 != 0 && save_size1 < 4096, "save is invalid size %u", save_size1); printf("%u\n", save_size1); - uint8_t save1[save_size1]; + VLA(uint8_t, save1, save_size1); tox_get_savedata(tox2, save1); tox_kill(tox2); -- cgit v1.2.3