Skip to content

Commit 12fab0c

Browse files
iphydfzoff99
authored andcommitted
cleanup: Remove all uses of SIZEOF_VLA.
This is step 1 towards removing VLAs altogether.
1 parent 9142c35 commit 12fab0c

File tree

20 files changed

+138
-106
lines changed

20 files changed

+138
-106
lines changed

auto_tests/TCP_test.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,13 +266,14 @@ static void kill_tcp_con(struct sec_TCP_con *con)
266266
static int write_packet_tcp_test_connection(const Logger *logger, struct sec_TCP_con *con, const uint8_t *data,
267267
uint16_t length)
268268
{
269-
VLA(uint8_t, packet, sizeof(uint16_t) + length + CRYPTO_MAC_SIZE);
269+
const uint16_t packet_size = sizeof(uint16_t) + length + CRYPTO_MAC_SIZE;
270+
VLA(uint8_t, packet, packet_size);
270271

271272
uint16_t c_length = net_htons(length + CRYPTO_MAC_SIZE);
272273
memcpy(packet, &c_length, sizeof(uint16_t));
273274
int len = encrypt_data_symmetric(con->shared_key, con->sent_nonce, data, length, packet + sizeof(uint16_t));
274275

275-
if ((unsigned int)len != (SIZEOF_VLA(packet) - sizeof(uint16_t))) {
276+
if ((unsigned int)len != (packet_size - sizeof(uint16_t))) {
276277
return -1;
277278
}
278279

@@ -282,7 +283,7 @@ static int write_packet_tcp_test_connection(const Logger *logger, struct sec_TCP
282283
localhost.ip = get_loopback();
283284
localhost.port = 0;
284285

285-
ck_assert_msg(net_send(con->ns, logger, con->sock, packet, SIZEOF_VLA(packet), &localhost) == SIZEOF_VLA(packet),
286+
ck_assert_msg(net_send(con->ns, logger, con->sock, packet, packet_size, &localhost) == packet_size,
286287
"Failed to send a packet.");
287288
return 0;
288289
}

auto_tests/save_friend_test.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,12 @@ struct test_data {
2222
static void set_random(Tox *m, const Random *rng, bool (*setter)(Tox *, const uint8_t *, size_t, Tox_Err_Set_Info *), size_t length)
2323
{
2424
VLA(uint8_t, text, length);
25-
uint32_t i;
2625

27-
for (i = 0; i < length; ++i) {
26+
for (uint32_t i = 0; i < length; ++i) {
2827
text[i] = random_u08(rng);
2928
}
3029

31-
setter(m, text, SIZEOF_VLA(text), nullptr);
30+
setter(m, text, length, nullptr);
3231
}
3332

3433
static void alloc_string(uint8_t **to, size_t length)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<<<<<<< HEAD
2+
<<<<<<< HEAD
23
cbc0080745e6606a0267be2e39cf56366010512be7e8167afe0dfad176441c7a /usr/local/bin/tox-bootstrapd
34
=======
45
c028527fe5eecde7daa6ac9dacc47bd4bb765463e8e4b5af3881789797bf81a8 /usr/local/bin/tox-bootstrapd
56
>>>>>>> 8f0775583 (cleanup: Use `memzero(x, s)` instead of `memset(x, 0, s)`.)
7+
=======
8+
8a2cc2e20ac0688b42c6e6211cb1bc5797c276ed52a2322d8b786f8ad562d535 /usr/local/bin/tox-bootstrapd
9+
>>>>>>> 5c093c488 (cleanup: Remove all uses of `SIZEOF_VLA`.)

other/docker/misra/Makefile

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,6 @@ SUPPRESSIONS += 19.2
126126
#
127127
# Reason: We believe it should be used when #define is used in block scope.
128128
SUPPRESSIONS += 20.5
129-
# The # and ## preprocessor operators should not be used.
130-
#
131-
# TODO(iphydf): Remove suppression when VLAs are gone. This is only used in
132-
# the SIZEOF_VLA macro.
133-
SUPPRESSIONS += 20.10
134129
# #define and #undef shall not be used on a reserved identifier or reserved macro name.
135130
#
136131
# Reason: Needed for feature test macros like _DEFAULT_SOURCE.

toxav/rtp.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -806,8 +806,9 @@ int rtp_send_data(RTPSession *session, const uint8_t *data, uint32_t length,
806806
header.flags |= RTP_KEY_FRAME;
807807
}
808808

809-
VLA(uint8_t, rdata, length + RTP_HEADER_SIZE + 1);
810-
memset(rdata, 0, SIZEOF_VLA(rdata));
809+
const uint16_t rdata_size = length + RTP_HEADER_SIZE + 1;
810+
VLA(uint8_t, rdata, rdata_size);
811+
memset(rdata, 0, rdata_size);
811812
rdata[0] = session->payload_type; // packet id == payload_type
812813

813814
if (MAX_CRYPTO_DATA_SIZE > (length + RTP_HEADER_SIZE + 1)) {
@@ -818,10 +819,10 @@ int rtp_send_data(RTPSession *session, const uint8_t *data, uint32_t length,
818819
rtp_header_pack(rdata + 1, &header);
819820
memcpy(rdata + 1 + RTP_HEADER_SIZE, data, length);
820821

821-
if (-1 == rtp_send_custom_lossy_packet(session->tox, session->friend_number, rdata, SIZEOF_VLA(rdata))) {
822+
if (-1 == rtp_send_custom_lossy_packet(session->tox, session->friend_number, rdata, rdata_size)) {
822823
char *netstrerror = net_new_strerror(net_error());
823824
LOGGER_WARNING(session->m->log, "RTP send failed (len: %u)! net error: %s",
824-
(unsigned)SIZEOF_VLA(rdata), netstrerror);
825+
rdata_size, netstrerror);
825826
net_kill_strerror(netstrerror);
826827
}
827828
} else {

toxav/toxav.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -850,12 +850,14 @@ bool toxav_audio_send_frame(ToxAV *av, uint32_t friend_number, const int16_t *pc
850850
goto RETURN;
851851
}
852852

853-
VLA(uint8_t, dest, sample_count + sizeof(sampling_rate)); /* This is more than enough always */
853+
/* This is more than enough always */
854+
const uint16_t dest_size = sample_count + sizeof(sampling_rate);
855+
VLA(uint8_t, dest, dest_size);
854856

855857
sampling_rate = net_htonl(sampling_rate);
856858
memcpy(dest, &sampling_rate, sizeof(sampling_rate));
857859
const int vrc = opus_encode(call->audio->encoder, pcm, sample_count,
858-
dest + sizeof(sampling_rate), SIZEOF_VLA(dest) - sizeof(sampling_rate));
860+
dest + sizeof(sampling_rate), dest_size - sizeof(sampling_rate));
859861

860862
if (vrc < 0) {
861863
LOGGER_WARNING(av->m->log, "Failed to encode frame %s", opus_strerror(vrc));

toxcore/DHT.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1487,13 +1487,14 @@ static int sendnodes_ipv6(const DHT *dht, const IP_Port *ip_port, const uint8_t
14871487
memcpy(plain + 1 + nodes_length, sendback_data, length);
14881488

14891489
const uint32_t crypto_size = 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + CRYPTO_MAC_SIZE;
1490-
VLA(uint8_t, data, 1 + nodes_length + length + crypto_size);
1490+
const uint32_t data_size = 1 + nodes_length + length + crypto_size;
1491+
VLA(uint8_t, data, data_size);
14911492

14921493
const int len = dht_create_packet(dht->mem, dht->rng,
14931494
dht->self_public_key, shared_encryption_key, NET_PACKET_SEND_NODES_IPV6,
1494-
plain, 1 + nodes_length + length, data, SIZEOF_VLA(data));
1495+
plain, 1 + nodes_length + length, data, data_size);
14951496

1496-
if (len != SIZEOF_VLA(data)) {
1497+
if (len != data_size) {
14971498
return -1;
14981499
}
14991500

@@ -1576,7 +1577,8 @@ static bool handle_sendnodes_core(void *object, const IP_Port *source, const uin
15761577
return false;
15771578
}
15781579

1579-
VLA(uint8_t, plain, 1 + data_size + sizeof(uint64_t));
1580+
const uint32_t plain_size = 1 + data_size + sizeof(uint64_t);
1581+
VLA(uint8_t, plain, plain_size);
15801582
const uint8_t *shared_key = dht_get_shared_key_sent(dht, packet + 1);
15811583
const int len = decrypt_data_symmetric(
15821584
shared_key,
@@ -1585,7 +1587,7 @@ static bool handle_sendnodes_core(void *object, const IP_Port *source, const uin
15851587
1 + data_size + sizeof(uint64_t) + CRYPTO_MAC_SIZE,
15861588
plain);
15871589

1588-
if ((unsigned int)len != SIZEOF_VLA(plain)) {
1590+
if ((uint32_t)len != plain_size) {
15891591
return false;
15901592
}
15911593

toxcore/Messenger.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,7 +1223,8 @@ static bool file_sendrequest(const Messenger *m, int32_t friendnumber, uint8_t f
12231223
return false;
12241224
}
12251225

1226-
VLA(uint8_t, packet, 1 + sizeof(file_type) + sizeof(filesize) + FILE_ID_LENGTH + filename_length);
1226+
const uint16_t packet_size = 1 + sizeof(file_type) + sizeof(filesize) + FILE_ID_LENGTH + filename_length;
1227+
VLA(uint8_t, packet, packet_size);
12271228
packet[0] = filenumber;
12281229
file_type = net_htonl(file_type);
12291230
memcpy(packet + 1, &file_type, sizeof(file_type));
@@ -1234,7 +1235,7 @@ static bool file_sendrequest(const Messenger *m, int32_t friendnumber, uint8_t f
12341235
memcpy(packet + 1 + sizeof(file_type) + sizeof(filesize) + FILE_ID_LENGTH, filename, filename_length);
12351236
}
12361237

1237-
return write_cryptpacket_id(m, friendnumber, PACKET_ID_FILE_SENDREQUEST, packet, SIZEOF_VLA(packet), false);
1238+
return write_cryptpacket_id(m, friendnumber, PACKET_ID_FILE_SENDREQUEST, packet, packet_size, false);
12381239
}
12391240

12401241
/** @brief Send a file send request.
@@ -1301,7 +1302,8 @@ static bool send_file_control_packet(const Messenger *m, int32_t friendnumber, b
13011302
return false;
13021303
}
13031304

1304-
VLA(uint8_t, packet, 3 + data_length);
1305+
const uint16_t packet_size = 3 + data_length;
1306+
VLA(uint8_t, packet, packet_size);
13051307

13061308
packet[0] = inbound ? 1 : 0;
13071309
packet[1] = filenumber;
@@ -1311,7 +1313,7 @@ static bool send_file_control_packet(const Messenger *m, int32_t friendnumber, b
13111313
memcpy(packet + 3, data, data_length);
13121314
}
13131315

1314-
return write_cryptpacket_id(m, friendnumber, PACKET_ID_FILE_CONTROL, packet, SIZEOF_VLA(packet), false);
1316+
return write_cryptpacket_id(m, friendnumber, PACKET_ID_FILE_CONTROL, packet, packet_size, false);
13151317
}
13161318

13171319
/** @brief Send a file control request.
@@ -1501,7 +1503,8 @@ static int64_t send_file_data_packet(const Messenger *m, int32_t friendnumber, u
15011503
return -1;
15021504
}
15031505

1504-
VLA(uint8_t, packet, 2 + length);
1506+
const uint16_t packet_size = 2 + length;
1507+
VLA(uint8_t, packet, packet_size);
15051508
packet[0] = PACKET_ID_FILE_DATA;
15061509
packet[1] = filenumber;
15071510

@@ -1510,7 +1513,7 @@ static int64_t send_file_data_packet(const Messenger *m, int32_t friendnumber, u
15101513
}
15111514

15121515
return write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c,
1513-
m->friendlist[friendnumber].friendcon_id), packet, SIZEOF_VLA(packet), true);
1516+
m->friendlist[friendnumber].friendcon_id), packet, packet_size, true);
15141517
}
15151518

15161519
#define MAX_FILE_DATA_SIZE (MAX_CRYPTO_DATA_SIZE - 2)

toxcore/TCP_client.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -394,10 +394,11 @@ int send_data(const Logger *logger, TCP_Client_Connection *con, uint8_t con_id,
394394
return 0;
395395
}
396396

397-
VLA(uint8_t, packet, 1 + length);
397+
const uint16_t packet_size = 1 + length;
398+
VLA(uint8_t, packet, packet_size);
398399
packet[0] = con_id + NUM_RESERVED_PORTS;
399400
memcpy(packet + 1, data, length);
400-
return write_packet_tcp_secure_connection(logger, &con->con, packet, SIZEOF_VLA(packet), false);
401+
return write_packet_tcp_secure_connection(logger, &con->con, packet, packet_size, false);
401402
}
402403

403404
/**
@@ -412,11 +413,12 @@ int send_oob_packet(const Logger *logger, TCP_Client_Connection *con, const uint
412413
return -1;
413414
}
414415

415-
VLA(uint8_t, packet, 1 + CRYPTO_PUBLIC_KEY_SIZE + length);
416+
const uint16_t packet_size = 1 + CRYPTO_PUBLIC_KEY_SIZE + length;
417+
VLA(uint8_t, packet, packet_size);
416418
packet[0] = TCP_PACKET_OOB_SEND;
417419
memcpy(packet + 1, public_key, CRYPTO_PUBLIC_KEY_SIZE);
418420
memcpy(packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, data, length);
419-
return write_packet_tcp_secure_connection(logger, &con->con, packet, SIZEOF_VLA(packet), false);
421+
return write_packet_tcp_secure_connection(logger, &con->con, packet, packet_size, false);
420422
}
421423

422424

@@ -536,10 +538,11 @@ int send_disconnect_request(const Logger *logger, TCP_Client_Connection *con, ui
536538
*/
537539
int send_onion_request(const Logger *logger, TCP_Client_Connection *con, const uint8_t *data, uint16_t length)
538540
{
539-
VLA(uint8_t, packet, 1 + length);
541+
const uint16_t packet_size = 1 + length;
542+
VLA(uint8_t, packet, packet_size);
540543
packet[0] = TCP_PACKET_ONION_REQUEST;
541544
memcpy(packet + 1, data, length);
542-
return write_packet_tcp_secure_connection(logger, &con->con, packet, SIZEOF_VLA(packet), false);
545+
return write_packet_tcp_secure_connection(logger, &con->con, packet, packet_size, false);
543546
}
544547

545548
void onion_response_handler(TCP_Client_Connection *con, tcp_onion_response_cb *onion_callback, void *object)

toxcore/TCP_common.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -151,46 +151,47 @@ int write_packet_tcp_secure_connection(const Logger *logger, TCP_Connection *con
151151
}
152152
}
153153

154-
VLA(uint8_t, packet, sizeof(uint16_t) + length + CRYPTO_MAC_SIZE);
154+
const uint16_t packet_size = sizeof(uint16_t) + length + CRYPTO_MAC_SIZE;
155+
VLA(uint8_t, packet, packet_size);
155156

156157
uint16_t c_length = net_htons(length + CRYPTO_MAC_SIZE);
157158
memcpy(packet, &c_length, sizeof(uint16_t));
158159
int len = encrypt_data_symmetric(con->shared_key, con->sent_nonce, data, length, packet + sizeof(uint16_t));
159160

160-
if ((unsigned int)len != (SIZEOF_VLA(packet) - sizeof(uint16_t))) {
161+
if ((unsigned int)len != (packet_size - sizeof(uint16_t))) {
161162
return -1;
162163
}
163164

164165
if (priority) {
165-
len = sendpriority ? net_send(con->ns, logger, con->sock, packet, SIZEOF_VLA(packet), &con->ip_port) : 0;
166+
len = sendpriority ? net_send(con->ns, logger, con->sock, packet, packet_size, &con->ip_port) : 0;
166167

167168
if (len <= 0) {
168169
len = 0;
169170
}
170171

171172
increment_nonce(con->sent_nonce);
172173

173-
if ((unsigned int)len == SIZEOF_VLA(packet)) {
174+
if ((unsigned int)len == packet_size) {
174175
return 1;
175176
}
176177

177-
return add_priority(con, packet, SIZEOF_VLA(packet), len) ? 1 : 0;
178+
return add_priority(con, packet, packet_size, len) ? 1 : 0;
178179
}
179180

180-
len = net_send(con->ns, logger, con->sock, packet, SIZEOF_VLA(packet), &con->ip_port);
181+
len = net_send(con->ns, logger, con->sock, packet, packet_size, &con->ip_port);
181182

182183
if (len <= 0) {
183184
return 0;
184185
}
185186

186187
increment_nonce(con->sent_nonce);
187188

188-
if ((unsigned int)len == SIZEOF_VLA(packet)) {
189+
if ((unsigned int)len == packet_size) {
189190
return 1;
190191
}
191192

192-
memcpy(con->last_packet, packet, SIZEOF_VLA(packet));
193-
con->last_packet_length = SIZEOF_VLA(packet);
193+
memcpy(con->last_packet, packet, packet_size);
194+
con->last_packet_length = packet_size;
194195
con->last_packet_sent = len;
195196
return 1;
196197
}

0 commit comments

Comments
 (0)