From 99da248ac22b3216372e7f6c5e39fa467527628c Mon Sep 17 00:00:00 2001 From: Andre Date: Sat, 15 Jul 2017 17:58:51 +0200 Subject: [PATCH] fixed all mem leaks on server side apart from environ --- inc/ft_p.h | 4 +-- libft/ft_strtrim.c | 1 + libftp/src/chdir.c | 1 + src/server/cd.c | 10 ++++-- src/server/handle_request.c | 15 +++++++-- src/server/ls.c | 67 +++++++++++++++++++++++++++++++++---- src/server/main.c | 4 +-- 7 files changed, 87 insertions(+), 15 deletions(-) diff --git a/inc/ft_p.h b/inc/ft_p.h index e653feb..9beba5a 100644 --- a/inc/ft_p.h +++ b/inc/ft_p.h @@ -17,9 +17,9 @@ # include void ft_pwd(char*); -void ft_cd(char *, char *, char **); +void ft_cd(char *, char **); void ft_ls(char *); void ft_invalid(char *); -void ft_handle_request(char *, int, char *); +void ft_handle_request(char *, int); #endif diff --git a/libft/ft_strtrim.c b/libft/ft_strtrim.c index dbf69b6..a438762 100644 --- a/libft/ft_strtrim.c +++ b/libft/ft_strtrim.c @@ -75,6 +75,7 @@ char *ft_strtrim(char const *s) { len = start_end[1] - start_end[0] + 1; copy = ft_strsub(s, start_end[0], len); + free(start_end); } } return (copy); diff --git a/libftp/src/chdir.c b/libftp/src/chdir.c index 359c778..762208d 100644 --- a/libftp/src/chdir.c +++ b/libftp/src/chdir.c @@ -13,6 +13,7 @@ int ft_wchdir(char *path) { ft_set_environ("OLDPWD", cwd); } + ft_strdel(&cwd); } return (n); } \ No newline at end of file diff --git a/src/server/cd.c b/src/server/cd.c index aad8c4b..23a9fed 100644 --- a/src/server/cd.c +++ b/src/server/cd.c @@ -4,14 +4,14 @@ ** Change the server directory */ -void ft_cd(char *buff, char *root_dir, char **argv) +void ft_cd(char *buff, char **argv) { char *resp; char *path; resp = NULL; path = NULL; - if (buff && root_dir && argv) + if (buff && argv) { /* @@ -45,6 +45,12 @@ void ft_cd(char *buff, char *root_dir, char **argv) */ ft_fill_buffer(buff, resp); + + /* + ** free path + */ + + ft_strdel(&path); } } else diff --git a/src/server/handle_request.c b/src/server/handle_request.c index 2a393b4..9f09c94 100644 --- a/src/server/handle_request.c +++ b/src/server/handle_request.c @@ -12,7 +12,7 @@ void ft_send_response(char *buff, int connfd) } } -void ft_handle_request(char *line, int connfd, char *root) +void ft_handle_request(char *line, int connfd) { char **argv; char buff[MAXLINE]; @@ -22,6 +22,11 @@ void ft_handle_request(char *line, int connfd, char *root) */ argv = ft_get_argv(line); + + /* + ** clear buff + */ + ft_bzero(buff, MAXLINE); /* @@ -31,7 +36,7 @@ void ft_handle_request(char *line, int connfd, char *root) if (ft_strequ(argv[0], "pwd")) ft_pwd(buff); else if (ft_strequ(argv[0], "cd")) - ft_cd(buff, root, argv); + ft_cd(buff, argv); else if (ft_strequ(argv[0], "ls")) ft_ls(buff); else @@ -42,4 +47,10 @@ void ft_handle_request(char *line, int connfd, char *root) */ ft_send_response(buff, connfd); + + /* + ** free + */ + + ft_dstrdel(argv); } \ No newline at end of file diff --git a/src/server/ls.c b/src/server/ls.c index 7a10bc5..a711085 100644 --- a/src/server/ls.c +++ b/src/server/ls.c @@ -1,14 +1,58 @@ #include -// this function has a fuck load of memory leaks +/* +** append src to dst after newline is appended to src +*/ -char *read_dir(char *dir) +static char *append(char *dst, char *src) +{ + char *final; + char *append; + + final = NULL; + append = NULL; + if (dst && src) + { + + /* + ** append newline to src + */ + + if ((append = ft_strjoin(src, "\n" ))) + { + + /* + ** append newlined src to dst + */ + + if ((final = ft_strjoin(dst, append))) + { + + /* + ** argument dst is a malloced str that append must free + */ + + ft_strdel(&dst); + + } + + ft_strdel(&append); + } + } + return (final); +} + +/* +** this function has a fuck load of memory leaks +*/ + +static char *read_dir(char *dir) { DIR *p_dir; struct dirent *drnt; char *dir_contents; - if ((dir_contents = ft_strnew(1))) + if ((dir_contents = ft_strdup(""))) { /* @@ -29,7 +73,7 @@ char *read_dir(char *dir) ** append the contents to the char * alone with a newline */ - dir_contents = ft_strjoin( dir_contents, ft_strjoin( drnt->d_name, "\n" ) ); + dir_contents = append(dir_contents, drnt->d_name); } @@ -72,14 +116,25 @@ void ft_ls(char *buff) { /* - ** fill buff with direcotory contents + ** fill buff with direcotory contents, + ** using resp if newline was found */ ft_fill_buffer(buff, resp); - ft_strdel(&dir_contents); } else + { + + /* + ** fill buff with direcotory contents, + ** using dir_con if newline was not found + */ + ft_fill_buffer(buff, dir_contents); + + } + ft_strdel(&dir_contents); + ft_strdel(&resp); } else ft_err_quit("ft_ls fail"); diff --git a/src/server/main.c b/src/server/main.c index 06be2ae..b7cf923 100644 --- a/src/server/main.c +++ b/src/server/main.c @@ -8,9 +8,7 @@ int main(void) socklen_t clilen; struct sockaddr_in cliaddr, servaddr; char buff[MAXLINE]; - char *root; - root = ft_wgetcwd(); /* ** create a socket */ @@ -71,7 +69,7 @@ int main(void) ** use buff to handle client request */ - ft_handle_request(buff, connfd, root); + ft_handle_request(buff, connfd); }