From ab213a5b6fa378b7f132132c03187ac83db75cc1 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Fri, 16 Jul 2021 10:13:00 +0200 Subject: [PATCH] MINOR: arg: add a free_args() function to free an args array make_arg_list() can create an array of arguments, some of which remain to be resolved, but all users had to deal with their own roll back on error. Let's add a free_args() function to release all the array's elements and let the caller deal with the array itself (sometimes it's allocated in the stack). --- include/haproxy/arg.h | 1 + src/arg.c | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/include/haproxy/arg.h b/include/haproxy/arg.h index 1817daef7..5fe18888e 100644 --- a/include/haproxy/arg.h +++ b/include/haproxy/arg.h @@ -82,6 +82,7 @@ struct arg_list *arg_list_add(struct arg_list *orig, struct arg *arg, int pos); int make_arg_list(const char *in, int len, uint64_t mask, struct arg **argp, char **err_msg, const char **end_ptr, int *err_arg, struct arg_list *al); +struct arg *free_args(struct arg *args); #endif /* _HAPROXY_ARG_H */ diff --git a/src/arg.c b/src/arg.c index d44f268da..be27c457c 100644 --- a/src/arg.c +++ b/src/arg.c @@ -17,6 +17,7 @@ #include #include #include +#include #include const char *arg_type_names[ARGT_NBTYPES] = { @@ -446,3 +447,24 @@ alloc_err: memprintf(err_msg, "out of memory"); goto err; } + +/* Free all args of an args array, taking care of unresolved arguments as well. + * It stops at the ARGT_STOP, which must be present. The array itself is not + * freed, it's up to the caller to do it. However it is returned, allowing to + * call free(free_args(argptr)). It is valid to call it with a NULL args, and + * nothing will be done). + */ +struct arg *free_args(struct arg *args) +{ + struct arg *arg; + + for (arg = args; arg && arg->type != ARGT_STOP; arg++) { + if (arg->type == ARGT_STR || arg->unresolved) + chunk_destroy(&arg->data.str); + else if (arg->type == ARGT_REG) + regex_free(arg->data.reg); + else if (arg->type == ARGT_PBUF_FNUM) + ha_free(&arg->data.fid.ids); + } + return args; +}