slweb

Једноставни генератор статичких веб страна
git clone https://git.sr.ht/~strahinja/slweb
Дневник | Датотеке | Референце | ПРОЧИТАЈМЕ | ЛИЦЕНЦА

чување c42fa9519aca611ce56876b8b2f7d4439a4844eb
родитељ 0969af1b6b3441f0a8912490739059762b873cd6
Аутор: Страхиња Радић <sr@strahinja.org>
Датум:   Tue, 23 Jul 2024 10:32:40 +0200

slweb.c: Correct several asserts, add checks for one instance of strdup

Diffstat:
Mslweb.c | 27++++++++++++++++++---------
измењених датотека: 1, додавања: 18(+), брисања: 9(-)

diff --git a/slweb.c b/slweb.c @@ -61,7 +61,7 @@ static size_t vars_count = 0; static int incdir_only_summary = 0; static int output_firstcol = 1; -static int add_css(FILE* output, int output_yaml); +static void add_css(FILE* output, int output_yaml); static int error(const int code, const char* file, const char* func, const int line, const char* fmt, ...); static u8* format_date(const u8* date_arg, const char* timestamp_format); @@ -110,11 +110,12 @@ static char* strip_ext(const char* fn, const size_t fn_size); static int url_is_local(const char* url); static int warning(const int code, const u8* fmt, ...); -static int +static void add_css(FILE* output, int output_yaml) { KeyValue* pvars = vars; - assert(vars != NULL); + if (!vars) + return; while (pvars < vars + vars_count) { assert(pvars->key != NULL); @@ -125,7 +126,6 @@ add_css(FILE* output, int output_yaml) output_yaml); pvars++; } - return 0; } static int @@ -517,7 +517,9 @@ get_value(KeyValue* list, const size_t list_count, const u8* key, KeyValue** cursor) { KeyValue* plist = list; - assert((list != NULL) && (key != NULL)); + if (!list) + return NULL; + assert(key != NULL); while (plist < list + list_count) { if (!strcmp((char*)plist->key, (char*)key)) @@ -836,6 +838,7 @@ print_meta_var(FILE* output, u8** tsv_header, u8** tsv_register) { u8* pvalue; u8* var_name = NULL; + u8* value = NULL; size_t value_len; UNUSED(tsv_header); assert((output != NULL) && (tsv_header != NULL) @@ -846,9 +849,16 @@ print_meta_var(FILE* output, u8** tsv_header, u8** tsv_register) value_len = strlen((char*)pvalue); if (*pvalue == '%' && *(pvalue + value_len - 1) == '%') { - var_name = (u8*)strdup((char*)pvalue + 1); + errno = 0; + var_name = (u8*)strdup((char*)pvalue + 1); + if (!var_name) + { + perror(PROGRAMNAME ": strdup"); + exit(error(errno, __FILE__, __func__, __LINE__, + "Allocation failed")); + } *(var_name + value_len - 2) = 0; - u8* value = get_value(vars, vars_count, var_name, NULL); + value = get_value(vars, vars_count, var_name, NULL); if (value) print_output(output, "<meta name=\"%s\" content=\"%s\">\n", @@ -1432,8 +1442,7 @@ process_incdir_subdir(FILE* output, const char* subdirname, int result = 0; UNUSED(link_prefix); - assert((output != NULL) && (subdirname != NULL) - && (link_prefix != NULL)); + assert((output != NULL) && (subdirname != NULL)); if (list_only) ; /*print_output(output, "<ul>\n");*/