Fixed memory leaks and removed repeated code

This commit is contained in:
Maxim Biro 2014-02-09 00:59:03 -05:00
parent 5fad72b910
commit ecbceac341
7 changed files with 27 additions and 48 deletions

View File

@ -14,6 +14,7 @@
#include "config.h"
#endif
#include "../testing/misc_tools.c" // hex_string_to_bin
#include "../toxcore/Messenger.h"
#include "../toxcore/Lossless_UDP.h"
#include <sys/types.h>
@ -41,18 +42,6 @@ int friend_id_num = 0;
Messenger *m;
unsigned char *hex_string_to_bin(char hex_string[])
{
size_t i, len = strlen(hex_string);
unsigned char *val = calloc(1, len);
char *pos = hex_string;
for (i = 0; i < len; ++i, pos += 2)
sscanf(pos, "%2hhx", &val[i]);
return val;
}
START_TEST(test_m_sendmesage)
{
char *message = "h-hi :3";

View File

@ -266,8 +266,9 @@ int bootstrap_from_config(char *cfg_file_path, DHT *dht, int enable_ipv6)
goto next;
}
const int address_resolved = DHT_bootstrap_from_address(dht, bs_address, enable_ipv6, htons(bs_port),
hex_string_to_bin((char *)bs_public_key));
uint8_t *bs_public_key_bin = hex_string_to_bin((char *)bs_public_key);
const int address_resolved = DHT_bootstrap_from_address(dht, bs_address, enable_ipv6, htons(bs_port), bs_public_key_bin);
free(bs_public_key_bin);
if (!address_resolved) {
syslog(LOG_WARNING, "Bootstrap server #%d: Invalid '%s': %s. Skipping the server.\n", i, NAME_ADDRESS, bs_address);

View File

@ -17,19 +17,7 @@
*/
#include <sodium.h>
#include <string.h>
unsigned char *hex_string_to_bin(char hex_string[])
{
size_t len = strlen(hex_string);
unsigned char *val = malloc(len);
char *pos = hex_string;
int i;
for (i = 0; i < len; ++i, pos += 2)
sscanf(pos, "%2hhx", &val[i]);
return val;
}
#include "../../testing/misc_tools.c" // hex_string_to_bin
int load_file(char *filename, char **result)
{
@ -90,6 +78,7 @@ int main(int argc, char *argv[])
unsigned long long smlen;
char *sm = malloc(size + crypto_sign_ed25519_BYTES * 2);
crypto_sign_ed25519(sm, &smlen, data, size, secret_key);
free(secret_key);
if (smlen - size != crypto_sign_ed25519_BYTES)
goto fail;

View File

@ -205,8 +205,10 @@ int main(int argc, char *argv[])
if (scanf("%s", temp_id) != 1)
exit(0);
DHT_addfriend(dht, hex_string_to_bin(temp_id));
uint8_t *bin_id = hex_string_to_bin(temp_id);
DHT_addfriend(dht, bin_id);
free(bin_id);
perror("Initialization");

View File

@ -161,14 +161,16 @@ int main(int argc, char *argv[])
setname(m, (uint8_t *)"Anon", 5);
char temp_id[128];
char temp_hex_id[128];
printf("\nEnter the address of the friend you wish to add (38 bytes HEX format):\n");
if (scanf("%s", temp_id) != 1) {
if (scanf("%s", temp_hex_id) != 1) {
return 1;
}
int num = m_addfriend(m, hex_string_to_bin(temp_id), (uint8_t *)"Install Gentoo", sizeof("Install Gentoo"));
uint8_t *bin_id = hex_string_to_bin(temp_hex_id);
int num = m_addfriend(m, bin_id, (uint8_t *)"Install Gentoo", sizeof("Install Gentoo"));
free(bin_id);
perror("Initialization");

View File

@ -1,4 +1,6 @@
#include "../../toxcore/group_chats.h"
#include "../misc_tools.c" // hex_string_to_bin
#define NUM_CHATS 8
#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
@ -54,19 +56,6 @@ void print_group(Group_Chat *chat)
}
}
unsigned char *hex_string_to_bin(char hex_string[])
{
size_t len = strlen(hex_string);
unsigned char *val = malloc(len);
char *pos = hex_string;
int i;
for (i = 0; i < len; ++i, pos += 2)
sscanf(pos, "%2hhx", &val[i]);
return val;
}
void print_message(Group_Chat *chat, int peer_number, uint8_t *message, uint16_t length, void *userdata)
{
printf("%u: %s | %u\n", peer_number, message, length);
@ -95,8 +84,12 @@ int main(int argc, char *argv[])
* bootstrap_ip_port.ip.c[2] = 0;
* bootstrap_ip_port.ip.c[3] = 1; */
bootstrap_ip_port.ip.uint32 = inet_addr(argv[1]);
uint8_t *bootstrap_id = hex_string_to_bin(argv[3]);
chat_bootstrap(chat, bootstrap_ip_port, hex_string_to_bin(argv[3]));
chat_bootstrap(chat, bootstrap_ip_port, bootstrap_id);
free(bootstrap_id);
while (1) {

View File

@ -221,6 +221,7 @@ int main(int argc, char *argv[])
uint16_t port = htons(atoi(argv[argvoffset + 2]));
unsigned char *binary_string = hex_string_to_bin(argv[argvoffset + 3]);
int res = tox_bootstrap_from_address(tox, argv[argvoffset + 1], ipv6enabled, port, binary_string);
free(binary_string);
if (!res) {
printf("Failed to convert \"%s\" into an IP address. Exiting...\n", argv[argvoffset + 1]);
@ -242,7 +243,9 @@ int main(int argc, char *argv[])
return 1;
}
int num = tox_add_friend(tox, hex_string_to_bin(temp_id), (uint8_t *)"Install Gentoo", sizeof("Install Gentoo"));
uint8_t *bin_id = hex_string_to_bin(temp_id);
int num = tox_add_friend(tox, bin_id, (uint8_t *)"Install Gentoo", sizeof("Install Gentoo"));
free(bin_id);
if (num < 0) {
printf("\nSomething went wrong when adding friend.\n");