]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: arg: add a free_args() function to free an args array
authorWilly Tarreau <w@1wt.eu>
Fri, 16 Jul 2021 08:13:00 +0000 (10:13 +0200)
committerWilly Tarreau <w@1wt.eu>
Fri, 16 Jul 2021 17:18:41 +0000 (19:18 +0200)
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
src/arg.c

index 1817daef7ca9cd648b95dd4810db05296497b970..5fe18888ebbc5b087b0e44331b08f025b007396d 100644 (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 */
 
index d44f268da8735d88974aa70537972042c481be00..be27c457cf8b8545a6ad5bc765bafcc5c4be4cd4 100644 (file)
--- a/src/arg.c
+++ b/src/arg.c
@@ -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;
+}