Fixed memory leaks

This commit is contained in:
Dmitriy Shishkov 2021-09-14 00:31:18 +03:00
parent 4709313543
commit c818a029e4
No known key found for this signature in database
GPG Key ID: 14358F96FCDD8060
3 changed files with 9 additions and 6 deletions

View File

@ -45,6 +45,8 @@ int list_articles(article_info **articles)
else
(*articles)[articles_amount - 1].title = strdup("No title");
(*articles)[articles_amount - 1].content = malloc(0);
free(tmp);
// printf("Found article \"%s\" posted on %ld\n", (*articles)[articles_amount - 1].time, (*articles)[articles_amount - 1].title);
@ -67,7 +69,7 @@ long get_article_contents(article_info *article)
name = malloc(line_length);
if (name == NULL)
{
article->content = "500";
article->content = strdup("500");
return -1;
}
@ -78,7 +80,7 @@ long get_article_contents(article_info *article)
char *path = malloc(line_length);
if (path == NULL)
{
article->content = "500";
article->content = strdup("500");
return -1;
}
@ -89,7 +91,7 @@ long get_article_contents(article_info *article)
if (file == NULL)
{
perror("Couldn't open article file");
article->content = "404";
article->content = strdup("404");
return -1;
}
@ -112,8 +114,8 @@ void free_article_info_arr(article_info **articles, int length)
{
for (int i = 0; i < length; i++)
{
free((*articles)[i].content);
free((*articles)[i].title);
free((*articles)[i].content);
}
free(*articles);

View File

@ -71,13 +71,13 @@ int gen_html_article_list(article_info *articles, int n, char **out)
snprintf(insert, line_length, "<li><a href=\"/article/%d\" >%s</a></li>\n", i, articles[i].title);
strcat(*out, insert);
free(insert);
}
*out = realloc(*out, strlen(*out) + strlen("</ul>") + 1);
strcat(*out, "</ul>");
free(insert);
return 0;
}

View File

@ -187,6 +187,7 @@ void handle_get_request(int fd, char *request)
fread(template, file_size, 1, fp);
fclose(fp);
template[file_size-1] = '\0';
article_info *articles = malloc(0);
int amount = list_articles(&articles);