From 6ae33c16cf9e37fda85d70c78b3c2779eb8ca21a Mon Sep 17 00:00:00 2001 From: iphydf Date: Fri, 20 Jan 2017 21:16:55 +0000 Subject: Add VLA compatibility macro for C89-ish compilers. --- testing/irc_syncbot.c | 7 ++++--- testing/nTox.c | 24 +++++++++++++----------- testing/tox_sync.c | 5 +++-- 3 files changed, 20 insertions(+), 16 deletions(-) (limited to 'testing') 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) return new_sock; } +#include "../toxcore/ccompat.h" #include "../toxcore/tox.h" #include "misc_tools.c" @@ -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; @@ -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"); @@ -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); 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 @@ #include +#include "../toxcore/ccompat.h" #include "misc_tools.c" #include "nTox.h" @@ -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; @@ -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; @@ -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; @@ -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); @@ -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); @@ -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); @@ -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); @@ -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) { @@ -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; @@ -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); 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 @@ #include "config.h" #endif +#include "../toxcore/ccompat.h" #include "../toxcore/tox.h" #include "misc_tools.c" @@ -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; @@ -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); -- cgit v1.2.3