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).
This commit is contained in:
Willy Tarreau 2021-07-16 10:13:00 +02:00
parent a87e782a2d
commit ab213a5b6f
2 changed files with 23 additions and 0 deletions

View File

@ -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 */

View File

@ -17,6 +17,7 @@
#include <haproxy/arg.h>
#include <haproxy/chunk.h>
#include <haproxy/global.h>
#include <haproxy/regex.h>
#include <haproxy/tools.h>
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;
}