ssnail

crappy and opinionated static site generator
git clone https://git.e1e0.net/ssnail.git
Log | Files | Refs | README | LICENSE

commit 2add52650ba64c2d4d7f9ef3d2c1bf07276cea91
parent 1008efb8a6b0cfa06c7b31e0efab52b789d12f5e
Author: Paco Esteban <paco@e1e0.net>
Date:   Fri, 12 Feb 2021 15:43:43 +0100

fix str_rep leaks

Diffstat:
Mhelpers.c | 18++++++++++++------
Mhelpers.h | 2+-
Mssnail.c | 27++++++++++++++++++---------
3 files changed, 31 insertions(+), 16 deletions(-)

diff --git a/helpers.c b/helpers.c @@ -142,16 +142,16 @@ load_from_file(char **buffer, char *path) return length + 1; } -char * -str_rep(const char *s, const char *old, const char *new) +int +str_rep(char **str, const char *old, const char *new) { - char *r; + char *r, *s = *str; int i, cnt = 0; int new_len = strlen(new); int old_len = strlen(old); if (new_len == 0 || old_len == 0) - return NULL; + return -1; /* Counting the number of times old word * occur in the string @@ -166,7 +166,7 @@ str_rep(const char *s, const char *old, const char *new) /* Making new string long enough */ r = (char *)malloc(i + cnt * (new_len - old_len) + 1); - if (!r) return NULL; + if (!r) return -1; i = 0; while (*s) { @@ -191,5 +191,11 @@ str_rep(const char *s, const char *old, const char *new) } r[i] = '\0'; - return r; + + free(*str); + s = NULL; + + *str = r; + + return 0; } diff --git a/helpers.h b/helpers.h @@ -25,10 +25,10 @@ const struct ssnail_error *copy_file(const char *, const char *, int); int load_from_file(char **, char *); +int str_rep(char **, const char *, const char *); const char *get_filename_ext(const char *); char *build_full_path(char *, char *); -char *str_rep(const char *, const char *, const char *); #endif /* HELPERS_H */ diff --git a/ssnail.c b/ssnail.c @@ -334,6 +334,7 @@ write_html(struct article *ap, char *head_tpl, char *foot_tpl) const struct ssnail_error *error = NULL; FILE *fout; char *header = NULL, *footer = NULL; + int c = 0; if (ap->title == NULL || ap->author == NULL || ap->date == NULL) { error = ssnail_error_msg(2, "metadata"); @@ -344,14 +345,22 @@ write_html(struct article *ap, char *head_tpl, char *foot_tpl) error = ssnail_error_from_errno("load header"); goto out; } - header = str_rep(header, "$title$", ap->title); - header = str_rep(header, "$author$", ap->author); - header = str_rep(header, "$date$", ap->date); + c += str_rep(&header, "$title$", ap->title); + c += str_rep(&header, "$author$", ap->author); + c += str_rep(&header, "$date$", ap->date); + if (c != 0) { + error = ssnail_error_from_errno("str_rep"); + goto out; + } if (load_from_file(&footer, foot_tpl) == 0) { error = ssnail_error_from_errno("load footer"); goto out; } - footer = str_rep(footer, "$title$", ap->title); + c = str_rep(&footer, "$title$", ap->title); + if (c != 0) { + error = ssnail_error_from_errno("str_rep"); + goto out; + } if ((fout = fopen(ap->dst_path, "w")) == NULL) { error = ssnail_error_from_errno("openw dst_path"); @@ -383,6 +392,7 @@ populate_article_entry(char *src_path, char *dst_path) { struct article *ap = init_article(); struct stat st; + int c = 0; if (ap == NULL) return NULL; @@ -391,13 +401,12 @@ populate_article_entry(char *src_path, char *dst_path) if (ap->src_path == NULL) goto error; - char *fbuf = NULL; - fbuf = str_rep(dst_path, ".md", ".html"); - - ap->dst_path = strndup(fbuf, PATH_MAX); - free(fbuf); + ap->dst_path = strndup(dst_path, PATH_MAX); if (ap->dst_path == NULL) goto error; + c = str_rep(&(ap->dst_path), ".md", ".html"); + if (c == -1) + goto error; if (stat(ap->src_path, &st) == -1) goto error;