From d7fe6cd47638fe2ac7a15a3f8eb3250869b5347a Mon Sep 17 00:00:00 2001 From: dm1sh Date: Fri, 30 May 2025 22:35:11 +0300 Subject: [PATCH] Fixed unfreed memory and files handling errors --- include/utils_op/utils.h | 2 +- src/articles_p/article.c | 4 ++-- src/articles_p/html.c | 2 +- src/file_op/file.c | 24 +++++++++++++++++++++--- src/file_op/mime.c | 2 +- src/gallery_p/gallery.c | 30 +++++++++++++++++++----------- src/netw_op/request.c | 29 ++++++++++++++++++++++++----- src/projects_p/projects.c | 2 +- src/utils_op/utils.c | 14 ++++++++------ 9 files changed, 78 insertions(+), 31 deletions(-) diff --git a/include/utils_op/utils.h b/include/utils_op/utils.h index 5304b78..2ff8ac0 100644 --- a/include/utils_op/utils.h +++ b/include/utils_op/utils.h @@ -8,7 +8,7 @@ #include void err_msg(char *msg); -char *concat_to_front(char **str1, char *str2); +char *prepend(char *dest, const char *prefix); char *get_status_message(int status_code); char *to_lower(char *str); char *trim(char *str); diff --git a/src/articles_p/article.c b/src/articles_p/article.c index 599013f..8179c87 100644 --- a/src/articles_p/article.c +++ b/src/articles_p/article.c @@ -45,7 +45,7 @@ int list_articles(article_info **articles) else (*articles)[articles_amount - 1].title = strdup("No title"); - (*articles)[articles_amount - 1].content = malloc(0); + (*articles)[articles_amount - 1].content = NULL; free(tmp); @@ -124,7 +124,7 @@ void free_article_info_arr(article_info **articles, int length) // /* Only for testing purposes */ // int main() // { -// article_info *articles = malloc(0); +// article_info *articles = NULL; // int n = list_articles(&articles); diff --git a/src/articles_p/html.c b/src/articles_p/html.c index d49f86c..271defa 100644 --- a/src/articles_p/html.c +++ b/src/articles_p/html.c @@ -84,7 +84,7 @@ int gen_html_article_list(article_info *articles, int n, char **out) // /* Only for testing purposes */ // int main () // { -// article_info *articles = malloc(0); +// article_info *articles = NULL; // int n = list_articles(&articles); diff --git a/src/file_op/file.c b/src/file_op/file.c index 9c60e1a..90aece8 100644 --- a/src/file_op/file.c +++ b/src/file_op/file.c @@ -21,12 +21,30 @@ char *gen_file_path(char *req_path) path = strcat(path, "index.html"); } - char *webroot = "static"; + char webroot[PATH_MAX]; + if (getcwd(webroot, PATH_MAX) == NULL) + goto exit_error; - path = realloc(path, strlen(path) + strlen(webroot) + 1); - path = concat_to_front(&path, webroot); + if (PATH_MAX < strlen(webroot) + strlen("/static")) + goto exit_error; + + strcat(webroot, "/static"); + + path = prepend(path, webroot); + + char resolved_path[PATH_MAX]; + if (realpath(path, resolved_path) == NULL) + goto exit_error; + + if (strncmp(resolved_path, webroot, strlen(webroot)) != 0) { + goto exit_error; + } return path; + +exit_error: + free(path); + return NULL; } /** diff --git a/src/file_op/mime.c b/src/file_op/mime.c index 97ca821..0c12896 100644 --- a/src/file_op/mime.c +++ b/src/file_op/mime.c @@ -9,7 +9,7 @@ */ char *get_mime_type(char *file_path) { - char *ext = strchr(file_path, '.'); + char *ext = strrchr(file_path, '.'); if (ext == NULL) { diff --git a/src/gallery_p/gallery.c b/src/gallery_p/gallery.c index 1850c63..0ae6495 100644 --- a/src/gallery_p/gallery.c +++ b/src/gallery_p/gallery.c @@ -10,9 +10,9 @@ */ gallery_t *get_album_list() { - gallery_t *list = NULL, *curr = list; + gallery_t *list = NULL, *curr; - char **albums_titles_list = malloc(0); + char **albums_titles_list = NULL; ssize_t albums_am = get_dir_list(&albums_titles_list, GALLERY_ROOT); @@ -50,18 +50,18 @@ gallery_t *get_album_list() */ void free_albums_list(gallery_t *albums_list) { - gallery_t *curr_item = albums_list; - while (curr_item != NULL) + while (albums_list != NULL) { - free(curr_item->title); - for (int i = 0; i < curr_item->img_am; i++) - free_img_item(curr_item->images[i]); - free(curr_item->images); - curr_item = curr_item->next; + free(albums_list->title); + for (int i = 0; i < albums_list->img_am; i++) + free_img_item(albums_list->images[i]); + free(albums_list->images); + + gallery_t *next_item = albums_list->next; + free(albums_list); + albums_list = next_item; } - - free(albums_list); } /** @@ -154,7 +154,15 @@ int get_album_imgs(img_t **images_arr, int *size, char *title) char *gen_gallery_html() { FILE *album_template_fp = fopen("static/gallery/album.html", "r"); + if (album_template_fp == NULL) { + return "500 Internal Error\n"; + } + FILE *image_template_fp = fopen("static/gallery/image.html", "r"); + if (image_template_fp == NULL) { + fclose(album_template_fp); + return "500 Internal Error\n"; + } size_t album_file_size = get_file_size(album_template_fp) + 1; char *album_template = calloc(1, album_file_size); diff --git a/src/netw_op/request.c b/src/netw_op/request.c index 4a311f9..6f850ab 100644 --- a/src/netw_op/request.c +++ b/src/netw_op/request.c @@ -16,6 +16,12 @@ void res_404(int fd, char *path) { FILE *fp = fopen("static/404.html", "r"); + + if (fp == NULL) { + res_500(fd); + return; + } + const ssize_t fsize = 512; char *buff = malloc(fsize), *msg = malloc(fsize); @@ -102,6 +108,12 @@ int send_response(int fd, char *req_path) { char *file_path = gen_file_path(req_path); + if (file_path == NULL) { + res_404(fd, req_path); + free(file_path); + return 0; + } + struct file_s *file = get_file_info(file_path); if (file == NULL) @@ -121,6 +133,8 @@ int send_response(int fd, char *req_path) return 0; } + int return_code = 0; + char *mime_type = get_mime_type(file_path); struct header_s *header = gen_header(200, file->size, mime_type); @@ -130,15 +144,18 @@ int send_response(int fd, char *req_path) if (rv < 0) { err_msg("couldn't send header"); - return -1; + return_code = -1; + goto exit_code; } if (send_file(fd, file) < 0) { err_msg("couldn't send file"); - return -1; + return_code = -1; + goto exit_code; } +exit_code: close(fd); free(header->str); @@ -146,7 +163,8 @@ int send_response(int fd, char *req_path) free(file); free(file_path); - return 0; + + return return_code; } /** @@ -189,7 +207,7 @@ void handle_get_request(int fd, char *request) fclose(fp); template[file_size-1] = '\0'; - article_info *articles = malloc(0); + article_info *articles = NULL; int amount = list_articles(&articles); if (amount < 0) { @@ -245,7 +263,7 @@ void handle_get_request(int fd, char *request) } free(id_str); - article_info *articles = malloc(0); + article_info *articles = NULL; int amount = list_articles(&articles); if (amount < 0) { @@ -255,6 +273,7 @@ void handle_get_request(int fd, char *request) if (id > amount - 1) { + free_article_info_arr(&articles, amount); res_404(fd, path); return; } diff --git a/src/projects_p/projects.c b/src/projects_p/projects.c index 5a4ecb3..4f144ba 100644 --- a/src/projects_p/projects.c +++ b/src/projects_p/projects.c @@ -64,7 +64,7 @@ char *gen_project_html() fread(template, template_file_size, 1, template_fp); fclose(template_fp); - project_t *list = malloc(0); + project_t *list = NULL; size_t length = read_list(&list); char *content = strdup(""); diff --git a/src/utils_op/utils.c b/src/utils_op/utils.c index 32547ec..29a33df 100644 --- a/src/utils_op/utils.c +++ b/src/utils_op/utils.c @@ -17,16 +17,18 @@ void err_msg(char *msg) * @param str2 * @return char* */ -char *concat_to_front(char **str1, char *str2) +char *prepend(char *dest, const char *prefix) { - char *tmp = strdup(*str1); + size_t orig_len = strlen(dest); + size_t prefix_len = strlen(prefix); - strcpy(*str1, str2); - strcat(*str1, tmp); + dest = realloc(dest, orig_len + prefix_len + 1); + + memmove(dest + prefix_len, dest, orig_len + 1); - free(tmp); + memcpy(dest, prefix, prefix_len); - return *str1; + return dest; } /**