summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt1
-rw-r--r--appveyor.yml2
-rw-r--r--auto_tests/TCP_test.c6
-rw-r--r--auto_tests/encryptsave_test.c14
-rw-r--r--auto_tests/messenger_test.c10
-rw-r--r--auto_tests/save_friend_test.c7
-rw-r--r--auto_tests/tox_one_test.c3
-rw-r--r--auto_tests/tox_test.c9
-rw-r--r--other/bootstrap_daemon/src/log.c4
-rw-r--r--testing/irc_syncbot.c7
-rw-r--r--testing/nTox.c24
-rw-r--r--testing/tox_sync.c5
-rw-r--r--toxav/groupav.c4
-rw-r--r--toxav/rtp.c10
-rw-r--r--toxav/toxav.c4
-rw-r--r--toxcore/DHT.c22
-rw-r--r--toxcore/Makefile.inc3
-rw-r--r--toxcore/Messenger.c28
-rw-r--r--toxcore/TCP_client.c32
-rw-r--r--toxcore/TCP_connection.c5
-rw-r--r--toxcore/TCP_server.c30
-rw-r--r--toxcore/ccompat.h43
-rw-r--r--toxcore/crypto_core.c8
-rw-r--r--toxcore/friend_connection.c6
-rw-r--r--toxcore/friend_requests.c4
-rw-r--r--toxcore/group.c24
-rw-r--r--toxcore/net_crypto.c10
-rw-r--r--toxcore/network.h7
-rw-r--r--toxcore/onion.c24
-rw-r--r--toxcore/onion_announce.c4
-rw-r--r--toxcore/onion_client.c28
-rw-r--r--toxcore/util.c2
-rw-r--r--toxdns/toxdns.c4
33 files changed, 224 insertions, 170 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index bf3cc06a..244c13d1 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -197,6 +197,7 @@ set(toxcore_PKGCONFIG_LIBS)
197apidsl( 197apidsl(
198 toxcore/crypto_core.api.h) 198 toxcore/crypto_core.api.h)
199add_module(toxcrypto 199add_module(toxcrypto
200 toxcore/ccompat.h
200 toxcore/crypto_core.c 201 toxcore/crypto_core.c
201 toxcore/crypto_core.h 202 toxcore/crypto_core.h
202 toxcore/crypto_core_mem.c) 203 toxcore/crypto_core_mem.c)
diff --git a/appveyor.yml b/appveyor.yml
index 879ebc10..5add5f31 100644
--- a/appveyor.yml
+++ b/appveyor.yml
@@ -3,7 +3,7 @@ install:
3- unzip libsodium-1.0.11-msvc.zip 3- unzip libsodium-1.0.11-msvc.zip
4 4
5before_build: 5before_build:
6- cmake . -DBOOTSTRAP_DAEMON=OFF 6- cmake . -DBOOTSTRAP_DAEMON=OFF -DENABLE_SHARED=OFF
7 7
8build: 8build:
9 project: INSTALL.vcxproj 9 project: INSTALL.vcxproj
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)
188 188
189static int write_packet_TCP_secure_connection(struct sec_TCP_con *con, uint8_t *data, uint16_t length) 189static int write_packet_TCP_secure_connection(struct sec_TCP_con *con, uint8_t *data, uint16_t length)
190{ 190{
191 uint8_t packet[sizeof(uint16_t) + length + CRYPTO_MAC_SIZE]; 191 VLA(uint8_t, packet, sizeof(uint16_t) + length + CRYPTO_MAC_SIZE);
192 192
193 uint16_t c_length = htons(length + CRYPTO_MAC_SIZE); 193 uint16_t c_length = htons(length + CRYPTO_MAC_SIZE);
194 memcpy(packet, &c_length, sizeof(uint16_t)); 194 memcpy(packet, &c_length, sizeof(uint16_t));
195 int len = encrypt_data_symmetric(con->shared_key, con->sent_nonce, data, length, packet + sizeof(uint16_t)); 195 int len = encrypt_data_symmetric(con->shared_key, con->sent_nonce, data, length, packet + sizeof(uint16_t));
196 196
197 if ((unsigned int)len != (sizeof(packet) - sizeof(uint16_t))) { 197 if ((unsigned int)len != (SIZEOF_VLA(packet) - sizeof(uint16_t))) {
198 return -1; 198 return -1;
199 } 199 }
200 200
201 increment_nonce(con->sent_nonce); 201 increment_nonce(con->sent_nonce);
202 202
203 ck_assert_msg(send(con->sock, (const char *)packet, sizeof(packet), 0) == sizeof(packet), "send failed"); 203 ck_assert_msg(send(con->sock, (const char *)packet, SIZEOF_VLA(packet), 0) == SIZEOF_VLA(packet), "send failed");
204 return 0; 204 return 0;
205} 205}
206 206
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 @@
13 13
14#include "../toxcore/tox.h" 14#include "../toxcore/tox.h"
15 15
16#include "../toxcore/ccompat.h"
16#include "../toxcore/crypto_core.h" 17#include "../toxcore/crypto_core.h"
17#include "../toxencryptsave/toxencryptsave.h" 18#include "../toxencryptsave/toxencryptsave.h"
18#ifdef VANILLA_NACL 19#ifdef VANILLA_NACL
@@ -68,10 +69,10 @@ START_TEST(test_save_friend)
68 ck_assert_msg(test != UINT32_MAX, "Failed to add friend"); 69 ck_assert_msg(test != UINT32_MAX, "Failed to add friend");
69 70
70 size_t size = tox_get_savedata_size(tox1); 71 size_t size = tox_get_savedata_size(tox1);
71 uint8_t data[size]; 72 VLA(uint8_t, data, size);
72 tox_get_savedata(tox1, data); 73 tox_get_savedata(tox1, data);
73 size_t size2 = size + TOX_PASS_ENCRYPTION_EXTRA_LENGTH; 74 size_t size2 = size + TOX_PASS_ENCRYPTION_EXTRA_LENGTH;
74 uint8_t enc_data[size2]; 75 VLA(uint8_t, enc_data, size2);
75 TOX_ERR_ENCRYPTION error1; 76 TOX_ERR_ENCRYPTION error1;
76 bool ret = tox_pass_encrypt(data, size, (const uint8_t *)"correcthorsebatterystaple", 25, enc_data, &error1); 77 bool ret = tox_pass_encrypt(data, size, (const uint8_t *)"correcthorsebatterystaple", 25, enc_data, &error1);
77 ck_assert_msg(ret, "failed to encrypted save: %u", error1); 78 ck_assert_msg(ret, "failed to encrypted save: %u", error1);
@@ -86,7 +87,7 @@ START_TEST(test_save_friend)
86 ck_assert_msg(err2 == TOX_ERR_NEW_LOAD_ENCRYPTED, "wrong error! %u. should fail with %u", err2, 87 ck_assert_msg(err2 == TOX_ERR_NEW_LOAD_ENCRYPTED, "wrong error! %u. should fail with %u", err2,
87 TOX_ERR_NEW_LOAD_ENCRYPTED); 88 TOX_ERR_NEW_LOAD_ENCRYPTED);
88 ck_assert_msg(tox3 == NULL, "tox_new with error should return NULL"); 89 ck_assert_msg(tox3 == NULL, "tox_new with error should return NULL");
89 uint8_t dec_data[size]; 90 VLA(uint8_t, dec_data, size);
90 TOX_ERR_DECRYPTION err3; 91 TOX_ERR_DECRYPTION err3;
91 ret = tox_pass_decrypt(enc_data, size2, (const uint8_t *)"correcthorsebatterystaple", 25, dec_data, &err3); 92 ret = tox_pass_decrypt(enc_data, size2, (const uint8_t *)"correcthorsebatterystaple", 25, dec_data, &err3);
92 ck_assert_msg(ret, "failed to decrypt save: %u", err3); 93 ck_assert_msg(ret, "failed to decrypt save: %u", err3);
@@ -99,19 +100,20 @@ START_TEST(test_save_friend)
99 ck_assert_msg(memcmp(address, address2, TOX_PUBLIC_KEY_SIZE) == 0, "addresses don't match!"); 100 ck_assert_msg(memcmp(address, address2, TOX_PUBLIC_KEY_SIZE) == 0, "addresses don't match!");
100 101
101 size = tox_get_savedata_size(tox3); 102 size = tox_get_savedata_size(tox3);
102 uint8_t data2[size]; 103 VLA(uint8_t, data2, size);
103 tox_get_savedata(tox3, data2); 104 tox_get_savedata(tox3, data2);
104 Tox_Pass_Key *key = tox_pass_key_new(); 105 Tox_Pass_Key *key = tox_pass_key_new();
105 ck_assert_msg(key != NULL, "pass key allocation failure"); 106 ck_assert_msg(key != NULL, "pass key allocation failure");
106 memcpy((uint8_t *)key, test_salt, TOX_PASS_SALT_LENGTH); 107 memcpy((uint8_t *)key, test_salt, TOX_PASS_SALT_LENGTH);
107 memcpy((uint8_t *)key + TOX_PASS_SALT_LENGTH, known_key2, TOX_PASS_KEY_LENGTH); 108 memcpy((uint8_t *)key + TOX_PASS_SALT_LENGTH, known_key2, TOX_PASS_KEY_LENGTH);
108 size2 = size + TOX_PASS_ENCRYPTION_EXTRA_LENGTH; 109 size2 = size + TOX_PASS_ENCRYPTION_EXTRA_LENGTH;
109 uint8_t encdata2[size2]; 110 VLA(uint8_t, encdata2, size2);
110 ret = tox_pass_key_encrypt(key, data2, size, encdata2, &error1); 111 ret = tox_pass_key_encrypt(key, data2, size, encdata2, &error1);
111 ck_assert_msg(ret, "failed to key encrypt %u", error1); 112 ck_assert_msg(ret, "failed to key encrypt %u", error1);
112 ck_assert_msg(tox_is_data_encrypted(encdata2), "magic number the second missing"); 113 ck_assert_msg(tox_is_data_encrypted(encdata2), "magic number the second missing");
113 114
114 uint8_t out1[size], out2[size]; 115 VLA(uint8_t, out1, size);
116 VLA(uint8_t, out2, size);
115 ret = tox_pass_decrypt(encdata2, size2, (const uint8_t *)pw, pwlen, out1, &err3); 117 ret = tox_pass_decrypt(encdata2, size2, (const uint8_t *)pw, pwlen, out1, &err3);
116 ck_assert_msg(ret, "failed to pw decrypt %u", err3); 118 ck_assert_msg(ret, "failed to pw decrypt %u", err3);
117 ret = tox_pass_key_decrypt(key, encdata2, size2, out2, &err3); 119 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)
184{ 184{
185 const char *nickname = "testGallop"; 185 const char *nickname = "testGallop";
186 int len = strlen(nickname); 186 int len = strlen(nickname);
187 char nick_check[len]; 187 VLA(char, nick_check, len);
188 188
189 setname(m, (const uint8_t *)nickname, len); 189 setname(m, (const uint8_t *)nickname, len);
190 getself_name(m, (uint8_t *)nick_check); 190 getself_name(m, (uint8_t *)nick_check);
@@ -237,7 +237,7 @@ START_TEST(test_dht_state_saveloadsave)
237 * d) the second save() is of equal content */ 237 * d) the second save() is of equal content */
238 size_t i, extra = 64; 238 size_t i, extra = 64;
239 size_t size = DHT_size(m->dht); 239 size_t size = DHT_size(m->dht);
240 uint8_t buffer[size + 2 * extra]; 240 VLA(uint8_t, buffer, size + 2 * extra);
241 memset(buffer, 0xCD, extra); 241 memset(buffer, 0xCD, extra);
242 memset(buffer + extra + size, 0xCD, extra); 242 memset(buffer + extra + size, 0xCD, extra);
243 DHT_save(m->dht, buffer + extra); 243 DHT_save(m->dht, buffer + extra);
@@ -263,7 +263,7 @@ START_TEST(test_dht_state_saveloadsave)
263 size_t size2 = DHT_size(m->dht); 263 size_t size2 = DHT_size(m->dht);
264 ck_assert_msg(size == size2, "Messenger \"grew\" in size from a store/load cycle: %u -> %u", size, size2); 264 ck_assert_msg(size == size2, "Messenger \"grew\" in size from a store/load cycle: %u -> %u", size, size2);
265 265
266 uint8_t buffer2[size2]; 266 VLA(uint8_t, buffer2, size2);
267 DHT_save(m->dht, buffer2); 267 DHT_save(m->dht, buffer2);
268 268
269 ck_assert_msg(!memcmp(buffer + extra, buffer2, size), "DHT state changed by store/load/store cycle"); 269 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)
279 * d) the second save() is of equal content */ 279 * d) the second save() is of equal content */
280 size_t i, extra = 64; 280 size_t i, extra = 64;
281 size_t size = messenger_size(m); 281 size_t size = messenger_size(m);
282 uint8_t buffer[size + 2 * extra]; 282 VLA(uint8_t, buffer, size + 2 * extra);
283 memset(buffer, 0xCD, extra); 283 memset(buffer, 0xCD, extra);
284 memset(buffer + extra + size, 0xCD, extra); 284 memset(buffer + extra + size, 0xCD, extra);
285 messenger_save(m, buffer + extra); 285 messenger_save(m, buffer + extra);
@@ -305,7 +305,7 @@ START_TEST(test_messenger_state_saveloadsave)
305 size_t size2 = messenger_size(m); 305 size_t size2 = messenger_size(m);
306 ck_assert_msg(size == size2, "Messenger \"grew\" in size from a store/load cycle: %u -> %u", size, size2); 306 ck_assert_msg(size == size2, "Messenger \"grew\" in size from a store/load cycle: %u -> %u", size, size2);
307 307
308 uint8_t buffer2[size2]; 308 VLA(uint8_t, buffer2, size2);
309 messenger_save(m, buffer2); 309 messenger_save(m, buffer2);
310 310
311 ck_assert_msg(!memcmp(buffer + extra, buffer2, size), "Messenger state changed by store/load/store cycle"); 311 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 @@
4#define _XOPEN_SOURCE 600 4#define _XOPEN_SOURCE 600
5 5
6#include "helpers.h" 6#include "helpers.h"
7#include "../toxcore/ccompat.h"
7#include "../toxcore/tox.h" 8#include "../toxcore/tox.h"
8 9
9#include <assert.h> 10#include <assert.h>
@@ -28,14 +29,14 @@ struct test_data {
28 29
29static void set_random(Tox *m, bool (*setter)(Tox *, const uint8_t *, size_t, TOX_ERR_SET_INFO *), size_t length) 30static void set_random(Tox *m, bool (*setter)(Tox *, const uint8_t *, size_t, TOX_ERR_SET_INFO *), size_t length)
30{ 31{
31 uint8_t text[length]; 32 VLA(uint8_t, text, length);
32 uint32_t i; 33 uint32_t i;
33 34
34 for (i = 0; i < length; ++i) { 35 for (i = 0; i < length; ++i) {
35 text[i] = rand(); 36 text[i] = rand();
36 } 37 }
37 38
38 setter(m, text, sizeof(text), 0); 39 setter(m, text, SIZEOF_VLA(text), 0);
39} 40}
40 41
41void namechange_callback(Tox *tox, uint32_t friend_number, const uint8_t *name, size_t length, void *user_data) 42void 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[])
106 } 107 }
107 108
108 size_t save_size = tox_get_savedata_size(tox1); 109 size_t save_size = tox_get_savedata_size(tox1);
109 uint8_t savedata[save_size]; 110 VLA(uint8_t, savedata, save_size);
110 tox_get_savedata(tox1, savedata); 111 tox_get_savedata(tox1, savedata);
111 112
112 struct Tox_Options *options = tox_options_new(NULL); 113 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 @@
9#include <stdlib.h> 9#include <stdlib.h>
10#include <time.h> 10#include <time.h>
11 11
12#include "../toxcore/ccompat.h"
12#include "../toxcore/tox.h" 13#include "../toxcore/tox.h"
13#include "../toxcore/util.h" 14#include "../toxcore/util.h"
14 15
@@ -77,7 +78,7 @@ START_TEST(test_one)
77 78
78 tox_self_get_address(tox1, address); 79 tox_self_get_address(tox1, address);
79 size_t save_size = tox_get_savedata_size(tox1); 80 size_t save_size = tox_get_savedata_size(tox1);
80 uint8_t data[save_size]; 81 VLA(uint8_t, data, save_size);
81 tox_get_savedata(tox1, data); 82 tox_get_savedata(tox1, data);
82 83
83 tox_kill(tox2); 84 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 @@
21#include <stdlib.h> 21#include <stdlib.h>
22#include <time.h> 22#include <time.h>
23 23
24#include "../toxcore/ccompat.h"
24#include "../toxcore/tox.h" 25#include "../toxcore/tox.h"
25#include "../toxcore/util.h" 26#include "../toxcore/util.h"
26 27
@@ -125,7 +126,7 @@ static void handle_custom_packet(Tox *m, uint32_t friend_num, const uint8_t *dat
125 return; 126 return;
126 } 127 }
127 128
128 uint8_t f_data[len]; 129 VLA(uint8_t, f_data, len);
129 memset(f_data, number, len); 130 memset(f_data, number, len);
130 131
131 if (memcmp(f_data, data, len) == 0) { 132 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
260 } 261 }
261 262
262 TOX_ERR_FILE_SEND_CHUNK error; 263 TOX_ERR_FILE_SEND_CHUNK error;
263 uint8_t f_data[length]; 264 VLA(uint8_t, f_data, length);
264 memset(f_data, sending_num, length); 265 memset(f_data, sending_num, length);
265 266
266 if (tox_file_send_chunk(tox, friend_number, file_number, position, f_data, length, &error)) { 267 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
294 return; 295 return;
295 } 296 }
296 297
297 uint8_t f_data[length]; 298 VLA(uint8_t, f_data, length);
298 memset(f_data, num, length); 299 memset(f_data, num, length);
299 ++num; 300 ++num;
300 301
@@ -416,7 +417,7 @@ START_TEST(test_few_clients)
416 unsigned int save_size1 = tox_get_savedata_size(tox2); 417 unsigned int save_size1 = tox_get_savedata_size(tox2);
417 ck_assert_msg(save_size1 != 0 && save_size1 < 4096, "save is invalid size %u", save_size1); 418 ck_assert_msg(save_size1 != 0 && save_size1 < 4096, "save is invalid size %u", save_size1);
418 printf("%u\n", save_size1); 419 printf("%u\n", save_size1);
419 uint8_t save1[save_size1]; 420 VLA(uint8_t, save1, save_size1);
420 tox_get_savedata(tox2, save1); 421 tox_get_savedata(tox2, save1);
421 tox_kill(tox2); 422 tox_kill(tox2);
422 423
diff --git a/other/bootstrap_daemon/src/log.c b/other/bootstrap_daemon/src/log.c
index 7431838c..a0517528 100644
--- a/other/bootstrap_daemon/src/log.c
+++ b/other/bootstrap_daemon/src/log.c
@@ -26,6 +26,8 @@
26 26
27#include "global.h" 27#include "global.h"
28 28
29#include "../../../toxcore/ccompat.h"
30
29#include <assert.h> 31#include <assert.h>
30#include <syslog.h> 32#include <syslog.h>
31#include <stdarg.h> 33#include <stdarg.h>
@@ -94,7 +96,7 @@ static void log_syslog(LOG_LEVEL level, const char *format, va_list args)
94 return; 96 return;
95 } 97 }
96 98
97 char buf[size + 1]; 99 VLA(char, buf, size + 1);
98 vsnprintf(buf, size + 1, format, args); 100 vsnprintf(buf, size + 1, format, args);
99 101
100 syslog(level_syslog(level), "%s", buf); 102 syslog(level_syslog(level), "%s", buf);
diff --git a/testing/irc_syncbot.c b/testing/irc_syncbot.c
index 783be51a..91267f84 100644
--- a/testing/irc_syncbot.c
+++ b/testing/irc_syncbot.c
@@ -83,6 +83,7 @@ static int reconnect(void)
83 return new_sock; 83 return new_sock;
84} 84}
85 85
86#include "../toxcore/ccompat.h"
86#include "../toxcore/tox.h" 87#include "../toxcore/tox.h"
87#include "misc_tools.c" 88#include "misc_tools.c"
88 89
@@ -178,7 +179,7 @@ static void send_irc_group(Tox *tox, uint8_t *msg, uint16_t len)
178 return; 179 return;
179 } 180 }
180 181
181 uint8_t req[len]; 182 VLA(uint8_t, req, len);
182 unsigned int i; 183 unsigned int i;
183 184
184 unsigned int spaces = 0; 185 unsigned int spaces = 0;
@@ -198,7 +199,7 @@ static void send_irc_group(Tox *tox, uint8_t *msg, uint16_t len)
198 unsigned int req_len = i; 199 unsigned int req_len = i;
199 req[i] = 0; 200 req[i] = 0;
200 201
201 uint8_t message[len]; 202 VLA(uint8_t, message, len);
202 uint16_t length = 0; 203 uint16_t length = 0;
203 204
204 uint8_t *pmsg = (uint8_t *)strstr((char *)req, " PRIVMSG"); 205 uint8_t *pmsg = (uint8_t *)strstr((char *)req, " PRIVMSG");
@@ -298,7 +299,7 @@ int main(int argc, char *argv[])
298 if (count > 0) { 299 if (count > 0) {
299 last_get = get_monotime_sec(); 300 last_get = get_monotime_sec();
300 ping_sent = 0; 301 ping_sent = 0;
301 uint8_t data[count + 1]; 302 VLA(uint8_t, data, count + 1);
302 data[count] = 0; 303 data[count] = 0;
303 recv(sock, data, count, MSG_NOSIGNAL); 304 recv(sock, data, count, MSG_NOSIGNAL);
304 printf("%s", data); 305 printf("%s", data);
diff --git a/testing/nTox.c b/testing/nTox.c
index 160aef29..d02a1652 100644
--- a/testing/nTox.c
+++ b/testing/nTox.c
@@ -39,6 +39,7 @@
39 39
40#include <sys/select.h> 40#include <sys/select.h>
41 41
42#include "../toxcore/ccompat.h"
42#include "misc_tools.c" 43#include "misc_tools.c"
43#include "nTox.h" 44#include "nTox.h"
44 45
@@ -144,7 +145,7 @@ static void tox_file_chunk_request(Tox *tox, uint32_t friend_number, uint32_t fi
144 } 145 }
145 146
146 fseek(file_senders[i].file, position, SEEK_SET); 147 fseek(file_senders[i].file, position, SEEK_SET);
147 uint8_t data[length]; 148 VLA(uint8_t, data, length);
148 int len = fread(data, 1, length, file_senders[i].file); 149 int len = fread(data, 1, length, file_senders[i].file);
149 tox_file_send_chunk(tox, friend_number, file_number, position, data, len, 0); 150 tox_file_send_chunk(tox, friend_number, file_number, position, data, len, 0);
150 break; 151 break;
@@ -266,7 +267,7 @@ static void print_friendlist(Tox *m)
266 char fraddr_str[FRADDR_TOSTR_BUFSIZE]; 267 char fraddr_str[FRADDR_TOSTR_BUFSIZE];
267 268
268 /* account for the longest name and the longest "base" string and number (int) and id_str */ 269 /* account for the longest name and the longest "base" string and number (int) and id_str */
269 char fstring[TOX_MAX_NAME_LENGTH + strlen(ptrn_friend) + 21 + id_str_len]; 270 VLA(char, fstring, TOX_MAX_NAME_LENGTH + strlen(ptrn_friend) + 21 + id_str_len);
270 271
271 uint32_t i = 0; 272 uint32_t i = 0;
272 273
@@ -299,7 +300,7 @@ static void print_formatted_message(Tox *m, char *message, int friendnum, uint8_
299 char name[TOX_MAX_NAME_LENGTH + 1]; 300 char name[TOX_MAX_NAME_LENGTH + 1];
300 getfriendname_terminated(m, friendnum, name); 301 getfriendname_terminated(m, friendnum, name);
301 302
302 char msg[100 + strlen(message) + strlen(name) + 1]; 303 VLA(char, msg, 100 + strlen(message) + strlen(name) + 1);
303 304
304 time_t rawtime; 305 time_t rawtime;
305 struct tm *timeinfo; 306 struct tm *timeinfo;
@@ -920,7 +921,7 @@ static void print_message(Tox *m, uint32_t friendnumber, TOX_MESSAGE_TYPE type,
920 void *userdata) 921 void *userdata)
921{ 922{
922 /* ensure null termination */ 923 /* ensure null termination */
923 uint8_t null_string[length + 1]; 924 VLA(uint8_t, null_string, length + 1);
924 memcpy(null_string, string, length); 925 memcpy(null_string, string, length);
925 null_string[length] = 0; 926 null_string[length] = 0;
926 print_formatted_message(m, (char *)null_string, friendnumber, 0); 927 print_formatted_message(m, (char *)null_string, friendnumber, 0);
@@ -931,7 +932,7 @@ static void print_nickchange(Tox *m, uint32_t friendnumber, const uint8_t *strin
931 char name[TOX_MAX_NAME_LENGTH + 1]; 932 char name[TOX_MAX_NAME_LENGTH + 1];
932 933
933 if (getfriendname_terminated(m, friendnumber, name) != -1) { 934 if (getfriendname_terminated(m, friendnumber, name) != -1) {
934 char msg[100 + length]; 935 VLA(char, msg, 100 + length);
935 936
936 if (name[0] != 0) { 937 if (name[0] != 0) {
937 sprintf(msg, "[i] [%d] %s is now known as %s.", friendnumber, name, string); 938 sprintf(msg, "[i] [%d] %s is now known as %s.", friendnumber, name, string);
@@ -948,7 +949,7 @@ static void print_statuschange(Tox *m, uint32_t friendnumber, const uint8_t *str
948 char name[TOX_MAX_NAME_LENGTH + 1]; 949 char name[TOX_MAX_NAME_LENGTH + 1];
949 950
950 if (getfriendname_terminated(m, friendnumber, name) != -1) { 951 if (getfriendname_terminated(m, friendnumber, name) != -1) {
951 char msg[100 + length + strlen(name) + 1]; 952 VLA(char, msg, 100 + length + strlen(name) + 1);
952 953
953 if (name[0] != 0) { 954 if (name[0] != 0) {
954 sprintf(msg, "[i] [%d] %s's status changed to %s.", friendnumber, name, string); 955 sprintf(msg, "[i] [%d] %s's status changed to %s.", friendnumber, name, string);
@@ -971,7 +972,7 @@ static Tox *load_data(void)
971 size_t size = ftell(data_file); 972 size_t size = ftell(data_file);
972 rewind(data_file); 973 rewind(data_file);
973 974
974 uint8_t data[size]; 975 VLA(uint8_t, data, size);
975 976
976 if (fread(data, sizeof(uint8_t), size, data_file) != size) { 977 if (fread(data, sizeof(uint8_t), size, data_file) != size) {
977 fputs("[!] could not read data file!\n", stderr); 978 fputs("[!] could not read data file!\n", stderr);
@@ -1014,7 +1015,7 @@ static int save_data(Tox *m)
1014 1015
1015 int res = 1; 1016 int res = 1;
1016 size_t size = tox_get_savedata_size(m); 1017 size_t size = tox_get_savedata_size(m);
1017 uint8_t data[size]; 1018 VLA(uint8_t, data, size);
1018 tox_get_savedata(m, data); 1019 tox_get_savedata(m, data);
1019 1020
1020 if (fwrite(data, sizeof(uint8_t), size, data_file) != size) { 1021 if (fwrite(data, sizeof(uint8_t), size, data_file) != size) {
@@ -1080,8 +1081,9 @@ static void print_groupchatpeers(Tox *m, int groupnumber)
1080 return; 1081 return;
1081 } 1082 }
1082 1083
1083 uint8_t names[num][TOX_MAX_NAME_LENGTH]; 1084 typedef uint8_t Peer_Name[TOX_MAX_NAME_LENGTH];
1084 size_t lengths[num]; 1085 VLA(Peer_Name, names, num);
1086 VLA(size_t, lengths, num);
1085 1087
1086 uint32_t i; 1088 uint32_t i;
1087 1089
@@ -1126,7 +1128,7 @@ static void print_groupmessage(Tox *m, uint32_t groupnumber, uint32_t peernumber
1126 const uint8_t *message, size_t length, 1128 const uint8_t *message, size_t length,
1127 void *userdata) 1129 void *userdata)
1128{ 1130{
1129 char msg[256 + length]; 1131 VLA(char, msg, 256 + length);
1130 1132
1131 TOX_ERR_CONFERENCE_PEER_QUERY error; 1133 TOX_ERR_CONFERENCE_PEER_QUERY error;
1132 size_t len = tox_conference_peer_get_name_size(m, groupnumber, peernumber, &error); 1134 size_t len = tox_conference_peer_get_name_size(m, groupnumber, peernumber, &error);
diff --git a/testing/tox_sync.c b/testing/tox_sync.c
index c719b3ec..d2fc41ca 100644
--- a/testing/tox_sync.c
+++ b/testing/tox_sync.c
@@ -34,6 +34,7 @@
34#include "config.h" 34#include "config.h"
35#endif 35#endif
36 36
37#include "../toxcore/ccompat.h"
37#include "../toxcore/tox.h" 38#include "../toxcore/tox.h"
38#include "misc_tools.c" 39#include "misc_tools.c"
39 40
@@ -73,7 +74,7 @@ static void tox_file_chunk_request(Tox *tox, uint32_t friend_number, uint32_t fi
73 } 74 }
74 75
75 fseek(file_senders[i].file, position, SEEK_SET); 76 fseek(file_senders[i].file, position, SEEK_SET);
76 uint8_t data[length]; 77 VLA(uint8_t, data, length);
77 int len = fread(data, 1, length, file_senders[i].file); 78 int len = fread(data, 1, length, file_senders[i].file);
78 tox_file_send_chunk(tox, friend_number, file_number, position, data, len, 0); 79 tox_file_send_chunk(tox, friend_number, file_number, position, data, len, 0);
79 break; 80 break;
@@ -314,7 +315,7 @@ int main(int argc, char *argv[])
314 315
315 if (d) { 316 if (d) {
316 while ((dir = readdir(d)) != NULL) { 317 while ((dir = readdir(d)) != NULL) {
317 char filepath[strlen(path) + strlen(dir->d_name) + 1]; 318 VLA(char, filepath, strlen(path) + strlen(dir->d_name) + 1);
318 memcpy(filepath, path, strlen(path)); 319 memcpy(filepath, path, strlen(path));
319 memcpy(filepath + strlen(path), dir->d_name, strlen(dir->d_name) + 1); 320 memcpy(filepath + strlen(path), dir->d_name, strlen(dir->d_name) + 1);
320 stat(filepath, &statbuf); 321 stat(filepath, &statbuf);
diff --git a/toxav/groupav.c b/toxav/groupav.c
index 6d84b480..2e77ec53 100644
--- a/toxav/groupav.c
+++ b/toxav/groupav.c
@@ -510,14 +510,14 @@ static int send_audio_packet(Group_Chats *g_c, int groupnumber, uint8_t *packet,
510 } 510 }
511 511
512 Group_AV *group_av = (Group_AV *)group_get_object(g_c, groupnumber); 512 Group_AV *group_av = (Group_AV *)group_get_object(g_c, groupnumber);
513 uint8_t data[1 + sizeof(uint16_t) + length]; 513 VLA(uint8_t, data, 1 + sizeof(uint16_t) + length);
514 data[0] = GROUP_AUDIO_PACKET_ID; 514 data[0] = GROUP_AUDIO_PACKET_ID;
515 515
516 uint16_t sequnum = htons(group_av->audio_sequnum); 516 uint16_t sequnum = htons(group_av->audio_sequnum);
517 memcpy(data + 1, &sequnum, sizeof(sequnum)); 517 memcpy(data + 1, &sequnum, sizeof(sequnum));
518 memcpy(data + 1 + sizeof(sequnum), packet, length); 518 memcpy(data + 1 + sizeof(sequnum), packet, length);
519 519
520 if (send_group_lossy_packet(g_c, groupnumber, data, sizeof(data)) == -1) { 520 if (send_group_lossy_packet(g_c, groupnumber, data, SIZEOF_VLA(data)) == -1) {
521 return -1; 521 return -1;
522 } 522 }
523 523
diff --git a/toxav/rtp.c b/toxav/rtp.c
index 9b7b1bfe..9403a43d 100644
--- a/toxav/rtp.c
+++ b/toxav/rtp.c
@@ -116,12 +116,12 @@ int rtp_send_data(RTPSession *session, const uint8_t *data, uint16_t length, Log
116 return -1; 116 return -1;
117 } 117 }
118 118
119 uint8_t rdata[length + sizeof(struct RTPHeader) + 1]; 119 VLA(uint8_t, rdata, length + sizeof(struct RTPHeader) + 1);
120 memset(rdata, 0, sizeof(rdata)); 120 memset(rdata, 0, SIZEOF_VLA(rdata));
121 121
122 rdata[0] = session->payload_type; 122 rdata[0] = session->payload_type;
123 123
124 struct RTPHeader *header = (struct RTPHeader *)(rdata + 1); 124 struct RTPHeader *header = (struct RTPHeader *)(rdata + 1);
125 125
126 header->ve = 2; 126 header->ve = 2;
127 header->pe = 0; 127 header->pe = 0;
@@ -147,8 +147,8 @@ int rtp_send_data(RTPSession *session, const uint8_t *data, uint16_t length, Log
147 147
148 memcpy(rdata + 1 + sizeof(struct RTPHeader), data, length); 148 memcpy(rdata + 1 + sizeof(struct RTPHeader), data, length);
149 149
150 if (-1 == m_send_custom_lossy_packet(session->m, session->friend_number, rdata, sizeof(rdata))) { 150 if (-1 == m_send_custom_lossy_packet(session->m, session->friend_number, rdata, SIZEOF_VLA(rdata))) {
151 LOGGER_WARNING(session->m->log, "RTP send failed (len: %d)! std error: %s", sizeof(rdata), strerror(errno)); 151 LOGGER_WARNING(session->m->log, "RTP send failed (len: %d)! std error: %s", SIZEOF_VLA(rdata), strerror(errno));
152 } 152 }
153 } else { 153 } else {
154 154
diff --git a/toxav/toxav.c b/toxav/toxav.c
index 1710f2a4..58db4597 100644
--- a/toxav/toxav.c
+++ b/toxav/toxav.c
@@ -704,12 +704,12 @@ bool toxav_audio_send_frame(ToxAV *av, uint32_t friend_number, const int16_t *pc
704 goto END; 704 goto END;
705 } 705 }
706 706
707 uint8_t dest[sample_count + sizeof(sampling_rate)]; /* This is more than enough always */ 707 VLA(uint8_t, dest, sample_count + sizeof(sampling_rate)); /* This is more than enough always */
708 708
709 sampling_rate = htonl(sampling_rate); 709 sampling_rate = htonl(sampling_rate);
710 memcpy(dest, &sampling_rate, sizeof(sampling_rate)); 710 memcpy(dest, &sampling_rate, sizeof(sampling_rate));
711 int vrc = opus_encode(call->audio.second->encoder, pcm, sample_count, 711 int vrc = opus_encode(call->audio.second->encoder, pcm, sample_count,
712 dest + sizeof(sampling_rate), sizeof(dest) - sizeof(sampling_rate)); 712 dest + sizeof(sampling_rate), SIZEOF_VLA(dest) - sizeof(sampling_rate));
713 713
714 if (vrc < 0) { 714 if (vrc < 0) {
715 LOGGER_WARNING(av->m->log, "Failed to encode frame %s", opus_strerror(vrc)); 715 LOGGER_WARNING(av->m->log, "Failed to encode frame %s", opus_strerror(vrc));
diff --git a/toxcore/DHT.c b/toxcore/DHT.c
index 5f3a1d07..227c6175 100644
--- a/toxcore/DHT.c
+++ b/toxcore/DHT.c
@@ -361,7 +361,7 @@ static int pack_ip_port(uint8_t *data, uint16_t length, const IP_Port *ip_port)
361static int DHT_create_packet(const uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE], 361static int DHT_create_packet(const uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE],
362 const uint8_t *shared_key, const uint8_t type, uint8_t *plain, size_t plain_length, uint8_t *packet) 362 const uint8_t *shared_key, const uint8_t type, uint8_t *plain, size_t plain_length, uint8_t *packet)
363{ 363{
364 uint8_t encrypted[plain_length + CRYPTO_MAC_SIZE]; 364 VLA(uint8_t, encrypted, plain_length + CRYPTO_MAC_SIZE);
365 uint8_t nonce[CRYPTO_NONCE_SIZE]; 365 uint8_t nonce[CRYPTO_NONCE_SIZE];
366 366
367 random_nonce(nonce); 367 random_nonce(nonce);
@@ -1264,7 +1264,7 @@ static int sendnodes_ipv6(const DHT *dht, IP_Port ip_port, const uint8_t *public
1264 Node_format nodes_list[MAX_SENT_NODES]; 1264 Node_format nodes_list[MAX_SENT_NODES];
1265 uint32_t num_nodes = get_close_nodes(dht, client_id, nodes_list, 0, LAN_ip(ip_port.ip) == 0, 1); 1265 uint32_t num_nodes = get_close_nodes(dht, client_id, nodes_list, 0, LAN_ip(ip_port.ip) == 0, 1);
1266 1266
1267 uint8_t plain[1 + Node_format_size * MAX_SENT_NODES + length]; 1267 VLA(uint8_t, plain, 1 + Node_format_size * MAX_SENT_NODES + length);
1268 1268
1269 int nodes_length = 0; 1269 int nodes_length = 0;
1270 1270
@@ -1279,13 +1279,13 @@ static int sendnodes_ipv6(const DHT *dht, IP_Port ip_port, const uint8_t *public
1279 plain[0] = num_nodes; 1279 plain[0] = num_nodes;
1280 memcpy(plain + 1 + nodes_length, sendback_data, length); 1280 memcpy(plain + 1 + nodes_length, sendback_data, length);
1281 1281
1282 uint8_t data[1 + nodes_length + length + 1 + CRYPTO_PUBLIC_KEY_SIZE 1282 VLA(uint8_t, data, 1 + nodes_length + length + 1 + CRYPTO_PUBLIC_KEY_SIZE
1283 + CRYPTO_NONCE_SIZE + CRYPTO_MAC_SIZE]; 1283 + CRYPTO_NONCE_SIZE + CRYPTO_MAC_SIZE);
1284 1284
1285 int len = DHT_create_packet(dht->self_public_key, shared_encryption_key, NET_PACKET_SEND_NODES_IPV6, 1285 int len = DHT_create_packet(dht->self_public_key, shared_encryption_key, NET_PACKET_SEND_NODES_IPV6,
1286 plain, 1 + nodes_length + length, data); 1286 plain, 1 + nodes_length + length, data);
1287 1287
1288 if (len != sizeof(data)) { 1288 if (len != SIZEOF_VLA(data)) {
1289 return -1; 1289 return -1;
1290 } 1290 }
1291 1291
@@ -1375,7 +1375,7 @@ static int handle_sendnodes_core(void *object, IP_Port source, const uint8_t *pa
1375 return 1; 1375 return 1;
1376 } 1376 }
1377 1377
1378 uint8_t plain[1 + data_size + sizeof(uint64_t)]; 1378 VLA(uint8_t, plain, 1 + data_size + sizeof(uint64_t));
1379 uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE]; 1379 uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE];
1380 DHT_get_shared_key_sent(dht, shared_key, packet + 1); 1380 DHT_get_shared_key_sent(dht, shared_key, packet + 1);
1381 int len = decrypt_data_symmetric( 1381 int len = decrypt_data_symmetric(
@@ -1385,7 +1385,7 @@ static int handle_sendnodes_core(void *object, IP_Port source, const uint8_t *pa
1385 1 + data_size + sizeof(uint64_t) + CRYPTO_MAC_SIZE, 1385 1 + data_size + sizeof(uint64_t) + CRYPTO_MAC_SIZE,
1386 plain); 1386 plain);
1387 1387
1388 if ((unsigned int)len != sizeof(plain)) { 1388 if ((unsigned int)len != SIZEOF_VLA(plain)) {
1389 return 1; 1389 return 1;
1390 } 1390 }
1391 1391
@@ -1598,8 +1598,8 @@ static uint8_t do_ping_and_sendnode_requests(DHT *dht, uint64_t *lastgetnode, co
1598 uint64_t temp_time = unix_time(); 1598 uint64_t temp_time = unix_time();
1599 1599
1600 uint32_t num_nodes = 0; 1600 uint32_t num_nodes = 0;
1601 Client_data *client_list[list_count * 2]; 1601 VLA(Client_data *, client_list, list_count * 2);
1602 IPPTsPng *assoc_list[list_count * 2]; 1602 VLA(IPPTsPng *, assoc_list, list_count * 2);
1603 unsigned int sort = 0; 1603 unsigned int sort = 0;
1604 bool sort_ok = 0; 1604 bool sort_ok = 0;
1605 1605
@@ -2247,12 +2247,12 @@ static int send_hardening_getnode_res(const DHT *dht, const Node_format *sendto,
2247 } 2247 }
2248 2248
2249 uint8_t packet[MAX_CRYPTO_REQUEST_SIZE]; 2249 uint8_t packet[MAX_CRYPTO_REQUEST_SIZE];
2250 uint8_t data[1 + CRYPTO_PUBLIC_KEY_SIZE + nodes_data_length]; 2250 VLA(uint8_t, data, 1 + CRYPTO_PUBLIC_KEY_SIZE + nodes_data_length);
2251 data[0] = CHECK_TYPE_GETNODE_RES; 2251 data[0] = CHECK_TYPE_GETNODE_RES;
2252 memcpy(data + 1, queried_client_id, CRYPTO_PUBLIC_KEY_SIZE); 2252 memcpy(data + 1, queried_client_id, CRYPTO_PUBLIC_KEY_SIZE);
2253 memcpy(data + 1 + CRYPTO_PUBLIC_KEY_SIZE, nodes_data, nodes_data_length); 2253 memcpy(data + 1 + CRYPTO_PUBLIC_KEY_SIZE, nodes_data, nodes_data_length);
2254 int len = create_request(dht->self_public_key, dht->self_secret_key, packet, sendto->public_key, data, 2254 int len = create_request(dht->self_public_key, dht->self_secret_key, packet, sendto->public_key, data,
2255 sizeof(data), CRYPTO_PACKET_HARDENING); 2255 SIZEOF_VLA(data), CRYPTO_PACKET_HARDENING);
2256 2256
2257 if (len == -1) { 2257 if (len == -1) {
2258 return -1; 2258 return -1;
diff --git a/toxcore/Makefile.inc b/toxcore/Makefile.inc
index f01a5d62..2ca25650 100644
--- a/toxcore/Makefile.inc
+++ b/toxcore/Makefile.inc
@@ -5,7 +5,8 @@ libtoxcore_la_include_HEADERS = \
5 5
6libtoxcore_la_includedir = $(includedir)/tox 6libtoxcore_la_includedir = $(includedir)/tox
7 7
8libtoxcore_la_SOURCES = ../toxcore/DHT.h \ 8libtoxcore_la_SOURCES = ../toxcore/ccompat.h \
9 ../toxcore/DHT.h \
9 ../toxcore/DHT.c \ 10 ../toxcore/DHT.c \
10 ../toxcore/network.h \ 11 ../toxcore/network.h \
11 ../toxcore/network.c \ 12 ../toxcore/network.c \
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c
index 8291fab0..b13465ed 100644
--- a/toxcore/Messenger.c
+++ b/toxcore/Messenger.c
@@ -503,7 +503,7 @@ int m_send_message_generic(Messenger *m, int32_t friendnumber, uint8_t type, con
503 return -3; 503 return -3;
504 } 504 }
505 505
506 uint8_t packet[length + 1]; 506 VLA(uint8_t, packet, length + 1);
507 packet[0] = type + PACKET_ID_MESSAGE; 507 packet[0] = type + PACKET_ID_MESSAGE;
508 508
509 if (length != 0) { 509 if (length != 0) {
@@ -965,7 +965,7 @@ static int write_cryptpacket_id(const Messenger *m, int32_t friendnumber, uint8_
965 return 0; 965 return 0;
966 } 966 }
967 967
968 uint8_t packet[length + 1]; 968 VLA(uint8_t, packet, length + 1);
969 packet[0] = packet_id; 969 packet[0] = packet_id;
970 970
971 if (length != 0) { 971 if (length != 0) {
@@ -1111,7 +1111,7 @@ static int file_sendrequest(const Messenger *m, int32_t friendnumber, uint8_t fi
1111 return 0; 1111 return 0;
1112 } 1112 }
1113 1113
1114 uint8_t packet[1 + sizeof(file_type) + sizeof(filesize) + FILE_ID_LENGTH + filename_length]; 1114 VLA(uint8_t, packet, 1 + sizeof(file_type) + sizeof(filesize) + FILE_ID_LENGTH + filename_length);
1115 packet[0] = filenumber; 1115 packet[0] = filenumber;
1116 file_type = htonl(file_type); 1116 file_type = htonl(file_type);
1117 memcpy(packet + 1, &file_type, sizeof(file_type)); 1117 memcpy(packet + 1, &file_type, sizeof(file_type));
@@ -1123,7 +1123,7 @@ static int file_sendrequest(const Messenger *m, int32_t friendnumber, uint8_t fi
1123 memcpy(packet + 1 + sizeof(file_type) + sizeof(filesize) + FILE_ID_LENGTH, filename, filename_length); 1123 memcpy(packet + 1 + sizeof(file_type) + sizeof(filesize) + FILE_ID_LENGTH, filename, filename_length);
1124 } 1124 }
1125 1125
1126 return write_cryptpacket_id(m, friendnumber, PACKET_ID_FILE_SENDREQUEST, packet, sizeof(packet), 0); 1126 return write_cryptpacket_id(m, friendnumber, PACKET_ID_FILE_SENDREQUEST, packet, SIZEOF_VLA(packet), 0);
1127} 1127}
1128 1128
1129/* Send a file send request. 1129/* Send a file send request.
@@ -1190,7 +1190,7 @@ static int send_file_control_packet(const Messenger *m, int32_t friendnumber, ui
1190 return -1; 1190 return -1;
1191 } 1191 }
1192 1192
1193 uint8_t packet[3 + data_length]; 1193 VLA(uint8_t, packet, 3 + data_length);
1194 1194
1195 packet[0] = send_receive; 1195 packet[0] = send_receive;
1196 packet[1] = filenumber; 1196 packet[1] = filenumber;
@@ -1200,7 +1200,7 @@ static int send_file_control_packet(const Messenger *m, int32_t friendnumber, ui
1200 memcpy(packet + 3, data, data_length); 1200 memcpy(packet + 3, data, data_length);
1201 } 1201 }
1202 1202
1203 return write_cryptpacket_id(m, friendnumber, PACKET_ID_FILE_CONTROL, packet, sizeof(packet), 0); 1203 return write_cryptpacket_id(m, friendnumber, PACKET_ID_FILE_CONTROL, packet, SIZEOF_VLA(packet), 0);
1204} 1204}
1205 1205
1206/* Send a file control request. 1206/* Send a file control request.
@@ -1378,7 +1378,7 @@ static int64_t send_file_data_packet(const Messenger *m, int32_t friendnumber, u
1378 return -1; 1378 return -1;
1379 } 1379 }
1380 1380
1381 uint8_t packet[2 + length]; 1381 VLA(uint8_t, packet, 2 + length);
1382 packet[0] = PACKET_ID_FILE_DATA; 1382 packet[0] = PACKET_ID_FILE_DATA;
1383 packet[1] = filenumber; 1383 packet[1] = filenumber;
1384 1384
@@ -1387,7 +1387,7 @@ static int64_t send_file_data_packet(const Messenger *m, int32_t friendnumber, u
1387 } 1387 }
1388 1388
1389 return write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c, 1389 return write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c,
1390 m->friendlist[friendnumber].friendcon_id), packet, sizeof(packet), 1); 1390 m->friendlist[friendnumber].friendcon_id), packet, SIZEOF_VLA(packet), 1);
1391} 1391}
1392 1392
1393#define MAX_FILE_DATA_SIZE (MAX_CRYPTO_DATA_SIZE - 2) 1393#define MAX_FILE_DATA_SIZE (MAX_CRYPTO_DATA_SIZE - 2)
@@ -2132,7 +2132,7 @@ static int handle_packet(void *object, int i, const uint8_t *temp, uint16_t len,
2132 } 2132 }
2133 2133
2134 /* Make sure the NULL terminator is present. */ 2134 /* Make sure the NULL terminator is present. */
2135 uint8_t data_terminated[data_length + 1]; 2135 VLA(uint8_t, data_terminated, data_length + 1);
2136 memcpy(data_terminated, data, data_length); 2136 memcpy(data_terminated, data, data_length);
2137 data_terminated[data_length] = 0; 2137 data_terminated[data_length] = 0;
2138 2138
@@ -2153,7 +2153,7 @@ static int handle_packet(void *object, int i, const uint8_t *temp, uint16_t len,
2153 } 2153 }
2154 2154
2155 /* Make sure the NULL terminator is present. */ 2155 /* Make sure the NULL terminator is present. */
2156 uint8_t data_terminated[data_length + 1]; 2156 VLA(uint8_t, data_terminated, data_length + 1);
2157 memcpy(data_terminated, data, data_length); 2157 memcpy(data_terminated, data, data_length);
2158 data_terminated[data_length] = 0; 2158 data_terminated[data_length] = 0;
2159 2159
@@ -2210,7 +2210,7 @@ static int handle_packet(void *object, int i, const uint8_t *temp, uint16_t len,
2210 uint16_t message_length = data_length; 2210 uint16_t message_length = data_length;
2211 2211
2212 /* Make sure the NULL terminator is present. */ 2212 /* Make sure the NULL terminator is present. */
2213 uint8_t message_terminated[message_length + 1]; 2213 VLA(uint8_t, message_terminated, message_length + 1);
2214 memcpy(message_terminated, message, message_length); 2214 memcpy(message_terminated, message, message_length);
2215 message_terminated[message_length] = 0; 2215 message_terminated[message_length] = 0;
2216 uint8_t type = packet_id - PACKET_ID_MESSAGE; 2216 uint8_t type = packet_id - PACKET_ID_MESSAGE;
@@ -2272,7 +2272,7 @@ static int handle_packet(void *object, int i, const uint8_t *temp, uint16_t len,
2272 ft->paused = FILE_PAUSE_NOT; 2272 ft->paused = FILE_PAUSE_NOT;
2273 memcpy(ft->id, data + 1 + sizeof(uint32_t) + sizeof(uint64_t), FILE_ID_LENGTH); 2273 memcpy(ft->id, data + 1 + sizeof(uint32_t) + sizeof(uint64_t), FILE_ID_LENGTH);
2274 2274
2275 uint8_t filename_terminated[filename_length + 1]; 2275 VLA(uint8_t, filename_terminated, filename_length + 1);
2276 uint8_t *filename = NULL; 2276 uint8_t *filename = NULL;
2277 2277
2278 if (filename_length) { 2278 if (filename_length) {
@@ -2584,8 +2584,8 @@ void do_messenger(Messenger *m, void *userdata)
2584 2584
2585 /* dht contains additional "friends" (requests) */ 2585 /* dht contains additional "friends" (requests) */
2586 uint32_t num_dhtfriends = m->dht->num_friends; 2586 uint32_t num_dhtfriends = m->dht->num_friends;
2587 int32_t m2dht[num_dhtfriends]; 2587 VLA(int32_t, m2dht, num_dhtfriends);
2588 int32_t dht2m[num_dhtfriends]; 2588 VLA(int32_t, dht2m, num_dhtfriends);
2589 2589
2590 for (friend_idx = 0; friend_idx < num_dhtfriends; friend_idx++) { 2590 for (friend_idx = 0; friend_idx < num_dhtfriends; friend_idx++) {
2591 m2dht[friend_idx] = -1; 2591 m2dht[friend_idx] = -1;
diff --git a/toxcore/TCP_client.c b/toxcore/TCP_client.c
index 3d65415e..0910534d 100644
--- a/toxcore/TCP_client.c
+++ b/toxcore/TCP_client.c
@@ -120,7 +120,7 @@ static int proxy_http_read_connection_response(TCP_Client_Connection *TCP_conn)
120 unsigned int data_left = TCP_socket_data_recv_buffer(TCP_conn->sock); 120 unsigned int data_left = TCP_socket_data_recv_buffer(TCP_conn->sock);
121 121
122 if (data_left) { 122 if (data_left) {
123 uint8_t temp_data[data_left]; 123 VLA(uint8_t, temp_data, data_left);
124 read_TCP_packet(TCP_conn->sock, temp_data, data_left); 124 read_TCP_packet(TCP_conn->sock, temp_data, data_left);
125 } 125 }
126 126
@@ -387,18 +387,18 @@ static int write_packet_TCP_secure_connection(TCP_Client_Connection *con, const
387 } 387 }
388 } 388 }
389 389
390 uint8_t packet[sizeof(uint16_t) + length + CRYPTO_MAC_SIZE]; 390 VLA(uint8_t, packet, sizeof(uint16_t) + length + CRYPTO_MAC_SIZE);
391 391
392 uint16_t c_length = htons(length + CRYPTO_MAC_SIZE); 392 uint16_t c_length = htons(length + CRYPTO_MAC_SIZE);
393 memcpy(packet, &c_length, sizeof(uint16_t)); 393 memcpy(packet, &c_length, sizeof(uint16_t));
394 int len = encrypt_data_symmetric(con->shared_key, con->sent_nonce, data, length, packet + sizeof(uint16_t)); 394 int len = encrypt_data_symmetric(con->shared_key, con->sent_nonce, data, length, packet + sizeof(uint16_t));
395 395
396 if ((unsigned int)len != (sizeof(packet) - sizeof(uint16_t))) { 396 if ((unsigned int)len != (SIZEOF_VLA(packet) - sizeof(uint16_t))) {
397 return -1; 397 return -1;
398 } 398 }
399 399
400 if (priority) { 400 if (priority) {
401 len = sendpriority ? send(con->sock, (const char *)packet, sizeof(packet), MSG_NOSIGNAL) : 0; 401 len = sendpriority ? send(con->sock, (const char *)packet, SIZEOF_VLA(packet), MSG_NOSIGNAL) : 0;
402 402
403 if (len <= 0) { 403 if (len <= 0) {
404 len = 0; 404 len = 0;
@@ -406,14 +406,14 @@ static int write_packet_TCP_secure_connection(TCP_Client_Connection *con, const
406 406
407 increment_nonce(con->sent_nonce); 407 increment_nonce(con->sent_nonce);
408 408
409 if ((unsigned int)len == sizeof(packet)) { 409 if ((unsigned int)len == SIZEOF_VLA(packet)) {
410 return 1; 410 return 1;
411 } 411 }
412 412
413 return add_priority(con, packet, sizeof(packet), len); 413 return add_priority(con, packet, SIZEOF_VLA(packet), len);
414 } 414 }
415 415
416 len = send(con->sock, (const char *)packet, sizeof(packet), MSG_NOSIGNAL); 416 len = send(con->sock, (const char *)packet, SIZEOF_VLA(packet), MSG_NOSIGNAL);
417 417
418 if (len <= 0) { 418 if (len <= 0) {
419 return 0; 419 return 0;
@@ -421,12 +421,12 @@ static int write_packet_TCP_secure_connection(TCP_Client_Connection *con, const
421 421
422 increment_nonce(con->sent_nonce); 422 increment_nonce(con->sent_nonce);
423 423
424 if ((unsigned int)len == sizeof(packet)) { 424 if ((unsigned int)len == SIZEOF_VLA(packet)) {
425 return 1; 425 return 1;
426 } 426 }
427 427
428 memcpy(con->last_packet, packet, sizeof(packet)); 428 memcpy(con->last_packet, packet, SIZEOF_VLA(packet));
429 con->last_packet_length = sizeof(packet); 429 con->last_packet_length = SIZEOF_VLA(packet);
430 con->last_packet_sent = len; 430 con->last_packet_sent = len;
431 return 1; 431 return 1;
432} 432}
@@ -478,10 +478,10 @@ int send_data(TCP_Client_Connection *con, uint8_t con_id, const uint8_t *data, u
478 return 0; 478 return 0;
479 } 479 }
480 480
481 uint8_t packet[1 + length]; 481 VLA(uint8_t, packet, 1 + length);
482 packet[0] = con_id + NUM_RESERVED_PORTS; 482 packet[0] = con_id + NUM_RESERVED_PORTS;
483 memcpy(packet + 1, data, length); 483 memcpy(packet + 1, data, length);
484 return write_packet_TCP_secure_connection(con, packet, sizeof(packet), 0); 484 return write_packet_TCP_secure_connection(con, packet, SIZEOF_VLA(packet), 0);
485} 485}
486 486
487/* return 1 on success. 487/* return 1 on success.
@@ -494,11 +494,11 @@ int send_oob_packet(TCP_Client_Connection *con, const uint8_t *public_key, const
494 return -1; 494 return -1;
495 } 495 }
496 496
497 uint8_t packet[1 + CRYPTO_PUBLIC_KEY_SIZE + length]; 497 VLA(uint8_t, packet, 1 + CRYPTO_PUBLIC_KEY_SIZE + length);
498 packet[0] = TCP_PACKET_OOB_SEND; 498 packet[0] = TCP_PACKET_OOB_SEND;
499 memcpy(packet + 1, public_key, CRYPTO_PUBLIC_KEY_SIZE); 499 memcpy(packet + 1, public_key, CRYPTO_PUBLIC_KEY_SIZE);
500 memcpy(packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, data, length); 500 memcpy(packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, data, length);
501 return write_packet_TCP_secure_connection(con, packet, sizeof(packet), 0); 501 return write_packet_TCP_secure_connection(con, packet, SIZEOF_VLA(packet), 0);
502} 502}
503 503
504 504
@@ -614,10 +614,10 @@ int send_disconnect_request(TCP_Client_Connection *con, uint8_t con_id)
614 */ 614 */
615int send_onion_request(TCP_Client_Connection *con, const uint8_t *data, uint16_t length) 615int send_onion_request(TCP_Client_Connection *con, const uint8_t *data, uint16_t length)
616{ 616{
617 uint8_t packet[1 + length]; 617 VLA(uint8_t, packet, 1 + length);
618 packet[0] = TCP_PACKET_ONION_REQUEST; 618 packet[0] = TCP_PACKET_ONION_REQUEST;
619 memcpy(packet + 1, data, length); 619 memcpy(packet + 1, data, length);
620 return write_packet_TCP_secure_connection(con, packet, sizeof(packet), 0); 620 return write_packet_TCP_secure_connection(con, packet, SIZEOF_VLA(packet), 0);
621} 621}
622 622
623void onion_response_handler(TCP_Client_Connection *con, int (*onion_callback)(void *object, const uint8_t *data, 623void onion_response_handler(TCP_Client_Connection *con, int (*onion_callback)(void *object, const uint8_t *data,
diff --git a/toxcore/TCP_connection.c b/toxcore/TCP_connection.c
index 904b686e..18c1e76e 100644
--- a/toxcore/TCP_connection.c
+++ b/toxcore/TCP_connection.c
@@ -1436,7 +1436,10 @@ static void kill_nonused_tcp(TCP_Connections *tcp_c)
1436 return; 1436 return;
1437 } 1437 }
1438 1438
1439 unsigned int i, num_online = 0, num_kill = 0, to_kill[tcp_c->tcp_connections_length]; 1439 unsigned int i;
1440 unsigned int num_online = 0;
1441 unsigned int num_kill = 0;
1442 VLA(unsigned int, to_kill, tcp_c->tcp_connections_length);
1440 1443
1441 for (i = 0; i < tcp_c->tcp_connections_length; ++i) { 1444 for (i = 0; i < tcp_c->tcp_connections_length; ++i) {
1442 TCP_con *tcp_con = get_tcp_connection(tcp_c, i); 1445 TCP_con *tcp_con = get_tcp_connection(tcp_c, i);
diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c
index 47ad9933..10f64d38 100644
--- a/toxcore/TCP_server.c
+++ b/toxcore/TCP_server.c
@@ -325,7 +325,7 @@ int read_packet_TCP_secure_connection(Socket sock, uint16_t *next_packet_length,
325 return -1; 325 return -1;
326 } 326 }
327 327
328 uint8_t data_encrypted[*next_packet_length]; 328 VLA(uint8_t, data_encrypted, *next_packet_length);
329 int len_packet = read_TCP_packet(sock, data_encrypted, *next_packet_length); 329 int len_packet = read_TCP_packet(sock, data_encrypted, *next_packet_length);
330 330
331 if (len_packet != *next_packet_length) { 331 if (len_packet != *next_packet_length) {
@@ -458,18 +458,18 @@ static int write_packet_TCP_secure_connection(TCP_Secure_Connection *con, const
458 } 458 }
459 } 459 }
460 460
461 uint8_t packet[sizeof(uint16_t) + length + CRYPTO_MAC_SIZE]; 461 VLA(uint8_t, packet, sizeof(uint16_t) + length + CRYPTO_MAC_SIZE);
462 462
463 uint16_t c_length = htons(length + CRYPTO_MAC_SIZE); 463 uint16_t c_length = htons(length + CRYPTO_MAC_SIZE);
464 memcpy(packet, &c_length, sizeof(uint16_t)); 464 memcpy(packet, &c_length, sizeof(uint16_t));
465 int len = encrypt_data_symmetric(con->shared_key, con->sent_nonce, data, length, packet + sizeof(uint16_t)); 465 int len = encrypt_data_symmetric(con->shared_key, con->sent_nonce, data, length, packet + sizeof(uint16_t));
466 466
467 if ((unsigned int)len != (sizeof(packet) - sizeof(uint16_t))) { 467 if ((unsigned int)len != (SIZEOF_VLA(packet) - sizeof(uint16_t))) {
468 return -1; 468 return -1;
469 } 469 }
470 470
471 if (priority) { 471 if (priority) {
472 len = sendpriority ? send(con->sock, (const char *)packet, sizeof(packet), MSG_NOSIGNAL) : 0; 472 len = sendpriority ? send(con->sock, (const char *)packet, SIZEOF_VLA(packet), MSG_NOSIGNAL) : 0;
473 473
474 if (len <= 0) { 474 if (len <= 0) {
475 len = 0; 475 len = 0;
@@ -477,14 +477,14 @@ static int write_packet_TCP_secure_connection(TCP_Secure_Connection *con, const
477 477
478 increment_nonce(con->sent_nonce); 478 increment_nonce(con->sent_nonce);
479 479
480 if ((unsigned int)len == sizeof(packet)) { 480 if ((unsigned int)len == SIZEOF_VLA(packet)) {
481 return 1; 481 return 1;
482 } 482 }
483 483
484 return add_priority(con, packet, sizeof(packet), len); 484 return add_priority(con, packet, SIZEOF_VLA(packet), len);
485 } 485 }
486 486
487 len = send(con->sock, (const char *)packet, sizeof(packet), MSG_NOSIGNAL); 487 len = send(con->sock, (const char *)packet, SIZEOF_VLA(packet), MSG_NOSIGNAL);
488 488
489 if (len <= 0) { 489 if (len <= 0) {
490 return 0; 490 return 0;
@@ -492,12 +492,12 @@ static int write_packet_TCP_secure_connection(TCP_Secure_Connection *con, const
492 492
493 increment_nonce(con->sent_nonce); 493 increment_nonce(con->sent_nonce);
494 494
495 if ((unsigned int)len == sizeof(packet)) { 495 if ((unsigned int)len == SIZEOF_VLA(packet)) {
496 return 1; 496 return 1;
497 } 497 }
498 498
499 memcpy(con->last_packet, packet, sizeof(packet)); 499 memcpy(con->last_packet, packet, SIZEOF_VLA(packet));
500 con->last_packet_length = sizeof(packet); 500 con->last_packet_length = SIZEOF_VLA(packet);
501 con->last_packet_sent = len; 501 con->last_packet_sent = len;
502 return 1; 502 return 1;
503} 503}
@@ -737,12 +737,12 @@ static int handle_TCP_oob_send(TCP_Server *TCP_server, uint32_t con_id, const ui
737 int other_index = get_TCP_connection_index(TCP_server, public_key); 737 int other_index = get_TCP_connection_index(TCP_server, public_key);
738 738
739 if (other_index != -1) { 739 if (other_index != -1) {
740 uint8_t resp_packet[1 + CRYPTO_PUBLIC_KEY_SIZE + length]; 740 VLA(uint8_t, resp_packet, 1 + CRYPTO_PUBLIC_KEY_SIZE + length);
741 resp_packet[0] = TCP_PACKET_OOB_RECV; 741 resp_packet[0] = TCP_PACKET_OOB_RECV;
742 memcpy(resp_packet + 1, con->public_key, CRYPTO_PUBLIC_KEY_SIZE); 742 memcpy(resp_packet + 1, con->public_key, CRYPTO_PUBLIC_KEY_SIZE);
743 memcpy(resp_packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, data, length); 743 memcpy(resp_packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, data, length);
744 write_packet_TCP_secure_connection(&TCP_server->accepted_connection_array[other_index], resp_packet, 744 write_packet_TCP_secure_connection(&TCP_server->accepted_connection_array[other_index], resp_packet,
745 sizeof(resp_packet), 0); 745 SIZEOF_VLA(resp_packet), 0);
746 } 746 }
747 747
748 return 0; 748 return 0;
@@ -800,11 +800,11 @@ static int handle_onion_recv_1(void *object, IP_Port dest, const uint8_t *data,
800 return 1; 800 return 1;
801 } 801 }
802 802
803 uint8_t packet[1 + length]; 803 VLA(uint8_t, packet, 1 + length);
804 memcpy(packet + 1, data, length); 804 memcpy(packet + 1, data, length);
805 packet[0] = TCP_PACKET_ONION_RESPONSE; 805 packet[0] = TCP_PACKET_ONION_RESPONSE;
806 806
807 if (write_packet_TCP_secure_connection(con, packet, sizeof(packet), 0) != 1) { 807 if (write_packet_TCP_secure_connection(con, packet, SIZEOF_VLA(packet), 0) != 1) {
808 return 1; 808 return 1;
809 } 809 }
810 810
@@ -931,7 +931,7 @@ static int handle_TCP_packet(TCP_Server *TCP_server, uint32_t con_id, const uint
931 931
932 uint32_t index = con->connections[c_id].index; 932 uint32_t index = con->connections[c_id].index;
933 uint8_t other_c_id = con->connections[c_id].other_id + NUM_RESERVED_PORTS; 933 uint8_t other_c_id = con->connections[c_id].other_id + NUM_RESERVED_PORTS;
934 uint8_t new_data[length]; 934 VLA(uint8_t, new_data, length);
935 memcpy(new_data, data, length); 935 memcpy(new_data, data, length);
936 new_data[0] = other_c_id; 936 new_data[0] = other_c_id;
937 int ret = write_packet_TCP_secure_connection(&TCP_server->accepted_connection_array[index], new_data, length, 0); 937 int ret = write_packet_TCP_secure_connection(&TCP_server->accepted_connection_array[index], new_data, length, 0);
diff --git a/toxcore/ccompat.h b/toxcore/ccompat.h
new file mode 100644
index 00000000..e72e66ae
--- /dev/null
+++ b/toxcore/ccompat.h
@@ -0,0 +1,43 @@
1/*
2 * C language compatibility macros for varying compiler support.
3 */
4#ifndef CCOMPAT_H
5#define CCOMPAT_H
6
7// Marking GNU extensions to avoid warnings.
8#if defined(__GNUC__)
9#define GNU_EXTENSION __extension__
10#else
11#define GNU_EXTENSION
12#endif
13
14// Variable length arrays.
15// VLA(type, name, size) allocates a variable length array with automatic
16// storage duration. VLA_SIZE(name) evaluates to the runtime size of that array
17// in bytes.
18//
19// If C99 VLAs are not available, an emulation using alloca (stack allocation
20// "function") is used. Note the semantic difference: alloca'd memory does not
21// get freed at the end of the declaration's scope. Do not use VLA() in loops or
22// you may run out of stack space.
23#if !defined(_MSC_VER) && __STDC_VERSION__ >= 199901L
24// C99 VLAs.
25#define VLA(type, name, size) type name[size]
26#define SIZEOF_VLA sizeof
27#else
28
29// Emulation using alloca.
30#ifdef _WIN32
31#include <malloc.h>
32#else
33#include <alloca.h>
34#endif
35
36#define VLA(type, name, size) \
37 const size_t name##_size = (size) * sizeof(type); \
38 type *const name = (type *)alloca(name##_size)
39#define SIZEOF_VLA(name) name##_size
40
41#endif
42
43#endif /* CCOMPAT_H */
diff --git a/toxcore/crypto_core.c b/toxcore/crypto_core.c
index e114b44f..03150396 100644
--- a/toxcore/crypto_core.c
+++ b/toxcore/crypto_core.c
@@ -127,8 +127,8 @@ int32_t encrypt_data_symmetric(const uint8_t *secret_key, const uint8_t *nonce,
127 return -1; 127 return -1;
128 } 128 }
129 129
130 uint8_t temp_plain[length + crypto_box_ZEROBYTES]; 130 VLA(uint8_t, temp_plain, length + crypto_box_ZEROBYTES);
131 uint8_t temp_encrypted[length + crypto_box_MACBYTES + crypto_box_BOXZEROBYTES]; 131 VLA(uint8_t, temp_encrypted, length + crypto_box_MACBYTES + crypto_box_BOXZEROBYTES);
132 132
133 memset(temp_plain, 0, crypto_box_ZEROBYTES); 133 memset(temp_plain, 0, crypto_box_ZEROBYTES);
134 memcpy(temp_plain + crypto_box_ZEROBYTES, plain, length); // Pad the message with 32 0 bytes. 134 memcpy(temp_plain + crypto_box_ZEROBYTES, plain, length); // Pad the message with 32 0 bytes.
@@ -149,8 +149,8 @@ int32_t decrypt_data_symmetric(const uint8_t *secret_key, const uint8_t *nonce,
149 return -1; 149 return -1;
150 } 150 }
151 151
152 uint8_t temp_plain[length + crypto_box_ZEROBYTES]; 152 VLA(uint8_t, temp_plain, length + crypto_box_ZEROBYTES);
153 uint8_t temp_encrypted[length + crypto_box_BOXZEROBYTES]; 153 VLA(uint8_t, temp_encrypted, length + crypto_box_BOXZEROBYTES);
154 154
155 memset(temp_encrypted, 0, crypto_box_BOXZEROBYTES); 155 memset(temp_encrypted, 0, crypto_box_BOXZEROBYTES);
156 memcpy(temp_encrypted + crypto_box_BOXZEROBYTES, encrypted, length); // Pad the message with 16 0 bytes. 156 memcpy(temp_encrypted + crypto_box_BOXZEROBYTES, encrypted, length); // Pad the message with 16 0 bytes.
diff --git a/toxcore/friend_connection.c b/toxcore/friend_connection.c
index 2fb768ea..a63855af 100644
--- a/toxcore/friend_connection.c
+++ b/toxcore/friend_connection.c
@@ -791,17 +791,17 @@ int send_friend_request_packet(Friend_Connections *fr_c, int friendcon_id, uint3
791 return -1; 791 return -1;
792 } 792 }
793 793
794 uint8_t packet[1 + sizeof(nospam_num) + length]; 794 VLA(uint8_t, packet, 1 + sizeof(nospam_num) + length);
795 memcpy(packet + 1, &nospam_num, sizeof(nospam_num)); 795 memcpy(packet + 1, &nospam_num, sizeof(nospam_num));
796 memcpy(packet + 1 + sizeof(nospam_num), data, length); 796 memcpy(packet + 1 + sizeof(nospam_num), data, length);
797 797
798 if (friend_con->status == FRIENDCONN_STATUS_CONNECTED) { 798 if (friend_con->status == FRIENDCONN_STATUS_CONNECTED) {
799 packet[0] = PACKET_ID_FRIEND_REQUESTS; 799 packet[0] = PACKET_ID_FRIEND_REQUESTS;
800 return write_cryptpacket(fr_c->net_crypto, friend_con->crypt_connection_id, packet, sizeof(packet), 0) != -1; 800 return write_cryptpacket(fr_c->net_crypto, friend_con->crypt_connection_id, packet, SIZEOF_VLA(packet), 0) != -1;
801 } 801 }
802 802
803 packet[0] = CRYPTO_PACKET_FRIEND_REQ; 803 packet[0] = CRYPTO_PACKET_FRIEND_REQ;
804 int num = send_onion_data(fr_c->onion_c, friend_con->onion_friendnum, packet, sizeof(packet)); 804 int num = send_onion_data(fr_c->onion_c, friend_con->onion_friendnum, packet, SIZEOF_VLA(packet));
805 805
806 if (num <= 0) { 806 if (num <= 0) {
807 return -1; 807 return -1;
diff --git a/toxcore/friend_requests.c b/toxcore/friend_requests.c
index eedd2051..ba782e2b 100644
--- a/toxcore/friend_requests.c
+++ b/toxcore/friend_requests.c
@@ -138,9 +138,9 @@ static int friendreq_handlepacket(void *object, const uint8_t *source_pubkey, co
138 addto_receivedlist(fr, source_pubkey); 138 addto_receivedlist(fr, source_pubkey);
139 139
140 uint32_t message_len = length - sizeof(fr->nospam); 140 uint32_t message_len = length - sizeof(fr->nospam);
141 uint8_t message[message_len + 1]; 141 VLA(uint8_t, message, message_len + 1);
142 memcpy(message, packet + sizeof(fr->nospam), message_len); 142 memcpy(message, packet + sizeof(fr->nospam), message_len);
143 message[sizeof(message) - 1] = 0; /* Be sure the message is null terminated. */ 143 message[SIZEOF_VLA(message) - 1] = 0; /* Be sure the message is null terminated. */
144 144
145 (*fr->handle_friendrequest)(fr->handle_friendrequest_object, source_pubkey, message, message_len, userdata); 145 (*fr->handle_friendrequest)(fr->handle_friendrequest_object, source_pubkey, message, message_len, userdata);
146 return 0; 146 return 0;
diff --git a/toxcore/group.c b/toxcore/group.c
index c4af8fa2..5d8a6ac2 100644
--- a/toxcore/group.c
+++ b/toxcore/group.c
@@ -969,12 +969,12 @@ static unsigned int send_packet_group_peer(Friend_Connections *fr_c, int friendc
969 } 969 }
970 970
971 group_num = htons(group_num); 971 group_num = htons(group_num);
972 uint8_t packet[1 + sizeof(uint16_t) + length]; 972 VLA(uint8_t, packet, 1 + sizeof(uint16_t) + length);
973 packet[0] = packet_id; 973 packet[0] = packet_id;
974 memcpy(packet + 1, &group_num, sizeof(uint16_t)); 974 memcpy(packet + 1, &group_num, sizeof(uint16_t));
975 memcpy(packet + 1 + sizeof(uint16_t), data, length); 975 memcpy(packet + 1 + sizeof(uint16_t), data, length);
976 return write_cryptpacket(fr_c->net_crypto, friend_connection_crypt_connection_id(fr_c, friendcon_id), packet, 976 return write_cryptpacket(fr_c->net_crypto, friend_connection_crypt_connection_id(fr_c, friendcon_id), packet,
977 sizeof(packet), 0) != -1; 977 SIZEOF_VLA(packet), 0) != -1;
978} 978}
979 979
980/* Send a group lossy packet to friendcon_id. 980/* Send a group lossy packet to friendcon_id.
@@ -990,12 +990,12 @@ static unsigned int send_lossy_group_peer(Friend_Connections *fr_c, int friendco
990 } 990 }
991 991
992 group_num = htons(group_num); 992 group_num = htons(group_num);
993 uint8_t packet[1 + sizeof(uint16_t) + length]; 993 VLA(uint8_t, packet, 1 + sizeof(uint16_t) + length);
994 packet[0] = packet_id; 994 packet[0] = packet_id;
995 memcpy(packet + 1, &group_num, sizeof(uint16_t)); 995 memcpy(packet + 1, &group_num, sizeof(uint16_t));
996 memcpy(packet + 1 + sizeof(uint16_t), data, length); 996 memcpy(packet + 1 + sizeof(uint16_t), data, length);
997 return send_lossy_cryptpacket(fr_c->net_crypto, friend_connection_crypt_connection_id(fr_c, friendcon_id), packet, 997 return send_lossy_cryptpacket(fr_c->net_crypto, friend_connection_crypt_connection_id(fr_c, friendcon_id), packet,
998 sizeof(packet)) != -1; 998 SIZEOF_VLA(packet)) != -1;
999} 999}
1000 1000
1001#define INVITE_PACKET_SIZE (1 + sizeof(uint16_t) + GROUP_IDENTIFIER_LENGTH) 1001#define INVITE_PACKET_SIZE (1 + sizeof(uint16_t) + GROUP_IDENTIFIER_LENGTH)
@@ -1646,10 +1646,10 @@ static unsigned int send_peers(Group_Chats *g_c, int groupnumber, int friendcon_
1646 } 1646 }
1647 1647
1648 if (g->title_len) { 1648 if (g->title_len) {
1649 uint8_t Packet[1 + g->title_len]; 1649 VLA(uint8_t, Packet, 1 + g->title_len);
1650 Packet[0] = PEER_TITLE_ID; 1650 Packet[0] = PEER_TITLE_ID;
1651 memcpy(Packet + 1, g->title, g->title_len); 1651 memcpy(Packet + 1, g->title, g->title_len);
1652 send_packet_group_peer(g_c->fr_c, friendcon_id, PACKET_ID_DIRECT_CONFERENCE, group_num, Packet, sizeof(Packet)); 1652 send_packet_group_peer(g_c->fr_c, friendcon_id, PACKET_ID_DIRECT_CONFERENCE, group_num, Packet, SIZEOF_VLA(Packet));
1653 } 1653 }
1654 1654
1655 return sent; 1655 return sent;
@@ -1902,7 +1902,7 @@ static int send_message_group(const Group_Chats *g_c, int groupnumber, uint8_t m
1902 return -3; 1902 return -3;
1903 } 1903 }
1904 1904
1905 uint8_t packet[sizeof(uint16_t) + sizeof(uint32_t) + 1 + len]; 1905 VLA(uint8_t, packet, sizeof(uint16_t) + sizeof(uint32_t) + 1 + len);
1906 uint16_t peer_num = htons(g->peer_number); 1906 uint16_t peer_num = htons(g->peer_number);
1907 memcpy(packet, &peer_num, sizeof(peer_num)); 1907 memcpy(packet, &peer_num, sizeof(peer_num));
1908 1908
@@ -1921,7 +1921,7 @@ static int send_message_group(const Group_Chats *g_c, int groupnumber, uint8_t m
1921 memcpy(packet + sizeof(uint16_t) + sizeof(uint32_t) + 1, data, len); 1921 memcpy(packet + sizeof(uint16_t) + sizeof(uint32_t) + 1, data, len);
1922 } 1922 }
1923 1923
1924 unsigned int ret = send_message_all_close(g_c, groupnumber, packet, sizeof(packet), -1); 1924 unsigned int ret = send_message_all_close(g_c, groupnumber, packet, SIZEOF_VLA(packet), -1);
1925 1925
1926 return (ret == 0) ? -4 : ret; 1926 return (ret == 0) ? -4 : ret;
1927} 1927}
@@ -1970,14 +1970,14 @@ int send_group_lossy_packet(const Group_Chats *g_c, int groupnumber, const uint8
1970 return -1; 1970 return -1;
1971 } 1971 }
1972 1972
1973 uint8_t packet[sizeof(uint16_t) * 2 + length]; 1973 VLA(uint8_t, packet, sizeof(uint16_t) * 2 + length);
1974 uint16_t peer_number = htons(g->peer_number); 1974 uint16_t peer_number = htons(g->peer_number);
1975 memcpy(packet, &peer_number, sizeof(uint16_t)); 1975 memcpy(packet, &peer_number, sizeof(uint16_t));
1976 uint16_t message_num = htons(g->lossy_message_number); 1976 uint16_t message_num = htons(g->lossy_message_number);
1977 memcpy(packet + sizeof(uint16_t), &message_num, sizeof(uint16_t)); 1977 memcpy(packet + sizeof(uint16_t), &message_num, sizeof(uint16_t));
1978 memcpy(packet + sizeof(uint16_t) * 2, data, length); 1978 memcpy(packet + sizeof(uint16_t) * 2, data, length);
1979 1979
1980 if (send_lossy_all_close(g_c, groupnumber, packet, sizeof(packet), -1) == 0) { 1980 if (send_lossy_all_close(g_c, groupnumber, packet, SIZEOF_VLA(packet), -1) == 0) {
1981 return -1; 1981 return -1;
1982 } 1982 }
1983 1983
@@ -2088,7 +2088,7 @@ static void handle_message_packet_group(Group_Chats *g_c, int groupnumber, const
2088 return; 2088 return;
2089 } 2089 }
2090 2090
2091 uint8_t newmsg[msg_data_len + 1]; 2091 VLA(uint8_t, newmsg, msg_data_len + 1);
2092 memcpy(newmsg, msg_data, msg_data_len); 2092 memcpy(newmsg, msg_data, msg_data_len);
2093 newmsg[msg_data_len] = 0; 2093 newmsg[msg_data_len] = 0;
2094 2094
@@ -2105,7 +2105,7 @@ static void handle_message_packet_group(Group_Chats *g_c, int groupnumber, const
2105 return; 2105 return;
2106 } 2106 }
2107 2107
2108 uint8_t newmsg[msg_data_len + 1]; 2108 VLA(uint8_t, newmsg, msg_data_len + 1);
2109 memcpy(newmsg, msg_data, msg_data_len); 2109 memcpy(newmsg, msg_data, msg_data_len);
2110 newmsg[msg_data_len] = 0; 2110 newmsg[msg_data_len] = 0;
2111 2111
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c
index cc3c5226..ecd0177c 100644
--- a/toxcore/net_crypto.c
+++ b/toxcore/net_crypto.c
@@ -903,12 +903,12 @@ static int send_data_packet(Net_Crypto *c, int crypt_connection_id, const uint8_
903 } 903 }
904 904
905 pthread_mutex_lock(&conn->mutex); 905 pthread_mutex_lock(&conn->mutex);
906 uint8_t packet[1 + sizeof(uint16_t) + length + CRYPTO_MAC_SIZE]; 906 VLA(uint8_t, packet, 1 + sizeof(uint16_t) + length + CRYPTO_MAC_SIZE);
907 packet[0] = NET_PACKET_CRYPTO_DATA; 907 packet[0] = NET_PACKET_CRYPTO_DATA;
908 memcpy(packet + 1, conn->sent_nonce + (CRYPTO_NONCE_SIZE - sizeof(uint16_t)), sizeof(uint16_t)); 908 memcpy(packet + 1, conn->sent_nonce + (CRYPTO_NONCE_SIZE - sizeof(uint16_t)), sizeof(uint16_t));
909 int len = encrypt_data_symmetric(conn->shared_key, conn->sent_nonce, data, length, packet + 1 + sizeof(uint16_t)); 909 int len = encrypt_data_symmetric(conn->shared_key, conn->sent_nonce, data, length, packet + 1 + sizeof(uint16_t));
910 910
911 if (len + 1 + sizeof(uint16_t) != sizeof(packet)) { 911 if (len + 1 + sizeof(uint16_t) != SIZEOF_VLA(packet)) {
912 pthread_mutex_unlock(&conn->mutex); 912 pthread_mutex_unlock(&conn->mutex);
913 return -1; 913 return -1;
914 } 914 }
@@ -916,7 +916,7 @@ static int send_data_packet(Net_Crypto *c, int crypt_connection_id, const uint8_
916 increment_nonce(conn->sent_nonce); 916 increment_nonce(conn->sent_nonce);
917 pthread_mutex_unlock(&conn->mutex); 917 pthread_mutex_unlock(&conn->mutex);
918 918
919 return send_packet_to(c, crypt_connection_id, packet, sizeof(packet)); 919 return send_packet_to(c, crypt_connection_id, packet, SIZEOF_VLA(packet));
920} 920}
921 921
922/* Creates and sends a data packet with buffer_start and num to the peer using the fastest route. 922/* Creates and sends a data packet with buffer_start and num to the peer using the fastest route.
@@ -934,13 +934,13 @@ static int send_data_packet_helper(Net_Crypto *c, int crypt_connection_id, uint3
934 num = htonl(num); 934 num = htonl(num);
935 buffer_start = htonl(buffer_start); 935 buffer_start = htonl(buffer_start);
936 uint16_t padding_length = (MAX_CRYPTO_DATA_SIZE - length) % CRYPTO_MAX_PADDING; 936 uint16_t padding_length = (MAX_CRYPTO_DATA_SIZE - length) % CRYPTO_MAX_PADDING;
937 uint8_t packet[sizeof(uint32_t) + sizeof(uint32_t) + padding_length + length]; 937 VLA(uint8_t, packet, sizeof(uint32_t) + sizeof(uint32_t) + padding_length + length);
938 memcpy(packet, &buffer_start, sizeof(uint32_t)); 938 memcpy(packet, &buffer_start, sizeof(uint32_t));
939 memcpy(packet + sizeof(uint32_t), &num, sizeof(uint32_t)); 939 memcpy(packet + sizeof(uint32_t), &num, sizeof(uint32_t));
940 memset(packet + (sizeof(uint32_t) * 2), PACKET_ID_PADDING, padding_length); 940 memset(packet + (sizeof(uint32_t) * 2), PACKET_ID_PADDING, padding_length);
941 memcpy(packet + (sizeof(uint32_t) * 2) + padding_length, data, length); 941 memcpy(packet + (sizeof(uint32_t) * 2) + padding_length, data, length);
942 942
943 return send_data_packet(c, crypt_connection_id, packet, sizeof(packet)); 943 return send_data_packet(c, crypt_connection_id, packet, SIZEOF_VLA(packet));
944} 944}
945 945
946static int reset_max_speed_reached(Net_Crypto *c, int crypt_connection_id) 946static int reset_max_speed_reached(Net_Crypto *c, int crypt_connection_id)
diff --git a/toxcore/network.h b/toxcore/network.h
index a3b746c9..bcd1abd1 100644
--- a/toxcore/network.h
+++ b/toxcore/network.h
@@ -24,18 +24,13 @@
24#ifndef NETWORK_H 24#ifndef NETWORK_H
25#define NETWORK_H 25#define NETWORK_H
26 26
27#if defined(__GNUC__)
28#define GNU_EXTENSION __extension__
29#else
30#define GNU_EXTENSION
31#endif
32
33#ifdef PLAN9 27#ifdef PLAN9
34#include <u.h> // Plan 9 requires this is imported first 28#include <u.h> // Plan 9 requires this is imported first
35// Comment line here to avoid reordering by source code formatters. 29// Comment line here to avoid reordering by source code formatters.
36#include <libc.h> 30#include <libc.h>
37#endif 31#endif
38 32
33#include "ccompat.h"
39#include "logger.h" 34#include "logger.h"
40 35
41#include <stdint.h> 36#include <stdint.h>
diff --git a/toxcore/onion.c b/toxcore/onion.c
index 1d5cc71f..60fdcce0 100644
--- a/toxcore/onion.c
+++ b/toxcore/onion.c
@@ -185,7 +185,7 @@ int create_onion_packet(uint8_t *packet, uint16_t max_packet_length, const Onion
185 return -1; 185 return -1;
186 } 186 }
187 187
188 uint8_t step1[SIZE_IPPORT + length]; 188 VLA(uint8_t, step1, SIZE_IPPORT + length);
189 189
190 ipport_pack(step1, &dest); 190 ipport_pack(step1, &dest);
191 memcpy(step1 + SIZE_IPPORT, data, length); 191 memcpy(step1 + SIZE_IPPORT, data, length);
@@ -193,21 +193,21 @@ int create_onion_packet(uint8_t *packet, uint16_t max_packet_length, const Onion
193 uint8_t nonce[CRYPTO_NONCE_SIZE]; 193 uint8_t nonce[CRYPTO_NONCE_SIZE];
194 random_nonce(nonce); 194 random_nonce(nonce);
195 195
196 uint8_t step2[SIZE_IPPORT + SEND_BASE + length]; 196 VLA(uint8_t, step2, SIZE_IPPORT + SEND_BASE + length);
197 ipport_pack(step2, &path->ip_port3); 197 ipport_pack(step2, &path->ip_port3);
198 memcpy(step2 + SIZE_IPPORT, path->public_key3, CRYPTO_PUBLIC_KEY_SIZE); 198 memcpy(step2 + SIZE_IPPORT, path->public_key3, CRYPTO_PUBLIC_KEY_SIZE);
199 199
200 int len = encrypt_data_symmetric(path->shared_key3, nonce, step1, sizeof(step1), 200 int len = encrypt_data_symmetric(path->shared_key3, nonce, step1, SIZEOF_VLA(step1),
201 step2 + SIZE_IPPORT + CRYPTO_PUBLIC_KEY_SIZE); 201 step2 + SIZE_IPPORT + CRYPTO_PUBLIC_KEY_SIZE);
202 202
203 if (len != SIZE_IPPORT + length + CRYPTO_MAC_SIZE) { 203 if (len != SIZE_IPPORT + length + CRYPTO_MAC_SIZE) {
204 return -1; 204 return -1;
205 } 205 }
206 206
207 uint8_t step3[SIZE_IPPORT + SEND_BASE * 2 + length]; 207 VLA(uint8_t, step3, SIZE_IPPORT + SEND_BASE * 2 + length);
208 ipport_pack(step3, &path->ip_port2); 208 ipport_pack(step3, &path->ip_port2);
209 memcpy(step3 + SIZE_IPPORT, path->public_key2, CRYPTO_PUBLIC_KEY_SIZE); 209 memcpy(step3 + SIZE_IPPORT, path->public_key2, CRYPTO_PUBLIC_KEY_SIZE);
210 len = encrypt_data_symmetric(path->shared_key2, nonce, step2, sizeof(step2), 210 len = encrypt_data_symmetric(path->shared_key2, nonce, step2, SIZEOF_VLA(step2),
211 step3 + SIZE_IPPORT + CRYPTO_PUBLIC_KEY_SIZE); 211 step3 + SIZE_IPPORT + CRYPTO_PUBLIC_KEY_SIZE);
212 212
213 if (len != SIZE_IPPORT + SEND_BASE + length + CRYPTO_MAC_SIZE) { 213 if (len != SIZE_IPPORT + SEND_BASE + length + CRYPTO_MAC_SIZE) {
@@ -218,7 +218,7 @@ int create_onion_packet(uint8_t *packet, uint16_t max_packet_length, const Onion
218 memcpy(packet + 1, nonce, CRYPTO_NONCE_SIZE); 218 memcpy(packet + 1, nonce, CRYPTO_NONCE_SIZE);
219 memcpy(packet + 1 + CRYPTO_NONCE_SIZE, path->public_key1, CRYPTO_PUBLIC_KEY_SIZE); 219 memcpy(packet + 1 + CRYPTO_NONCE_SIZE, path->public_key1, CRYPTO_PUBLIC_KEY_SIZE);
220 220
221 len = encrypt_data_symmetric(path->shared_key1, nonce, step3, sizeof(step3), 221 len = encrypt_data_symmetric(path->shared_key1, nonce, step3, SIZEOF_VLA(step3),
222 packet + 1 + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE); 222 packet + 1 + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE);
223 223
224 if (len != SIZE_IPPORT + SEND_BASE * 2 + length + CRYPTO_MAC_SIZE) { 224 if (len != SIZE_IPPORT + SEND_BASE * 2 + length + CRYPTO_MAC_SIZE) {
@@ -244,7 +244,7 @@ int create_onion_packet_tcp(uint8_t *packet, uint16_t max_packet_length, const O
244 return -1; 244 return -1;
245 } 245 }
246 246
247 uint8_t step1[SIZE_IPPORT + length]; 247 VLA(uint8_t, step1, SIZE_IPPORT + length);
248 248
249 ipport_pack(step1, &dest); 249 ipport_pack(step1, &dest);
250 memcpy(step1 + SIZE_IPPORT, data, length); 250 memcpy(step1 + SIZE_IPPORT, data, length);
@@ -252,11 +252,11 @@ int create_onion_packet_tcp(uint8_t *packet, uint16_t max_packet_length, const O
252 uint8_t nonce[CRYPTO_NONCE_SIZE]; 252 uint8_t nonce[CRYPTO_NONCE_SIZE];
253 random_nonce(nonce); 253 random_nonce(nonce);
254 254
255 uint8_t step2[SIZE_IPPORT + SEND_BASE + length]; 255 VLA(uint8_t, step2, SIZE_IPPORT + SEND_BASE + length);
256 ipport_pack(step2, &path->ip_port3); 256 ipport_pack(step2, &path->ip_port3);
257 memcpy(step2 + SIZE_IPPORT, path->public_key3, CRYPTO_PUBLIC_KEY_SIZE); 257 memcpy(step2 + SIZE_IPPORT, path->public_key3, CRYPTO_PUBLIC_KEY_SIZE);
258 258
259 int len = encrypt_data_symmetric(path->shared_key3, nonce, step1, sizeof(step1), 259 int len = encrypt_data_symmetric(path->shared_key3, nonce, step1, SIZEOF_VLA(step1),
260 step2 + SIZE_IPPORT + CRYPTO_PUBLIC_KEY_SIZE); 260 step2 + SIZE_IPPORT + CRYPTO_PUBLIC_KEY_SIZE);
261 261
262 if (len != SIZE_IPPORT + length + CRYPTO_MAC_SIZE) { 262 if (len != SIZE_IPPORT + length + CRYPTO_MAC_SIZE) {
@@ -265,7 +265,7 @@ int create_onion_packet_tcp(uint8_t *packet, uint16_t max_packet_length, const O
265 265
266 ipport_pack(packet + CRYPTO_NONCE_SIZE, &path->ip_port2); 266 ipport_pack(packet + CRYPTO_NONCE_SIZE, &path->ip_port2);
267 memcpy(packet + CRYPTO_NONCE_SIZE + SIZE_IPPORT, path->public_key2, CRYPTO_PUBLIC_KEY_SIZE); 267 memcpy(packet + CRYPTO_NONCE_SIZE + SIZE_IPPORT, path->public_key2, CRYPTO_PUBLIC_KEY_SIZE);
268 len = encrypt_data_symmetric(path->shared_key2, nonce, step2, sizeof(step2), 268 len = encrypt_data_symmetric(path->shared_key2, nonce, step2, SIZEOF_VLA(step2),
269 packet + CRYPTO_NONCE_SIZE + SIZE_IPPORT + CRYPTO_PUBLIC_KEY_SIZE); 269 packet + CRYPTO_NONCE_SIZE + SIZE_IPPORT + CRYPTO_PUBLIC_KEY_SIZE);
270 270
271 if (len != SIZE_IPPORT + SEND_BASE + length + CRYPTO_MAC_SIZE) { 271 if (len != SIZE_IPPORT + SEND_BASE + length + CRYPTO_MAC_SIZE) {
@@ -313,12 +313,12 @@ int send_onion_response(Networking_Core *net, IP_Port dest, const uint8_t *data,
313 return -1; 313 return -1;
314 } 314 }
315 315
316 uint8_t packet[1 + RETURN_3 + length]; 316 VLA(uint8_t, packet, 1 + RETURN_3 + length);
317 packet[0] = NET_PACKET_ONION_RECV_3; 317 packet[0] = NET_PACKET_ONION_RECV_3;
318 memcpy(packet + 1, ret, RETURN_3); 318 memcpy(packet + 1, ret, RETURN_3);
319 memcpy(packet + 1 + RETURN_3, data, length); 319 memcpy(packet + 1 + RETURN_3, data, length);
320 320
321 if ((uint32_t)sendpacket(net, dest, packet, sizeof(packet)) != sizeof(packet)) { 321 if ((uint32_t)sendpacket(net, dest, packet, SIZEOF_VLA(packet)) != SIZEOF_VLA(packet)) {
322 return -1; 322 return -1;
323 } 323 }
324 324
diff --git a/toxcore/onion_announce.c b/toxcore/onion_announce.c
index aa287d12..04450c57 100644
--- a/toxcore/onion_announce.c
+++ b/toxcore/onion_announce.c
@@ -426,11 +426,11 @@ static int handle_data_request(void *object, IP_Port source, const uint8_t *pack
426 return 1; 426 return 1;
427 } 427 }
428 428
429 uint8_t data[length - (CRYPTO_PUBLIC_KEY_SIZE + ONION_RETURN_3)]; 429 VLA(uint8_t, data, length - (CRYPTO_PUBLIC_KEY_SIZE + ONION_RETURN_3));
430 data[0] = NET_PACKET_ONION_DATA_RESPONSE; 430 data[0] = NET_PACKET_ONION_DATA_RESPONSE;
431 memcpy(data + 1, packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, length - (1 + CRYPTO_PUBLIC_KEY_SIZE + ONION_RETURN_3)); 431 memcpy(data + 1, packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, length - (1 + CRYPTO_PUBLIC_KEY_SIZE + ONION_RETURN_3));
432 432
433 if (send_onion_response(onion_a->net, onion_a->entries[index].ret_ip_port, data, sizeof(data), 433 if (send_onion_response(onion_a->net, onion_a->entries[index].ret_ip_port, data, SIZEOF_VLA(data),
434 onion_a->entries[index].ret) == -1) { 434 onion_a->entries[index].ret) == -1) {
435 return 1; 435 return 1;
436 } 436 }
diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c
index d324dbf2..a20d3d70 100644
--- a/toxcore/onion_client.c
+++ b/toxcore/onion_client.c
@@ -681,7 +681,7 @@ static int handle_announce_response(void *object, IP_Port source, const uint8_t
681 return 1; 681 return 1;
682 } 682 }
683 683
684 uint8_t plain[1 + ONION_PING_ID_SIZE + len_nodes]; 684 VLA(uint8_t, plain, 1 + ONION_PING_ID_SIZE + len_nodes);
685 int len = -1; 685 int len = -1;
686 686
687 if (num == 0) { 687 if (num == 0) {
@@ -699,7 +699,7 @@ static int handle_announce_response(void *object, IP_Port source, const uint8_t
699 length - (1 + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH + CRYPTO_NONCE_SIZE), plain); 699 length - (1 + ONION_ANNOUNCE_SENDBACK_DATA_LENGTH + CRYPTO_NONCE_SIZE), plain);
700 } 700 }
701 701
702 if ((uint32_t)len != sizeof(plain)) { 702 if ((uint32_t)len != SIZEOF_VLA(plain)) {
703 return 1; 703 return 1;
704 } 704 }
705 705
@@ -739,20 +739,20 @@ static int handle_data_response(void *object, IP_Port source, const uint8_t *pac
739 return 1; 739 return 1;
740 } 740 }
741 741
742 uint8_t temp_plain[length - ONION_DATA_RESPONSE_MIN_SIZE]; 742 VLA(uint8_t, temp_plain, length - ONION_DATA_RESPONSE_MIN_SIZE);
743 int len = decrypt_data(packet + 1 + CRYPTO_NONCE_SIZE, onion_c->temp_secret_key, packet + 1, 743 int len = decrypt_data(packet + 1 + CRYPTO_NONCE_SIZE, onion_c->temp_secret_key, packet + 1,
744 packet + 1 + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE, 744 packet + 1 + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE,
745 length - (1 + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE), temp_plain); 745 length - (1 + CRYPTO_NONCE_SIZE + CRYPTO_PUBLIC_KEY_SIZE), temp_plain);
746 746
747 if ((uint32_t)len != sizeof(temp_plain)) { 747 if ((uint32_t)len != SIZEOF_VLA(temp_plain)) {
748 return 1; 748 return 1;
749 } 749 }
750 750
751 uint8_t plain[sizeof(temp_plain) - DATA_IN_RESPONSE_MIN_SIZE]; 751 VLA(uint8_t, plain, SIZEOF_VLA(temp_plain) - DATA_IN_RESPONSE_MIN_SIZE);
752 len = decrypt_data(temp_plain, onion_c->c->self_secret_key, packet + 1, temp_plain + CRYPTO_PUBLIC_KEY_SIZE, 752 len = decrypt_data(temp_plain, onion_c->c->self_secret_key, packet + 1, temp_plain + CRYPTO_PUBLIC_KEY_SIZE,
753 sizeof(temp_plain) - CRYPTO_PUBLIC_KEY_SIZE, plain); 753 SIZEOF_VLA(temp_plain) - CRYPTO_PUBLIC_KEY_SIZE, plain);
754 754
755 if ((uint32_t)len != sizeof(plain)) { 755 if ((uint32_t)len != SIZEOF_VLA(plain)) {
756 return 1; 756 return 1;
757 } 757 }
758 758
@@ -761,7 +761,7 @@ static int handle_data_response(void *object, IP_Port source, const uint8_t *pac
761 } 761 }
762 762
763 return onion_c->Onion_Data_Handlers[plain[0]].function(onion_c->Onion_Data_Handlers[plain[0]].object, temp_plain, plain, 763 return onion_c->Onion_Data_Handlers[plain[0]].function(onion_c->Onion_Data_Handlers[plain[0]].object, temp_plain, plain,
764 sizeof(plain), userdata); 764 SIZEOF_VLA(plain), userdata);
765} 765}
766 766
767#define DHTPK_DATA_MIN_LENGTH (1 + sizeof(uint64_t) + CRYPTO_PUBLIC_KEY_SIZE) 767#define DHTPK_DATA_MIN_LENGTH (1 + sizeof(uint64_t) + CRYPTO_PUBLIC_KEY_SIZE)
@@ -899,12 +899,12 @@ int send_onion_data(Onion_Client *onion_c, int friend_num, const uint8_t *data,
899 uint8_t nonce[CRYPTO_NONCE_SIZE]; 899 uint8_t nonce[CRYPTO_NONCE_SIZE];
900 random_nonce(nonce); 900 random_nonce(nonce);
901 901
902 uint8_t packet[DATA_IN_RESPONSE_MIN_SIZE + length]; 902 VLA(uint8_t, packet, DATA_IN_RESPONSE_MIN_SIZE + length);
903 memcpy(packet, onion_c->c->self_public_key, CRYPTO_PUBLIC_KEY_SIZE); 903 memcpy(packet, onion_c->c->self_public_key, CRYPTO_PUBLIC_KEY_SIZE);
904 int len = encrypt_data(onion_c->friends_list[friend_num].real_public_key, onion_c->c->self_secret_key, nonce, data, 904 int len = encrypt_data(onion_c->friends_list[friend_num].real_public_key, onion_c->c->self_secret_key, nonce, data,
905 length, packet + CRYPTO_PUBLIC_KEY_SIZE); 905 length, packet + CRYPTO_PUBLIC_KEY_SIZE);
906 906
907 if ((uint32_t)len + CRYPTO_PUBLIC_KEY_SIZE != sizeof(packet)) { 907 if ((uint32_t)len + CRYPTO_PUBLIC_KEY_SIZE != SIZEOF_VLA(packet)) {
908 return -1; 908 return -1;
909 } 909 }
910 910
@@ -919,7 +919,7 @@ int send_onion_data(Onion_Client *onion_c, int friend_num, const uint8_t *data,
919 919
920 uint8_t o_packet[ONION_MAX_PACKET_SIZE]; 920 uint8_t o_packet[ONION_MAX_PACKET_SIZE];
921 len = create_data_request(o_packet, sizeof(o_packet), onion_c->friends_list[friend_num].real_public_key, 921 len = create_data_request(o_packet, sizeof(o_packet), onion_c->friends_list[friend_num].real_public_key,
922 list_nodes[good_nodes[i]].data_public_key, nonce, packet, sizeof(packet)); 922 list_nodes[good_nodes[i]].data_public_key, nonce, packet, SIZEOF_VLA(packet));
923 923
924 if (len == -1) { 924 if (len == -1) {
925 continue; 925 continue;
@@ -953,19 +953,19 @@ static int send_dht_dhtpk(const Onion_Client *onion_c, int friend_num, const uin
953 uint8_t nonce[CRYPTO_NONCE_SIZE]; 953 uint8_t nonce[CRYPTO_NONCE_SIZE];
954 random_nonce(nonce); 954 random_nonce(nonce);
955 955
956 uint8_t temp[DATA_IN_RESPONSE_MIN_SIZE + CRYPTO_NONCE_SIZE + length]; 956 VLA(uint8_t, temp, DATA_IN_RESPONSE_MIN_SIZE + CRYPTO_NONCE_SIZE + length);
957 memcpy(temp, onion_c->c->self_public_key, CRYPTO_PUBLIC_KEY_SIZE); 957 memcpy(temp, onion_c->c->self_public_key, CRYPTO_PUBLIC_KEY_SIZE);
958 memcpy(temp + CRYPTO_PUBLIC_KEY_SIZE, nonce, CRYPTO_NONCE_SIZE); 958 memcpy(temp + CRYPTO_PUBLIC_KEY_SIZE, nonce, CRYPTO_NONCE_SIZE);
959 int len = encrypt_data(onion_c->friends_list[friend_num].real_public_key, onion_c->c->self_secret_key, nonce, data, 959 int len = encrypt_data(onion_c->friends_list[friend_num].real_public_key, onion_c->c->self_secret_key, nonce, data,
960 length, temp + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE); 960 length, temp + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE);
961 961
962 if ((uint32_t)len + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE != sizeof(temp)) { 962 if ((uint32_t)len + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE != SIZEOF_VLA(temp)) {
963 return -1; 963 return -1;
964 } 964 }
965 965
966 uint8_t packet[MAX_CRYPTO_REQUEST_SIZE]; 966 uint8_t packet[MAX_CRYPTO_REQUEST_SIZE];
967 len = create_request(onion_c->dht->self_public_key, onion_c->dht->self_secret_key, packet, 967 len = create_request(onion_c->dht->self_public_key, onion_c->dht->self_secret_key, packet,
968 onion_c->friends_list[friend_num].dht_public_key, temp, sizeof(temp), CRYPTO_PACKET_DHTPK); 968 onion_c->friends_list[friend_num].dht_public_key, temp, SIZEOF_VLA(temp), CRYPTO_PACKET_DHTPK);
969 969
970 if (len == -1) { 970 if (len == -1) {
971 return -1; 971 return -1;
diff --git a/toxcore/util.c b/toxcore/util.c
index 22fa0fc9..92bbb68c 100644
--- a/toxcore/util.c
+++ b/toxcore/util.c
@@ -79,7 +79,7 @@ void host_to_net(uint8_t *num, uint16_t numbytes)
79{ 79{
80#ifndef WORDS_BIGENDIAN 80#ifndef WORDS_BIGENDIAN
81 uint32_t i; 81 uint32_t i;
82 uint8_t buff[numbytes]; 82 VLA(uint8_t, buff, numbytes);
83 83
84 for (i = 0; i < numbytes; ++i) { 84 for (i = 0; i < numbytes; ++i) {
85 buff[i] = num[numbytes - i - 1]; 85 buff[i] = num[numbytes - i - 1];
diff --git a/toxdns/toxdns.c b/toxdns/toxdns.c
index 3fb43452..96f3081f 100644
--- a/toxdns/toxdns.c
+++ b/toxdns/toxdns.c
@@ -220,10 +220,10 @@ int tox_decrypt_dns3_TXT(void *dns3_object, uint8_t *tox_id, uint8_t *id_record,
220 220
221#endif 221#endif
222 222
223 uint8_t id_record_null[id_record_len + 1]; 223 VLA(uint8_t, id_record_null, id_record_len + 1);
224 memcpy(id_record_null, id_record, id_record_len); 224 memcpy(id_record_null, id_record, id_record_len);
225 id_record_null[id_record_len] = 0; 225 id_record_null[id_record_len] = 0;
226 uint8_t data[id_record_len]; 226 VLA(uint8_t, data, id_record_len);
227 int length = decode(data, id_record_null); 227 int length = decode(data, id_record_null);
228 228
229 if (length == -1) { 229 if (length == -1) {