Skip to content

Commit

Permalink
Add VLA compatibility macro for C89-ish compilers.
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Jan 21, 2017
1 parent f185834 commit 2dc9681
Show file tree
Hide file tree
Showing 30 changed files with 214 additions and 162 deletions.
6 changes: 3 additions & 3 deletions auto_tests/TCP_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,19 +188,19 @@ static void kill_TCP_con(struct sec_TCP_con *con)

static int write_packet_TCP_secure_connection(struct sec_TCP_con *con, uint8_t *data, uint16_t length)
{
uint8_t packet[sizeof(uint16_t) + length + CRYPTO_MAC_SIZE];
VLA(uint8_t, packet, sizeof(uint16_t) + length + CRYPTO_MAC_SIZE);

uint16_t c_length = htons(length + CRYPTO_MAC_SIZE);
memcpy(packet, &c_length, sizeof(uint16_t));
int len = encrypt_data_symmetric(con->shared_key, con->sent_nonce, data, length, packet + sizeof(uint16_t));

if ((unsigned int)len != (sizeof(packet) - sizeof(uint16_t))) {
if ((unsigned int)len != (SIZEOF_VLA(packet) - sizeof(uint16_t))) {
return -1;
}

increment_nonce(con->sent_nonce);

ck_assert_msg(send(con->sock, (const char *)packet, sizeof(packet), 0) == sizeof(packet), "send failed");
ck_assert_msg(send(con->sock, (const char *)packet, SIZEOF_VLA(packet), 0) == SIZEOF_VLA(packet), "send failed");
return 0;
}

Expand Down
14 changes: 8 additions & 6 deletions auto_tests/encryptsave_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include "../toxcore/tox.h"

#include "../toxcore/ccompat.h"
#include "../toxcore/crypto_core.h"
#include "../toxencryptsave/toxencryptsave.h"
#ifdef VANILLA_NACL
Expand Down Expand Up @@ -68,10 +69,10 @@ START_TEST(test_save_friend)
ck_assert_msg(test != UINT32_MAX, "Failed to add friend");

size_t size = tox_get_savedata_size(tox1);
uint8_t data[size];
VLA(uint8_t, data, size);
tox_get_savedata(tox1, data);
size_t size2 = size + TOX_PASS_ENCRYPTION_EXTRA_LENGTH;
uint8_t enc_data[size2];
VLA(uint8_t, enc_data, size2);
TOX_ERR_ENCRYPTION error1;
bool ret = tox_pass_encrypt(data, size, (const uint8_t *)"correcthorsebatterystaple", 25, enc_data, &error1);
ck_assert_msg(ret, "failed to encrypted save: %u", error1);
Expand All @@ -86,7 +87,7 @@ START_TEST(test_save_friend)
ck_assert_msg(err2 == TOX_ERR_NEW_LOAD_ENCRYPTED, "wrong error! %u. should fail with %u", err2,
TOX_ERR_NEW_LOAD_ENCRYPTED);
ck_assert_msg(tox3 == NULL, "tox_new with error should return NULL");
uint8_t dec_data[size];
VLA(uint8_t, dec_data, size);
TOX_ERR_DECRYPTION err3;
ret = tox_pass_decrypt(enc_data, size2, (const uint8_t *)"correcthorsebatterystaple", 25, dec_data, &err3);
ck_assert_msg(ret, "failed to decrypt save: %u", err3);
Expand All @@ -99,19 +100,20 @@ START_TEST(test_save_friend)
ck_assert_msg(memcmp(address, address2, TOX_PUBLIC_KEY_SIZE) == 0, "addresses don't match!");

size = tox_get_savedata_size(tox3);
uint8_t data2[size];
VLA(uint8_t, data2, size);
tox_get_savedata(tox3, data2);
Tox_Pass_Key *key = tox_pass_key_new();
ck_assert_msg(key != NULL, "pass key allocation failure");
memcpy((uint8_t *)key, test_salt, TOX_PASS_SALT_LENGTH);
memcpy((uint8_t *)key + TOX_PASS_SALT_LENGTH, known_key2, TOX_PASS_KEY_LENGTH);
size2 = size + TOX_PASS_ENCRYPTION_EXTRA_LENGTH;
uint8_t encdata2[size2];
VLA(uint8_t, encdata2, size2);
ret = tox_pass_key_encrypt(key, data2, size, encdata2, &error1);
ck_assert_msg(ret, "failed to key encrypt %u", error1);
ck_assert_msg(tox_is_data_encrypted(encdata2), "magic number the second missing");

uint8_t out1[size], out2[size];
VLA(uint8_t, out1, size);
VLA(uint8_t, out2, size);
ret = tox_pass_decrypt(encdata2, size2, (const uint8_t *)pw, pwlen, out1, &err3);
ck_assert_msg(ret, "failed to pw decrypt %u", err3);
ret = tox_pass_key_decrypt(key, encdata2, size2, out2, &err3);
Expand Down
10 changes: 5 additions & 5 deletions auto_tests/messenger_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ START_TEST(test_getself_name)
{
const char *nickname = "testGallop";
int len = strlen(nickname);
char nick_check[len];
VLA(char, nick_check, len);

setname(m, (const uint8_t *)nickname, len);
getself_name(m, (uint8_t *)nick_check);
Expand Down Expand Up @@ -237,7 +237,7 @@ START_TEST(test_dht_state_saveloadsave)
* d) the second save() is of equal content */
size_t i, extra = 64;
size_t size = DHT_size(m->dht);
uint8_t buffer[size + 2 * extra];
VLA(uint8_t, buffer, size + 2 * extra);
memset(buffer, 0xCD, extra);
memset(buffer + extra + size, 0xCD, extra);
DHT_save(m->dht, buffer + extra);
Expand All @@ -263,7 +263,7 @@ START_TEST(test_dht_state_saveloadsave)
size_t size2 = DHT_size(m->dht);
ck_assert_msg(size == size2, "Messenger \"grew\" in size from a store/load cycle: %u -> %u", size, size2);

uint8_t buffer2[size2];
VLA(uint8_t, buffer2, size2);
DHT_save(m->dht, buffer2);

ck_assert_msg(!memcmp(buffer + extra, buffer2, size), "DHT state changed by store/load/store cycle");
Expand All @@ -279,7 +279,7 @@ START_TEST(test_messenger_state_saveloadsave)
* d) the second save() is of equal content */
size_t i, extra = 64;
size_t size = messenger_size(m);
uint8_t buffer[size + 2 * extra];
VLA(uint8_t, buffer, size + 2 * extra);
memset(buffer, 0xCD, extra);
memset(buffer + extra + size, 0xCD, extra);
messenger_save(m, buffer + extra);
Expand All @@ -305,7 +305,7 @@ START_TEST(test_messenger_state_saveloadsave)
size_t size2 = messenger_size(m);
ck_assert_msg(size == size2, "Messenger \"grew\" in size from a store/load cycle: %u -> %u", size, size2);

uint8_t buffer2[size2];
VLA(uint8_t, buffer2, size2);
messenger_save(m, buffer2);

ck_assert_msg(!memcmp(buffer + extra, buffer2, size), "Messenger state changed by store/load/store cycle");
Expand Down
7 changes: 4 additions & 3 deletions auto_tests/save_friend_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#define _XOPEN_SOURCE 600

#include "helpers.h"
#include "../toxcore/ccompat.h"
#include "../toxcore/tox.h"

#include <assert.h>
Expand All @@ -28,14 +29,14 @@ struct test_data {

static void set_random(Tox *m, bool (*setter)(Tox *, const uint8_t *, size_t, TOX_ERR_SET_INFO *), size_t length)
{
uint8_t text[length];
VLA(uint8_t, text, length);
uint32_t i;

for (i = 0; i < length; ++i) {
text[i] = rand();
}

setter(m, text, sizeof(text), 0);
setter(m, text, SIZEOF_VLA(text), 0);
}

void namechange_callback(Tox *tox, uint32_t friend_number, const uint8_t *name, size_t length, void *user_data)
Expand Down Expand Up @@ -106,7 +107,7 @@ int main(int argc, char *argv[])
}

size_t save_size = tox_get_savedata_size(tox1);
uint8_t savedata[save_size];
VLA(uint8_t, savedata, save_size);
tox_get_savedata(tox1, savedata);

struct Tox_Options *options = tox_options_new(NULL);
Expand Down
3 changes: 2 additions & 1 deletion auto_tests/tox_one_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <stdlib.h>
#include <time.h>

#include "../toxcore/ccompat.h"
#include "../toxcore/tox.h"
#include "../toxcore/util.h"

Expand Down Expand Up @@ -77,7 +78,7 @@ START_TEST(test_one)

tox_self_get_address(tox1, address);
size_t save_size = tox_get_savedata_size(tox1);
uint8_t data[save_size];
VLA(uint8_t, data, save_size);
tox_get_savedata(tox1, data);

tox_kill(tox2);
Expand Down
9 changes: 5 additions & 4 deletions auto_tests/tox_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <stdlib.h>
#include <time.h>

#include "../toxcore/ccompat.h"
#include "../toxcore/tox.h"
#include "../toxcore/util.h"

Expand Down Expand Up @@ -125,7 +126,7 @@ static void handle_custom_packet(Tox *m, uint32_t friend_num, const uint8_t *dat
return;
}

uint8_t f_data[len];
VLA(uint8_t, f_data, len);
memset(f_data, number, len);

if (memcmp(f_data, data, len) == 0) {
Expand Down Expand Up @@ -260,7 +261,7 @@ static void tox_file_chunk_request(Tox *tox, uint32_t friend_number, uint32_t fi
}

TOX_ERR_FILE_SEND_CHUNK error;
uint8_t f_data[length];
VLA(uint8_t, f_data, length);
memset(f_data, sending_num, length);

if (tox_file_send_chunk(tox, friend_number, file_number, position, f_data, length, &error)) {
Expand Down Expand Up @@ -294,7 +295,7 @@ static void write_file(Tox *tox, uint32_t friendnumber, uint32_t filenumber, uin
return;
}

uint8_t f_data[length];
VLA(uint8_t, f_data, length);
memset(f_data, num, length);
++num;

Expand Down Expand Up @@ -416,7 +417,7 @@ START_TEST(test_few_clients)
unsigned int save_size1 = tox_get_savedata_size(tox2);
ck_assert_msg(save_size1 != 0 && save_size1 < 4096, "save is invalid size %u", save_size1);
printf("%u\n", save_size1);
uint8_t save1[save_size1];
VLA(uint8_t, save1, save_size1);
tox_get_savedata(tox2, save1);
tox_kill(tox2);

Expand Down
4 changes: 3 additions & 1 deletion other/bootstrap_daemon/src/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

#include "global.h"

#include "../../../toxcore/ccompat.h"

#include <assert.h>
#include <syslog.h>
#include <stdarg.h>
Expand Down Expand Up @@ -94,7 +96,7 @@ static void log_syslog(LOG_LEVEL level, const char *format, va_list args)
return;
}

char buf[size + 1];
VLA(char, buf, size + 1);
vsnprintf(buf, size + 1, format, args);

syslog(level_syslog(level), "%s", buf);
Expand Down
7 changes: 4 additions & 3 deletions testing/irc_syncbot.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ static int reconnect(void)
return new_sock;
}

#include "../toxcore/ccompat.h"
#include "../toxcore/tox.h"
#include "misc_tools.c"

Expand Down Expand Up @@ -178,7 +179,7 @@ static void send_irc_group(Tox *tox, uint8_t *msg, uint16_t len)
return;
}

uint8_t req[len];
VLA(uint8_t, req, len);
unsigned int i;

unsigned int spaces = 0;
Expand All @@ -198,7 +199,7 @@ static void send_irc_group(Tox *tox, uint8_t *msg, uint16_t len)
unsigned int req_len = i;
req[i] = 0;

uint8_t message[len];
VLA(uint8_t, message, len);
uint16_t length = 0;

uint8_t *pmsg = (uint8_t *)strstr((char *)req, " PRIVMSG");
Expand Down Expand Up @@ -298,7 +299,7 @@ int main(int argc, char *argv[])
if (count > 0) {
last_get = get_monotime_sec();
ping_sent = 0;
uint8_t data[count + 1];
VLA(uint8_t, data, count + 1);
data[count] = 0;
recv(sock, data, count, MSG_NOSIGNAL);
printf("%s", data);
Expand Down
24 changes: 13 additions & 11 deletions testing/nTox.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

#include <sys/select.h>

#include "../toxcore/ccompat.h"
#include "misc_tools.c"
#include "nTox.h"

Expand Down Expand Up @@ -144,7 +145,7 @@ static void tox_file_chunk_request(Tox *tox, uint32_t friend_number, uint32_t fi
}

fseek(file_senders[i].file, position, SEEK_SET);
uint8_t data[length];
VLA(uint8_t, data, length);
int len = fread(data, 1, length, file_senders[i].file);
tox_file_send_chunk(tox, friend_number, file_number, position, data, len, 0);
break;
Expand Down Expand Up @@ -266,7 +267,7 @@ static void print_friendlist(Tox *m)
char fraddr_str[FRADDR_TOSTR_BUFSIZE];

/* account for the longest name and the longest "base" string and number (int) and id_str */
char fstring[TOX_MAX_NAME_LENGTH + strlen(ptrn_friend) + 21 + id_str_len];
VLA(char, fstring, TOX_MAX_NAME_LENGTH + strlen(ptrn_friend) + 21 + id_str_len);

uint32_t i = 0;

Expand Down Expand Up @@ -299,7 +300,7 @@ static void print_formatted_message(Tox *m, char *message, int friendnum, uint8_
char name[TOX_MAX_NAME_LENGTH + 1];
getfriendname_terminated(m, friendnum, name);

char msg[100 + strlen(message) + strlen(name) + 1];
VLA(char, msg, 100 + strlen(message) + strlen(name) + 1);

time_t rawtime;
struct tm *timeinfo;
Expand Down Expand Up @@ -920,7 +921,7 @@ static void print_message(Tox *m, uint32_t friendnumber, TOX_MESSAGE_TYPE type,
void *userdata)
{
/* ensure null termination */
uint8_t null_string[length + 1];
VLA(uint8_t, null_string, length + 1);
memcpy(null_string, string, length);
null_string[length] = 0;
print_formatted_message(m, (char *)null_string, friendnumber, 0);
Expand All @@ -931,7 +932,7 @@ static void print_nickchange(Tox *m, uint32_t friendnumber, const uint8_t *strin
char name[TOX_MAX_NAME_LENGTH + 1];

if (getfriendname_terminated(m, friendnumber, name) != -1) {
char msg[100 + length];
VLA(char, msg, 100 + length);

if (name[0] != 0) {
sprintf(msg, "[i] [%d] %s is now known as %s.", friendnumber, name, string);
Expand All @@ -948,7 +949,7 @@ static void print_statuschange(Tox *m, uint32_t friendnumber, const uint8_t *str
char name[TOX_MAX_NAME_LENGTH + 1];

if (getfriendname_terminated(m, friendnumber, name) != -1) {
char msg[100 + length + strlen(name) + 1];
VLA(char, msg, 100 + length + strlen(name) + 1);

if (name[0] != 0) {
sprintf(msg, "[i] [%d] %s's status changed to %s.", friendnumber, name, string);
Expand All @@ -971,7 +972,7 @@ static Tox *load_data(void)
size_t size = ftell(data_file);
rewind(data_file);

uint8_t data[size];
VLA(uint8_t, data, size);

if (fread(data, sizeof(uint8_t), size, data_file) != size) {
fputs("[!] could not read data file!\n", stderr);
Expand Down Expand Up @@ -1014,7 +1015,7 @@ static int save_data(Tox *m)

int res = 1;
size_t size = tox_get_savedata_size(m);
uint8_t data[size];
VLA(uint8_t, data, size);
tox_get_savedata(m, data);

if (fwrite(data, sizeof(uint8_t), size, data_file) != size) {
Expand Down Expand Up @@ -1080,8 +1081,9 @@ static void print_groupchatpeers(Tox *m, int groupnumber)
return;
}

uint8_t names[num][TOX_MAX_NAME_LENGTH];
size_t lengths[num];
typedef uint8_t Peer_Name[TOX_MAX_NAME_LENGTH];
VLA(Peer_Name, names, num);
VLA(size_t, lengths, num);

uint32_t i;

Expand Down Expand Up @@ -1126,7 +1128,7 @@ static void print_groupmessage(Tox *m, uint32_t groupnumber, uint32_t peernumber
const uint8_t *message, size_t length,
void *userdata)
{
char msg[256 + length];
VLA(char, msg, 256 + length);

TOX_ERR_CONFERENCE_PEER_QUERY error;
size_t len = tox_conference_peer_get_name_size(m, groupnumber, peernumber, &error);
Expand Down
5 changes: 3 additions & 2 deletions testing/tox_sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "config.h"
#endif

#include "../toxcore/ccompat.h"
#include "../toxcore/tox.h"
#include "misc_tools.c"

Expand Down Expand Up @@ -73,7 +74,7 @@ static void tox_file_chunk_request(Tox *tox, uint32_t friend_number, uint32_t fi
}

fseek(file_senders[i].file, position, SEEK_SET);
uint8_t data[length];
VLA(uint8_t, data, length);
int len = fread(data, 1, length, file_senders[i].file);
tox_file_send_chunk(tox, friend_number, file_number, position, data, len, 0);
break;
Expand Down Expand Up @@ -314,7 +315,7 @@ int main(int argc, char *argv[])

if (d) {
while ((dir = readdir(d)) != NULL) {
char filepath[strlen(path) + strlen(dir->d_name) + 1];
VLA(char, filepath, strlen(path) + strlen(dir->d_name) + 1);
memcpy(filepath, path, strlen(path));
memcpy(filepath + strlen(path), dir->d_name, strlen(dir->d_name) + 1);
stat(filepath, &statbuf);
Expand Down
Loading

0 comments on commit 2dc9681

Please sign in to comment.