Fix some memory or file descriptor leaks in test code.

Also some missing return value checks for `fopen`.
This commit is contained in:
iphydf 2018-01-14 11:48:35 +00:00
parent bacd74c4dd
commit 1eea3f0ab6
No known key found for this signature in database
GPG Key ID: 3855DBA2D74403C9
7 changed files with 70 additions and 7 deletions

View File

@ -137,6 +137,7 @@ START_TEST(test_basic)
ck_assert_msg(packet_resp_plain[0] == 1, "wrong packet id %u", packet_resp_plain[0]);
ck_assert_msg(packet_resp_plain[1] == 0, "connection not refused %u", packet_resp_plain[1]);
ck_assert_msg(public_key_cmp(packet_resp_plain + 2, f_public_key) == 0, "key in packet wrong");
kill_sock(sock);
kill_TCP_server(tcp_s);
}
END_TEST

View File

@ -298,17 +298,58 @@ static Onions *new_onions(uint16_t port)
IP ip = get_loopback();
ip.ip6.uint8[15] = 1;
Onions *on = (Onions *)malloc(sizeof(Onions));
DHT *dht = new_DHT(NULL, new_networking(NULL, ip, port), true);
if (!on) {
return NULL;
}
Networking_Core *net = new_networking(NULL, ip, port);
if (!net) {
free(on);
return NULL;
}
DHT *dht = new_DHT(NULL, net, true);
if (!dht) {
kill_networking(net);
free(on);
return NULL;
}
on->onion = new_onion(dht);
if (!on->onion) {
kill_DHT(dht);
kill_networking(net);
free(on);
return NULL;
}
on->onion_a = new_onion_announce(dht);
if (!on->onion_a) {
kill_onion(on->onion);
kill_DHT(dht);
kill_networking(net);
free(on);
return NULL;
}
TCP_Proxy_Info inf = {{{0}}};
on->onion_c = new_onion_client(new_net_crypto(NULL, dht, &inf));
if (on->onion && on->onion_a && on->onion_c) {
return on;
if (!on->onion_c) {
kill_onion_announce(on->onion_a);
kill_onion(on->onion);
kill_DHT(dht);
kill_networking(net);
free(on);
return NULL;
}
return NULL;
return on;
}
static void do_onions(Onions *on)

View File

@ -146,8 +146,13 @@ int main(int argc, char *argv[])
#endif
FILE *file;
file = fopen("PUBLIC_ID.txt", "w");
const char *const public_id_filename = "PUBLIC_ID.txt";
FILE *file = fopen(public_id_filename, "w");
if (file == NULL) {
printf("Could not open file \"%s\" for writing. Exiting...\n", public_id_filename);
exit(1);
}
for (i = 0; i < 32; i++) {
printf("%02hhX", dht->self_public_key[i]);

View File

@ -58,6 +58,13 @@ static bool type_check(msgpack_packer *pk, msgpack_object req, int index,
return true;
}
static int close_ptr(int *fd)
{
if (fd && *fd >= 0) {
close(*fd);
}
}
static int write_sample_input(msgpack_object req)
{
static unsigned int n;
@ -70,7 +77,7 @@ static int write_sample_input(msgpack_object req)
memcpy(filename + strlen(filename) + name.size, ".mp", 4);
memcpy(filename + strlen(filename), name.ptr, name.size);
int fd = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
int fd __attribute__((__cleanup__(close_ptr))) = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
if (fd < 0)
// If we can't open the sample file, we just don't write it.

View File

@ -75,6 +75,7 @@ static int reconnect(void)
if (connect(new_sock, (struct sockaddr *)&addr, addrsize) != 0) {
printf("error connect\n");
close(new_sock);
return -1;
}

View File

@ -1261,6 +1261,13 @@ static void write_file(Tox *tox, uint32_t friendnumber, uint32_t filenumber, uin
pFile = fopen(filename, "wb");
}
if (pFile == NULL) {
char msg[512];
sprintf(msg, "Could not open file \"%s\" for writing.", filename);
new_lines(msg);
return;
}
fseek(pFile, position, SEEK_SET);
if (fwrite(data, length, 1, pFile) != 1) {

View File

@ -95,6 +95,7 @@ static uint32_t add_filesender(Tox *m, uint16_t friendnum, char *filename)
strlen(filename), 0);
if (filenum == -1) {
fclose(tempfile);
return -1;
}