Fixed some behavioral differences between Darwin and Linux w/ unitialized values (didn't notice due to this), along with an improperly named #include due to differences in case sensitivity. Also fixed memory leaks in the new User/UserList related code.

master
kts 2013-08-17 00:01:47 -07:00
parent 3f6cfbca8e
commit 6e12546e06
6 changed files with 22 additions and 6 deletions

View File

@ -8,7 +8,7 @@ Common
#ifndef COMMON_H
#define COMMON_H
#include "Net.h" // for User
#include "net.h" // for User
#include "macros.h"
#include <stdio.h>

View File

@ -174,9 +174,9 @@ void showUsers() {
printf("--users--");
clearAttributes();
int i;
int j;
for (i=0;i<user_list.length;i++) {
printf("\n%s",user_list.users[i].nick);
int j;
for (j=0;j<user_list.users[i].address_count;j++) {
printf("\n %s", inet_ntoa(user_list.users[i].addresses[j]));
}
@ -208,7 +208,7 @@ void showInterfaces() {
fputs("\033[32m", stdout);
for (i = 0; i < interfaces; i++) {
if (ip_list[i]) {
printf("%s: %s\n", dev_list[i], ip_list[i]);
printf("%i: %i\n", dev_list[i], ip_list[i]);
}
}
fputs("\033[0m",stdout);

1
main.c
View File

@ -108,6 +108,7 @@ int main() {
}
closeInterface();
stopSockets(NULL);
freeMessage(&current_message);
freeInterfaces();
freeUserList(&user_list);
freeHelp();

10
net.c
View File

@ -59,6 +59,8 @@ void handleNetInput(const char *data_buffer) {
}
Message buildMessage(const char *data_buffer, struct sockaddr_in *socket) {
// free the old malloc'd ish
freeMessage(&current_message);
Message message;
message.address = socket->sin_addr;
int offset = 0;
@ -81,6 +83,11 @@ Message buildMessage(const char *data_buffer, struct sockaddr_in *socket) {
return message;
}
int freeMessage(Message *message) {
free(message->nick);
return 0;
}
int openListenSocket() {
int ip_hash = generateHash("ip", CONFIG_HASH_SIZE);
int port_hash = generateHash("port", CONFIG_HASH_SIZE);
@ -454,5 +461,8 @@ void freeInterfaces() {
if (ip_list[i]) {
free(ip_list[i]);
}
if (dev_list[i]) {
free(dev_list[i]);
}
}
}

2
net.h
View File

@ -122,6 +122,8 @@ typedef struct {
} Message;
Message buildMessage(const char *data_buffer, struct sockaddr_in *socket);
/* frees the malloc'd nick */
int freeMessage(Message *message);
Message current_message; // global message struct, contains last packet

View File

@ -12,14 +12,17 @@ int checkUser(UserList *user_list, const char nick[], struct in_addr address) {
int addUser(UserList *user_list, const char nick[], struct in_addr address) {
user_list->length++;
//printf("Adding!\n");
user_list->users = realloc(user_list->users, (user_list->length)*sizeof(User));
User new_user;
new_user.last_address = address;
//new_user.nick = realloc(new_user.nick, strlen(nick)+1);
new_user.nick = malloc(strlen(nick));
new_user.nick = realloc(new_user.nick, strlen(nick)+1);
// new_user.nick = malloc(strlen(nick));
new_user.addresses = malloc(1);
new_user.address_count = 0;
//strcpy(new_user.nick, nick);
memcpy(new_user.nick, nick, strlen(nick)+1);
//memcpy(new_user.nick, nick, strlen(nick)+1);
memcpy(new_user.nick, nick, strlen(nick));
// new_user.last_address = malloc(sizeof(struct in_addr));
// memcpy(new_user.last_address, &address, sizeof(struct in_addr));
//new_user.last_address = malloc(new_user.last_address, sizeof(struct in_addr));