Skip to content

Commit 0f12f38

Browse files
committed
cleanup: Reduce stack frame sizes to below 4096 bytes.
This is currently only detectable in tests, because the main code uses VLAs where stack frame size is unknown at compile time.
1 parent bc43cec commit 0f12f38

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

auto_tests/TCP_test.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,18 @@ static void test_basic(void)
165165
}
166166

167167
// Receiving the second response and verifying its validity
168-
uint8_t packet_resp[4096];
168+
const size_t max_packet_size = 4096;
169+
uint8_t *packet_resp = (uint8_t *)malloc(max_packet_size);
170+
ck_assert(packet_resp != nullptr);
169171
int recv_data_len = net_recv(ns, logger, sock, packet_resp, 2 + 2 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE, &localhost);
170172
ck_assert_msg(recv_data_len == 2 + 2 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE,
171173
"Failed to receive server response to request. %d", recv_data_len);
172174
memcpy(&size, packet_resp, 2);
173175
ck_assert_msg(net_ntohs(size) == 2 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE,
174176
"Wrong packet size for request response.");
175177

176-
uint8_t packet_resp_plain[4096];
178+
uint8_t *packet_resp_plain = (uint8_t *)malloc(max_packet_size);
179+
ck_assert(packet_resp_plain != nullptr);
177180
ret = decrypt_data_symmetric(mem, f_shared_key, f_nonce_r, packet_resp + 2, recv_data_len - 2, packet_resp_plain);
178181
ck_assert_msg(ret != -1, "Failed to decrypt the TCP server's response.");
179182
increment_nonce(f_nonce_r);
@@ -183,6 +186,9 @@ static void test_basic(void)
183186
ck_assert_msg(packet_resp_plain[1] == 0, "Server did not refuse the connection.");
184187
ck_assert_msg(pk_equal(packet_resp_plain + 2, f_public_key), "Server sent the wrong public key.");
185188

189+
free(packet_resp_plain);
190+
free(packet_resp);
191+
186192
// Closing connections.
187193
kill_sock(ns, sock);
188194
kill_tcp_server(tcp_s);
@@ -337,7 +343,8 @@ static void test_some(void)
337343
do_tcp_server_delay(tcp_s, mono_time, 50);
338344

339345
// Testing response from connection 1
340-
uint8_t data[2048];
346+
const size_t max_packet_size = 4096;
347+
uint8_t *data = (uint8_t *)malloc(max_packet_size);
341348
int len = read_packet_sec_tcp(logger, con1, data, 2 + 1 + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE);
342349
ck_assert_msg(len == 1 + 1 + CRYPTO_PUBLIC_KEY_SIZE, "Wrong response packet length of %d.", len);
343350
ck_assert_msg(data[0] == TCP_PACKET_ROUTING_RESPONSE, "Wrong response packet id of %d.", data[0]);
@@ -351,7 +358,7 @@ static void test_some(void)
351358
ck_assert_msg(data[1] == 16, "Server didn't refuse connection using wrong public key.");
352359
ck_assert_msg(pk_equal(data + 2, con1->public_key), "Key in response packet wrong.");
353360

354-
uint8_t test_packet[512] = {16, 17, 16, 86, 99, 127, 255, 189, 78}; // What is this packet????
361+
const uint8_t test_packet[512] = {16, 17, 16, 86, 99, 127, 255, 189, 78}; // What is this packet????
355362

356363
write_packet_tcp_test_connection(logger, con3, test_packet, sizeof(test_packet));
357364
write_packet_tcp_test_connection(logger, con3, test_packet, sizeof(test_packet));
@@ -406,6 +413,8 @@ static void test_some(void)
406413
ck_assert_msg(data[0] == TCP_PACKET_PONG, "wrong packet id %u", data[0]);
407414
ck_assert_msg(memcmp(ping_packet + 1, data + 1, sizeof(uint64_t)) == 0, "wrong packet data");
408415

416+
free(data);
417+
409418
// Kill off the connections
410419
kill_tcp_server(tcp_s);
411420
kill_tcp_con(con1);

auto_tests/save_compatibility_test.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,10 @@ static bool is_little_endian(void)
145145
// cppcheck-suppress constParameter
146146
int main(int argc, char *argv[])
147147
{
148-
char base_path[4096] = {0};
148+
const size_t base_path_size = 4096;
149+
char *base_path = (char *)malloc(base_path_size);
150+
ck_assert(base_path != nullptr);
151+
memset(base_path, 0, 4096);
149152

150153
if (argc <= 1) {
151154
const char *srcdir = getenv("srcdir");
@@ -154,21 +157,29 @@ int main(int argc, char *argv[])
154157
srcdir = ".";
155158
}
156159

157-
snprintf(base_path, sizeof(base_path), "%s", srcdir);
160+
snprintf(base_path, base_path_size, "%s", srcdir);
158161
} else {
159-
snprintf(base_path, sizeof(base_path), "%s", argv[1]);
162+
snprintf(base_path, base_path_size, "%s", argv[1]);
160163
base_path[strrchr(base_path, '/') - base_path] = '\0';
161164
}
162165

163166
if (is_little_endian()) {
164-
char save_path[4096 + sizeof(LOADED_SAVE_FILE_LITTLE)];
165-
snprintf(save_path, sizeof(save_path), "%s/%s", base_path, LOADED_SAVE_FILE_LITTLE);
167+
const size_t save_path_size = 4096 + sizeof(LOADED_SAVE_FILE_LITTLE);
168+
char *save_path = (char *)malloc(save_path_size);
169+
ck_assert(save_path != nullptr);
170+
snprintf(save_path, save_path_size, "%s/%s", base_path, LOADED_SAVE_FILE_LITTLE);
166171
test_save_compatibility(save_path);
172+
free(save_path);
167173
} else {
168-
char save_path[4096 + sizeof(LOADED_SAVE_FILE_BIG)];
169-
snprintf(save_path, sizeof(save_path), "%s/%s", base_path, LOADED_SAVE_FILE_BIG);
174+
const size_t save_path_size = 4096 + sizeof(LOADED_SAVE_FILE_BIG);
175+
char *save_path = (char *)malloc(save_path_size);
176+
ck_assert(save_path != nullptr);
177+
snprintf(save_path, save_path_size, "%s/%s", base_path, LOADED_SAVE_FILE_BIG);
170178
test_save_compatibility(save_path);
179+
free(save_path);
171180
}
172181

182+
free(base_path);
183+
173184
return 0;
174185
}

0 commit comments

Comments
 (0)