From 51aa8b2d9d3812b74fb4d15da712a31d549d928b Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Sat, 30 Sep 2023 12:55:49 +0200 Subject: Document new np_add_regex more and add error handling --- lib/utils_disk.c | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/lib/utils_disk.c b/lib/utils_disk.c index 34401e21..884f0052 100644 --- a/lib/utils_disk.c +++ b/lib/utils_disk.c @@ -41,15 +41,40 @@ np_add_name (struct name_list **list, const char *name) *list = new_entry; } -/* Initialises a new regex at the begin of list via regcomp(3) */ +/* @brief Initialises a new regex at the begin of list via regcomp(3) + * + * @details if the regex fails to compile the error code of regcomp(3) is returned + * and list is not modified, otherwise list is modified to point to the new + * element + * @param list Pointer to a linked list of regex_list elements + * @param regex the string containing the regex which should be inserted into the list + * @param clags the cflags parameter for regcomp(3) + */ int np_add_regex (struct regex_list **list, const char *regex, int cflags) { struct regex_list *new_entry = (struct regex_list *) malloc (sizeof *new_entry); - new_entry->next = *list; - *list = new_entry; - return regcomp(&new_entry->regex, regex, cflags); + if (new_entry == NULL) { + die(STATE_UNKNOWN, _("Cannot allocate memory: %s"), + strerror(errno)); + } + + int regcomp_result = regcomp(&new_entry->regex, regex, cflags); + + if (!regcomp_result) { + // regcomp succeded + new_entry->next = *list; + *list = new_entry; + + return 0; + } else { + // regcomp failed + free(new_entry); + + return regcomp_result; + } + } /* Initialises a new parameter at the end of list */ -- cgit v1.2.3-74-g34f1