From b5a9757ae0af4ac80e8845519e11460b5ba0d4bc Mon Sep 17 00:00:00 2001 From: kts Date: Mon, 2 Dec 2013 16:10:23 -0800 Subject: [PATCH] Fixed a segfault that would occur in adding a User, as a realloc was used when a malloc should have been. --- Makefile | 4 ++++ users.c | 13 +++---------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index 809e433..09a7de1 100644 --- a/Makefile +++ b/Makefile @@ -21,6 +21,10 @@ nchat: $(OBJS) $(CON_OBJS) xnchat: $(OBJS) $(X11_OBJS) $(CC) $(OBJS) $(X11_OBJS) $(LFLAGS) $(X11_LFLAGS) -o xnchat +install: nchat + cp -f nchat /usr/local/bin/ + chmod +x /usr/local/bin/nchat + all: nchat xnchat clean: diff --git a/users.c b/users.c index 87b1923..3a307a5 100644 --- a/users.c +++ b/users.c @@ -11,23 +11,16 @@ int checkUser(UserList *user_list, const char nick[], struct in_addr address) { } int addUser(UserList *user_list, const char nick[], struct in_addr address) { + int nick_length = strlen(nick)+1; 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 = malloc(nick_length); 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)); -// 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)); + memcpy(new_user.nick, nick, nick_length); addUserAddress(&new_user, address); - //new_user.last_address = &new_user.addresses[new_user.address_count-1]; user_list->users[user_list->length-1] = new_user; return 0; }