diff options
author | iphydf <iphydf@users.noreply.github.com> | 2017-01-20 21:16:55 +0000 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2017-01-28 20:49:12 +0000 |
commit | 6ae33c16cf9e37fda85d70c78b3c2779eb8ca21a (patch) | |
tree | 99c3a8c26e02039b515bb6f57d2797d1cdf77c1d /toxcore | |
parent | 895de7ef26e7617769f2271345e414545c2581f8 (diff) |
Add VLA compatibility macro for C89-ish compilers.
Diffstat (limited to 'toxcore')
-rw-r--r-- | toxcore/DHT.c | 22 | ||||
-rw-r--r-- | toxcore/Makefile.inc | 3 | ||||
-rw-r--r-- | toxcore/Messenger.c | 28 | ||||
-rw-r--r-- | toxcore/TCP_client.c | 32 | ||||
-rw-r--r-- | toxcore/TCP_connection.c | 5 | ||||
-rw-r--r-- | toxcore/TCP_server.c | 30 | ||||
-rw-r--r-- | toxcore/ccompat.h | 43 | ||||
-rw-r--r-- | toxcore/crypto_core.c | 8 | ||||
-rw-r--r-- | toxcore/friend_connection.c | 6 | ||||
-rw-r--r-- | toxcore/friend_requests.c | 4 | ||||
-rw-r--r-- | toxcore/group.c | 24 | ||||
-rw-r--r-- | toxcore/net_crypto.c | 10 | ||||
-rw-r--r-- | toxcore/network.h | 7 | ||||
-rw-r--r-- | toxcore/onion.c | 24 | ||||
-rw-r--r-- | toxcore/onion_announce.c | 4 | ||||
-rw-r--r-- | toxcore/onion_client.c | 28 | ||||
-rw-r--r-- | toxcore/util.c | 2 |
17 files changed, 161 insertions, 119 deletions
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) | |||
361 | static int DHT_create_packet(const uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE], | 361 | static 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 | ||
6 | libtoxcore_la_includedir = $(includedir)/tox | 6 | libtoxcore_la_includedir = $(includedir)/tox |
7 | 7 | ||
8 | libtoxcore_la_SOURCES = ../toxcore/DHT.h \ | 8 | libtoxcore_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 | */ |
615 | int send_onion_request(TCP_Client_Connection *con, const uint8_t *data, uint16_t length) | 615 | int 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 | ||
623 | void onion_response_handler(TCP_Client_Connection *con, int (*onion_callback)(void *object, const uint8_t *data, | 623 | void 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 | ||
946 | static int reset_max_speed_reached(Net_Crypto *c, int crypt_connection_id) | 946 | static 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]; |