slweb

Једноставни генератор статичких веб страна
Дневник | Датотеке | Референце | ПРОЧИТАЈМЕ | ЛИЦЕНЦА

чување 874b6535035a58675640c9b0293bedf0d9dfbe11
родитељ 90b6f3f49682ddc2f45d25a07d5a23f0bc727bd1
Аутор: Страхиња Радић <contact@strahinja.org>
Датум:   Sun, 28 Mar 2021 20:43:22 +0200

Propagated leak fixes from slw2gmi

Signed-off-by: Страхиња Радић <contact@strahinja.org>

Diffstat:
Mindex.html | 2+-
Mslweb.c | 208+++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------
измењених датотека: 2, додавања: 129(+), брисања: 81(-)

diff --git a/index.html b/index.html @@ -153,7 +153,7 @@ this program. If not, see &lt;<a href="https://www.gnu.org/licenses">https://www <div id="git-log"> Previous commit: -index.slw bd64c34 2021-03-23 19:11:57 +0100 (Страхиња Радић) (HEAD -> master, origin/master, origin/HEAD) +index.slw 90b6f3f 2021-03-28 17:05:23 +0200 (Страхиња Радић) (HEAD -> master, origin/master, origin/HEAD) </div><!--git-log--> diff --git a/slweb.c b/slweb.c @@ -139,6 +139,32 @@ free_keyvalue(KeyValue** list, size_t list_count) } int +slweb_cleanup() +{ + if (basedir) + free(basedir); + free(input_dirname); + while (inline_footnote_count--) + free(inline_footnotes[inline_footnote_count]); + if (inline_footnotes) + free(inline_footnotes); + free_keyvalue(&footnotes, footnote_count); + free_keyvalue(&links, links_count); + free_keyvalue(&macros, macros_count); + free_keyvalue(&vars, vars_count); + if (footnotes) + free(footnotes); + if (links) + free(links); + if (macros) + free(macros); + if (vars) + free(vars); + + return 0; +} + +int slweb_parse(uint8_t* buffer, FILE* output, BOOL body_only, BOOL read_yaml_macros_and_links); @@ -875,24 +901,22 @@ process_include(uint8_t* token, FILE* output, BOOL read_yaml_macros_and_links) read_file_into_buffer(&buffer, &buffer_size, input_filename, &input_dirname, &input); + free_keyvalue(&links, links_count); free(links); - CALLOC(links, KeyValue, 1) - links->key = NULL; - links->value = NULL; - links->value_size = 0; + links = NULL; links_count = 0; + free_keyvalue(&footnotes, footnote_count); free(footnotes); - CALLOC(footnotes, KeyValue, 1) - footnotes->key = NULL; - footnotes->value = NULL; - footnotes->value_size = 0; + footnotes = NULL; footnote_count = 0; current_footnote = 0; - free(inline_footnotes); - CALLOC(inline_footnotes, uint8_t*, 1) - *inline_footnotes = NULL; + while (inline_footnote_count--) + free(inline_footnotes[inline_footnote_count]); + if (inline_footnotes) + free(inline_footnotes); + inline_footnotes = NULL; inline_footnote_count = 0; current_inline_footnote = 0; @@ -904,7 +928,7 @@ process_include(uint8_t* token, FILE* output, BOOL read_yaml_macros_and_links) if (result) { - free(input_filename); + slweb_cleanup(); free(buffer); return result; } @@ -917,7 +941,7 @@ process_include(uint8_t* token, FILE* output, BOOL read_yaml_macros_and_links) state = saved_state; - free(input_filename); + slweb_cleanup(); free(buffer); exit(result); } @@ -1096,24 +1120,22 @@ process_incdir_subdir(const char* subdirname, const uint8_t* link_prefix, read_file_into_buffer(&buffer, &buffer_size, input_filename, &input_dirname, &input); + free_keyvalue(&links, links_count); free(links); - CALLOC(links, KeyValue, 1) - links->key = NULL; - links->value = NULL; - links->value_size = 0; + links = NULL; links_count = 0; + free_keyvalue(&footnotes, footnote_count); free(footnotes); - CALLOC(footnotes, KeyValue, 1) - footnotes->key = NULL; - footnotes->value = NULL; - footnotes->value_size = 0; + footnotes = NULL; footnote_count = 0; current_footnote = 0; - free(inline_footnotes); - CALLOC(inline_footnotes, uint8_t*, 1) - *inline_footnotes = NULL; + while (inline_footnote_count--) + free(inline_footnotes[inline_footnote_count]); + if (inline_footnotes) + free(inline_footnotes); + inline_footnotes = NULL; inline_footnote_count = 0; current_inline_footnote = 0; @@ -1125,9 +1147,8 @@ process_incdir_subdir(const char* subdirname, const uint8_t* link_prefix, if (result) { + slweb_cleanup(); free(buffer); - /*free(link);*/ - /*free(basename);*/ free(abs_subdirname); while (names_total--) free(namelist[names_total]); @@ -1145,9 +1166,8 @@ process_incdir_subdir(const char* subdirname, const uint8_t* link_prefix, state = saved_state; fflush(output); + slweb_cleanup(); free(buffer); - /*free(link);*/ - /*free(basename);*/ free(abs_subdirname); while (names_total--) free(namelist[names_total]); @@ -1157,8 +1177,6 @@ process_incdir_subdir(const char* subdirname, const uint8_t* link_prefix, else exit(error(1, (uint8_t*)"Fork failed")); - /*free(link);*/ - /*free(basename);*/ pnamelist++; names_output++; } @@ -1371,7 +1389,12 @@ process_macro(uint8_t* token, FILE* output, BOOL read_yaml_macros_and_links, { macros_count++; - if (macros_count > 1) + if (!macros) + { + CALLOC(macros, KeyValue, macros_count) + pmacros = macros; + } + else { REALLOC(macros, KeyValue, macros_count * sizeof(KeyValue)) pmacros = macros + macros_count - 1; @@ -1810,7 +1833,12 @@ process_inline_footnote(uint8_t* token, BOOL read_yaml_macros_and_links, inline_footnote_count++; if (inline_footnote_count == 1 && footnote_count > 0) warning(1, (uint8_t*)"Both inline and regular footnotes present"); - else if (inline_footnote_count > 1) + else if (!inline_footnotes) + { + inline_footnote_count = 1; + CALLOC(inline_footnotes, uint8_t*, inline_footnote_count) + } + else REALLOC(inline_footnotes, uint8_t*, sizeof(uint8_t*) * inline_footnote_count) CALLOC(inline_footnotes[inline_footnote_count-1], uint8_t, token_len+1) @@ -1840,9 +1868,14 @@ process_footnote(uint8_t* token, BOOL footnote_definition, BOOL footnote_output, footnote_count++; if (footnote_count == 1 && inline_footnote_count > 0) warning(1, (uint8_t*)"Both inline and regular footnotes present"); - else if (footnote_count > 1) + else if (!footnotes) + { + CALLOC(footnotes, KeyValue, footnote_count) + pfootnotes = footnotes; + } + else { - REALLOC(footnotes, KeyValue, footnote_count * sizeof(KeyValue)) + REALLOCARRAY(footnotes, KeyValue, footnote_count) pfootnotes = footnotes + footnote_count - 1; } CALLOC(pfootnotes->key, uint8_t, KEYSIZE) @@ -1960,6 +1993,8 @@ begin_html_and_head(FILE* output) int add_css(FILE* output) { + if (!vars) + return -1; KeyValue* pvars = vars; while (pvars < vars + vars_count) { @@ -2146,15 +2181,6 @@ slweb_parse(uint8_t* buffer, FILE* output, BOOL body_only, if (!buffer) exit(error(1, (uint8_t*)"Empty buffer")); - if (!vars) - exit(error(EINVAL, (uint8_t*)"Invalid argument (vars)")); - - if (!links) - exit(error(EINVAL, (uint8_t*)"Invalid argument (links)")); - - if (!macros) - exit(error(EINVAL, (uint8_t*)"Invalid argument (macros)")); - title = get_value(vars, vars_count, (uint8_t*)"title", NULL); title_heading_level = get_value(vars, vars_count, (uint8_t*)"title-heading-level", NULL); @@ -2301,7 +2327,12 @@ slweb_parse(uint8_t* buffer, FILE* output, BOOL body_only, vars_count++; - if (vars_count > 1) + if (!vars) + { + CALLOC(vars, KeyValue, vars_count) + pvars = vars; + } + else { REALLOCARRAY(vars, KeyValue, vars_count) pvars = vars + vars_count - 1; @@ -2803,6 +2834,12 @@ slweb_parse(uint8_t* buffer, FILE* output, BOOL body_only, skip_eol = TRUE; if (read_yaml_macros_and_links) { + if (!macros) + { + macros_count = 1; + CALLOC(macros, KeyValue, macros_count) + pmacros = macros; + } if (pmacros->value) { size_t value_len = u8_strlen(pmacros->value); @@ -3403,7 +3440,12 @@ slweb_parse(uint8_t* buffer, FILE* output, BOOL body_only, case ':': links_count++; - if (links_count > 1) + if (!links) + { + CALLOC(links, KeyValue, links_count) + plinks = links; + } + else { REALLOCARRAY(links, KeyValue, links_count) plinks = links + links_count - 1; @@ -3661,6 +3703,12 @@ slweb_parse(uint8_t* buffer, FILE* output, BOOL body_only, && (state & ST_YAML_VAL)) { *ptoken = 0; + if (!vars) + { + vars_count = 1; + CALLOC(vars, KeyValue, vars_count) + pvars = vars; + } pvars->value_size = u8_strlen(token)+1; CALLOC(pvars->value, uint8_t, pvars->value_size) u8_strncpy(pvars->value, token, pvars->value_size-1); @@ -3693,6 +3741,12 @@ slweb_parse(uint8_t* buffer, FILE* output, BOOL body_only, skip_eol = TRUE; if (read_yaml_macros_and_links) { + if (!macros) + { + macros_count = 1; + CALLOC(macros, KeyValue, macros_count) + pmacros = macros; + } if (pmacros->value) { size_t value_len = u8_strlen(pmacros->value); @@ -3722,6 +3776,12 @@ slweb_parse(uint8_t* buffer, FILE* output, BOOL body_only, skip_eol = TRUE; if (read_yaml_macros_and_links) { + if (!footnotes) + { + footnote_count = 1; + CALLOC(footnotes, KeyValue, footnote_count) + pfootnotes = footnotes; + } if (pfootnotes->value) { size_t value_len = u8_strlen(pfootnotes->value); @@ -3750,6 +3810,12 @@ slweb_parse(uint8_t* buffer, FILE* output, BOOL body_only, { if (read_yaml_macros_and_links) { + if (!links) + { + links_count = 1; + CALLOC(links, KeyValue, links_count) + plinks = links; + } plinks->value_size = u8_strlen(token)+1; CALLOC(plinks->value, uint8_t, plinks->value_size) u8_strncpy(plinks->value, token, plinks->value_size-1); @@ -3818,6 +3884,12 @@ slweb_parse(uint8_t* buffer, FILE* output, BOOL body_only, skip_eol = TRUE; if (read_yaml_macros_and_links) { + if (!footnotes) + { + footnote_count = 1; + CALLOC(footnotes, KeyValue, footnote_count) + pfootnotes = footnotes; + } if (pfootnotes->value) { size_t value_len = u8_strlen(pfootnotes->value); @@ -4038,28 +4110,16 @@ main(int argc, char** argv) free(bufline); } - CALLOC(vars, KeyValue, 1) - vars->key = NULL; - vars->value = NULL; - vars->value_size = 0; + vars = NULL; + vars_count = 0; + macros = NULL; + macros_count = 0; + links = NULL; + links_count = 0; + footnotes = NULL; + footnote_count = 0; - CALLOC(macros, KeyValue, 1) - macros->key = NULL; - macros->value = NULL; - macros->value_size = 0; - - CALLOC(links, KeyValue, 1) - links->key = NULL; - links->value = NULL; - links->value_size = 0; - - CALLOC(footnotes, KeyValue, 1) - footnotes->key = NULL; - footnotes->value = NULL; - footnotes->value_size = 0; - - CALLOC(inline_footnotes, uint8_t*, 1) - *inline_footnotes = NULL; + inline_footnotes = NULL; inline_footnote_count = 0; /* First pass: read YAML, macros and links */ @@ -4067,6 +4127,7 @@ main(int argc, char** argv) if (result) { + slweb_cleanup(); free(buffer); return result; } @@ -4078,20 +4139,7 @@ main(int argc, char** argv) /* Second pass: parse and output */ result = slweb_parse(buffer, output, body_only, FALSE); - if (basedir) - free(basedir); - if (input_dirname) - free(input_dirname); - if (inline_footnotes) - free(inline_footnotes); - free_keyvalue(&footnotes, footnote_count); - free_keyvalue(&links, links_count); - free_keyvalue(&macros, macros_count); - free_keyvalue(&vars, vars_count); - free(footnotes); - free(links); - free(macros); - free(vars); + slweb_cleanup(); free(buffer); return result;