potential seg fault

This commit is contained in:
Jfreegman 2013-08-11 00:55:09 -04:00
parent 56df7e556e
commit 2c2d608e29

View File

@ -140,6 +140,7 @@ static void init_tox()
} }
#define MAXLINE 90 /* Approx max number of chars in a sever line (IP + port + key) */ #define MAXLINE 90 /* Approx max number of chars in a sever line (IP + port + key) */
#define MINLINE 70
#define MAXSERVERS 50 #define MAXSERVERS 50
/* Connects to a random DHT server listed in the DHTservers file */ /* Connects to a random DHT server listed in the DHTservers file */
@ -149,7 +150,7 @@ int init_connection(void)
return 0; return 0;
FILE *fp = fopen("../../../other/DHTservers", "r"); FILE *fp = fopen("../../../other/DHTservers", "r");
if (fp == NULL) if (!fp)
return 1; return 1;
char servers[MAXSERVERS][MAXLINE]; char servers[MAXSERVERS][MAXLINE];
@ -157,7 +158,7 @@ int init_connection(void)
int linecnt = 0; int linecnt = 0;
while (fgets(line, sizeof(line), fp) && linecnt < MAXSERVERS) { while (fgets(line, sizeof(line), fp) && linecnt < MAXSERVERS) {
int len = strlen(line); int len = strlen(line);
if (len > 74 && len < MAXLINE) if (len > MINLINE && len < MAXLINE)
strcpy(servers[linecnt++], line); strcpy(servers[linecnt++], line);
} }
if (linecnt < 1) { if (linecnt < 1) {
@ -166,17 +167,18 @@ int init_connection(void)
} }
fclose(fp); fclose(fp);
int servnum = rand() % linecnt; char *server = servers[rand() % linecnt];
char *server = servers[servnum];
char *ip = strtok(server, " "); char *ip = strtok(server, " ");
char *port = strtok(NULL, " "); char *port = strtok(NULL, " ");
char *key = strtok(NULL, " "); char *key = strtok(NULL, " ");
if (!ip || !port || !key)
return 3;
IP_Port dht; IP_Port dht;
dht.port = htons(atoi(port)); dht.port = htons(atoi(port));
uint32_t resolved_address = resolve_addr(ip); uint32_t resolved_address = resolve_addr(ip);
if (resolved_address == 0) if (resolved_address == 0)
return 3; return 4;
dht.ip.i = resolved_address; dht.ip.i = resolved_address;
unsigned char *binary_string = hex_string_to_bin(key); unsigned char *binary_string = hex_string_to_bin(key);
DHT_bootstrap(dht, binary_string); DHT_bootstrap(dht, binary_string);