Fixed unfreed memory and files handling errors

This commit is contained in:
2025-05-30 22:35:11 +03:00
parent df3502ad40
commit d7fe6cd476
9 changed files with 78 additions and 31 deletions

View File

@ -8,7 +8,7 @@
#include <dirent.h> #include <dirent.h>
void err_msg(char *msg); 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 *get_status_message(int status_code);
char *to_lower(char *str); char *to_lower(char *str);
char *trim(char *str); char *trim(char *str);

View File

@ -45,7 +45,7 @@ int list_articles(article_info **articles)
else else
(*articles)[articles_amount - 1].title = strdup("No title"); (*articles)[articles_amount - 1].title = strdup("No title");
(*articles)[articles_amount - 1].content = malloc(0); (*articles)[articles_amount - 1].content = NULL;
free(tmp); free(tmp);
@ -124,7 +124,7 @@ void free_article_info_arr(article_info **articles, int length)
// /* Only for testing purposes */ // /* Only for testing purposes */
// int main() // int main()
// { // {
// article_info *articles = malloc(0); // article_info *articles = NULL;
// int n = list_articles(&articles); // int n = list_articles(&articles);

View File

@ -84,7 +84,7 @@ int gen_html_article_list(article_info *articles, int n, char **out)
// /* Only for testing purposes */ // /* Only for testing purposes */
// int main () // int main ()
// { // {
// article_info *articles = malloc(0); // article_info *articles = NULL;
// int n = list_articles(&articles); // int n = list_articles(&articles);

View File

@ -21,12 +21,30 @@ char *gen_file_path(char *req_path)
path = strcat(path, "index.html"); 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); if (PATH_MAX < strlen(webroot) + strlen("/static"))
path = concat_to_front(&path, webroot); 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; return path;
exit_error:
free(path);
return NULL;
} }
/** /**

View File

@ -9,7 +9,7 @@
*/ */
char *get_mime_type(char *file_path) char *get_mime_type(char *file_path)
{ {
char *ext = strchr(file_path, '.'); char *ext = strrchr(file_path, '.');
if (ext == NULL) if (ext == NULL)
{ {

View File

@ -10,9 +10,9 @@
*/ */
gallery_t *get_album_list() 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); 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) 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); free(albums_list->title);
for (int i = 0; i < curr_item->img_am; i++) for (int i = 0; i < albums_list->img_am; i++)
free_img_item(curr_item->images[i]); free_img_item(albums_list->images[i]);
free(curr_item->images); free(albums_list->images);
curr_item = curr_item->next;
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() char *gen_gallery_html()
{ {
FILE *album_template_fp = fopen("static/gallery/album.html", "r"); 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"); 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; size_t album_file_size = get_file_size(album_template_fp) + 1;
char *album_template = calloc(1, album_file_size); char *album_template = calloc(1, album_file_size);

View File

@ -16,6 +16,12 @@
void res_404(int fd, char *path) void res_404(int fd, char *path)
{ {
FILE *fp = fopen("static/404.html", "r"); FILE *fp = fopen("static/404.html", "r");
if (fp == NULL) {
res_500(fd);
return;
}
const ssize_t fsize = 512; const ssize_t fsize = 512;
char *buff = malloc(fsize), *msg = malloc(fsize); 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); 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); struct file_s *file = get_file_info(file_path);
if (file == NULL) if (file == NULL)
@ -121,6 +133,8 @@ int send_response(int fd, char *req_path)
return 0; return 0;
} }
int return_code = 0;
char *mime_type = get_mime_type(file_path); char *mime_type = get_mime_type(file_path);
struct header_s *header = gen_header(200, file->size, mime_type); 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) if (rv < 0)
{ {
err_msg("couldn't send header"); err_msg("couldn't send header");
return -1; return_code = -1;
goto exit_code;
} }
if (send_file(fd, file) < 0) if (send_file(fd, file) < 0)
{ {
err_msg("couldn't send file"); err_msg("couldn't send file");
return -1; return_code = -1;
goto exit_code;
} }
exit_code:
close(fd); close(fd);
free(header->str); free(header->str);
@ -146,7 +163,8 @@ int send_response(int fd, char *req_path)
free(file); free(file);
free(file_path); free(file_path);
return 0;
return return_code;
} }
/** /**
@ -189,7 +207,7 @@ void handle_get_request(int fd, char *request)
fclose(fp); fclose(fp);
template[file_size-1] = '\0'; template[file_size-1] = '\0';
article_info *articles = malloc(0); article_info *articles = NULL;
int amount = list_articles(&articles); int amount = list_articles(&articles);
if (amount < 0) if (amount < 0)
{ {
@ -245,7 +263,7 @@ void handle_get_request(int fd, char *request)
} }
free(id_str); free(id_str);
article_info *articles = malloc(0); article_info *articles = NULL;
int amount = list_articles(&articles); int amount = list_articles(&articles);
if (amount < 0) if (amount < 0)
{ {
@ -255,6 +273,7 @@ void handle_get_request(int fd, char *request)
if (id > amount - 1) if (id > amount - 1)
{ {
free_article_info_arr(&articles, amount);
res_404(fd, path); res_404(fd, path);
return; return;
} }

View File

@ -64,7 +64,7 @@ char *gen_project_html()
fread(template, template_file_size, 1, template_fp); fread(template, template_file_size, 1, template_fp);
fclose(template_fp); fclose(template_fp);
project_t *list = malloc(0); project_t *list = NULL;
size_t length = read_list(&list); size_t length = read_list(&list);
char *content = strdup(""); char *content = strdup("");

View File

@ -17,16 +17,18 @@ void err_msg(char *msg)
* @param str2 * @param str2
* @return char* * @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); dest = realloc(dest, orig_len + prefix_len + 1);
strcat(*str1, tmp);
memmove(dest + prefix_len, dest, orig_len + 1);
free(tmp); memcpy(dest, prefix, prefix_len);
return *str1; return dest;
} }
/** /**