blob: 140fbeff9c3b1e7027b2c26db29af403243e8753 [file] [log] [blame]
Willy Tarreau2ac57182012-04-19 15:24:50 +02001/*
2 * Functions used to parse typed argument lists
3 *
4 * Copyright 2012 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <sys/types.h>
14#include <sys/socket.h>
15#include <arpa/inet.h>
16
Willy Tarreaub2551052020-06-09 09:07:15 +020017#include <haproxy/arg.h>
Willy Tarreauc13ed532020-06-02 10:22:45 +020018#include <haproxy/chunk.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020019#include <haproxy/global.h>
Willy Tarreauab213a52021-07-16 10:13:00 +020020#include <haproxy/regex.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020021#include <haproxy/tools.h>
Willy Tarreau2ac57182012-04-19 15:24:50 +020022
Thierry FOURNIER49f45af2014-12-08 19:50:43 +010023const char *arg_type_names[ARGT_NBTYPES] = {
Willy Tarreau2ac57182012-04-19 15:24:50 +020024 [ARGT_STOP] = "end of arguments",
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +020025 [ARGT_SINT] = "integer",
Willy Tarreau2ac57182012-04-19 15:24:50 +020026 [ARGT_STR] = "string",
27 [ARGT_IPV4] = "IPv4 address",
28 [ARGT_MSK4] = "IPv4 mask",
29 [ARGT_IPV6] = "IPv6 address",
30 [ARGT_MSK6] = "IPv6 mask",
31 [ARGT_TIME] = "delay",
32 [ARGT_SIZE] = "size",
33 [ARGT_FE] = "frontend",
34 [ARGT_BE] = "backend",
35 [ARGT_TAB] = "table",
36 [ARGT_SRV] = "server",
37 [ARGT_USR] = "user list",
Willy Tarreau53c250e2015-01-19 18:58:20 +010038 [ARGT_MAP] = "map",
Willy Tarreau46947782015-01-19 19:00:58 +010039 [ARGT_REG] = "regex",
Willy Tarreauf7ead612015-09-21 20:57:12 +020040 [ARGT_VAR] = "variable",
Frédéric Lécaille3a463c92019-02-25 15:20:35 +010041 [ARGT_PBUF_FNUM] = "Protocol buffers field number",
Willy Tarreau2ac57182012-04-19 15:24:50 +020042 /* Unassigned types must never happen. Better crash during parsing if they do. */
43};
44
Willy Tarreau2e845be2012-10-19 19:49:09 +020045/* This dummy arg list may be used by default when no arg is found, it helps
46 * parsers by removing pointer checks.
47 */
Willy Tarreau3d241e72015-01-19 18:44:07 +010048struct arg empty_arg_list[ARGM_NBARGS] = { };
Willy Tarreau2e845be2012-10-19 19:49:09 +020049
Willy Tarreaua4312fa2013-04-02 16:34:32 +020050/* This function clones a struct arg_list template into a new one which is
51 * returned.
52 */
53struct arg_list *arg_list_clone(const struct arg_list *orig)
54{
55 struct arg_list *new;
56
57 if ((new = calloc(1, sizeof(*new))) != NULL) {
58 /* ->list will be set by the caller when inserting the element.
59 * ->arg and ->arg_pos will be set by the caller.
60 */
61 new->ctx = orig->ctx;
62 new->kw = orig->kw;
63 new->conv = orig->conv;
64 new->file = orig->file;
65 new->line = orig->line;
66 }
67 return new;
68}
69
70/* This function clones a struct <arg_list> template into a new one which is
71 * set to point to arg <arg> at pos <pos>, and which is returned if the caller
72 * wants to apply further changes.
73 */
74struct arg_list *arg_list_add(struct arg_list *orig, struct arg *arg, int pos)
75{
76 struct arg_list *new;
77
78 new = arg_list_clone(orig);
Willy Tarreaua9e2e4b2017-04-12 22:28:52 +020079 if (new) {
80 new->arg = arg;
81 new->arg_pos = pos;
Willy Tarreau2b718102021-04-21 07:32:39 +020082 LIST_APPEND(&orig->list, &new->list);
Willy Tarreaua9e2e4b2017-04-12 22:28:52 +020083 }
Willy Tarreaua4312fa2013-04-02 16:34:32 +020084 return new;
85}
86
Willy Tarreau80b53ff2020-02-14 08:40:37 +010087/* This function builds an argument list from a config line, and stops at the
88 * first non-matching character, which is pointed to in <end_ptr>. A valid arg
89 * list starts with an opening parenthesis '(', contains a number of comma-
90 * delimited words, and ends with the closing parenthesis ')'. An empty list
91 * (with or without the parenthesis) will lead to a valid empty argument if the
92 * keyword has a mandatory one. The function returns the number of arguments
93 * emitted, or <0 in case of any error. Everything needed it automatically
94 * allocated. A pointer to an error message might be returned in err_msg if not
95 * NULL, in which case it would be allocated and the caller will have to check
96 * it and free it. The output arg list is returned in argp which must be valid.
97 * The returned array is always terminated by an arg of type ARGT_STOP (0),
98 * unless the mask indicates that no argument is supported. Unresolved arguments
99 * are appended to arg list <al>, which also serves as a template to create new
100 * entries. The mask is composed of a number of mandatory arguments in its lower
101 * ARGM_BITS bits, and a concatenation of each argument type in each subsequent
102 * ARGT_BITS-bit sblock. If <err_msg> is not NULL, it must point to a freeable
103 * or NULL pointer. The caller is expected to restart the parsing from the new
104 * pointer set in <end_ptr>, which is the first character considered as not
105 * being part of the arg list. The input string ends on the first between <len>
106 * characters (when len is positive) or the first NUL character. Placing -1 in
107 * <len> will make it virtually unbounded (~2GB long strings).
Willy Tarreau2ac57182012-04-19 15:24:50 +0200108 */
David Carlier15073a32016-03-15 19:00:35 +0000109int make_arg_list(const char *in, int len, uint64_t mask, struct arg **argp,
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100110 char **err_msg, const char **end_ptr, int *err_arg,
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200111 struct arg_list *al)
Willy Tarreau2ac57182012-04-19 15:24:50 +0200112{
113 int nbarg;
114 int pos;
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200115 struct arg *arg;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200116 const char *beg;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200117 const char *ptr_err = NULL;
118 int min_arg;
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100119 int empty;
Willy Tarreau0622f022017-04-12 22:32:04 +0200120 struct arg_list *new_al = al;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200121
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200122 *argp = NULL;
123
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100124 empty = 0;
125 if (!len || *in != '(') {
126 /* it's already not for us, stop here */
127 empty = 1;
128 len = 0;
129 } else {
130 /* skip opening parenthesis */
131 len--;
132 in++;
133 }
134
Willy Tarreau3d241e72015-01-19 18:44:07 +0100135 min_arg = mask & ARGM_MASK;
136 mask >>= ARGM_BITS;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200137
138 pos = 0;
Willy Tarreau3d241e72015-01-19 18:44:07 +0100139 /* find between 0 and NBARGS the max number of args supported by the mask */
140 for (nbarg = 0; nbarg < ARGM_NBARGS && ((mask >> (nbarg * ARGT_BITS)) & ARGT_MASK); nbarg++);
Willy Tarreau2ac57182012-04-19 15:24:50 +0200141
142 if (!nbarg)
143 goto end_parse;
144
145 /* Note: an empty input string contains an empty argument if this argument
146 * is marked mandatory. Otherwise we can ignore it.
147 */
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100148 if (empty && !min_arg)
Willy Tarreau2ac57182012-04-19 15:24:50 +0200149 goto end_parse;
150
Tim Duesterhuse52b6e52020-09-12 20:26:43 +0200151 arg = *argp = calloc(nbarg + 1, sizeof(**argp));
Willy Tarreau2ac57182012-04-19 15:24:50 +0200152
Remi Tricot-Le Breton17acbab2021-05-19 12:00:54 +0200153 if (!arg)
154 goto alloc_err;
155
Willy Tarreau2ac57182012-04-19 15:24:50 +0200156 /* Note: empty arguments after a comma always exist. */
157 while (pos < nbarg) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200158 unsigned int uint;
Willy Tarreauef21fac2020-02-14 13:37:20 +0100159 int squote = 0, dquote = 0;
160 char *out;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200161
Willy Tarreauef21fac2020-02-14 13:37:20 +0100162 chunk_reset(&trash);
163 out = trash.area;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200164
Willy Tarreauef21fac2020-02-14 13:37:20 +0100165 while (len && *in && trash.data < trash.size - 1) {
166 if (*in == '"' && !squote) { /* double quote outside single quotes */
167 if (dquote)
168 dquote = 0;
169 else
170 dquote = 1;
171 in++; len--;
172 continue;
173 }
174 else if (*in == '\'' && !dquote) { /* single quote outside double quotes */
175 if (squote)
176 squote = 0;
177 else
178 squote = 1;
179 in++; len--;
180 continue;
181 }
182 else if (*in == '\\' && !squote && len != 1) {
183 /* '\', ', ' ', '"' support being escaped by '\' */
184 if (len == 1 || in[1] == 0)
185 goto unquote_err;
186
187 if (in[1] == '\\' || in[1] == ' ' || in[1] == '"' || in[1] == '\'') {
188 in++; len--;
189 *out++ = *in;
190 }
191 else if (in[1] == 'r') {
192 in++; len--;
193 *out++ = '\r';
194 }
195 else if (in[1] == 'n') {
196 in++; len--;
197 *out++ = '\n';
198 }
199 else if (in[1] == 't') {
200 in++; len--;
201 *out++ = '\t';
202 }
203 else {
204 /* just a lone '\' */
205 *out++ = *in;
206 }
207 in++; len--;
208 }
209 else {
210 if (!squote && !dquote && (*in == ',' || *in == ')')) {
211 /* end of argument */
212 break;
213 }
214 /* verbatim copy */
215 *out++ = *in++;
216 len--;
217 }
218 trash.data = out - trash.area;
219 }
Willy Tarreau807aef82020-02-15 14:54:28 +0100220
Willy Tarreau9af749b2020-02-16 10:46:37 +0100221 if (len && *in && *in != ',' && *in != ')')
Willy Tarreau807aef82020-02-15 14:54:28 +0100222 goto buffer_err;
223
Willy Tarreauef21fac2020-02-14 13:37:20 +0100224 trash.area[trash.data] = 0;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200225
Willy Tarreau3d241e72015-01-19 18:44:07 +0100226 arg->type = (mask >> (pos * ARGT_BITS)) & ARGT_MASK;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200227
228 switch (arg->type) {
229 case ARGT_SINT:
Willy Tarreau338c6702020-02-14 11:34:35 +0100230 if (!trash.data) // empty number
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200231 goto empty_err;
Willy Tarreau338c6702020-02-14 11:34:35 +0100232 beg = trash.area;
233 arg->data.sint = read_int64(&beg, trash.area + trash.data);
234 if (beg < trash.area + trash.data)
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200235 goto parse_err;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200236 arg->type = ARGT_SINT;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200237 break;
238
239 case ARGT_FE:
240 case ARGT_BE:
241 case ARGT_TAB:
242 case ARGT_SRV:
243 case ARGT_USR:
Willy Tarreau46947782015-01-19 19:00:58 +0100244 case ARGT_REG:
Willy Tarreau496aa012012-06-01 10:38:29 +0200245 /* These argument types need to be stored as strings during
246 * parsing then resolved later.
247 */
248 arg->unresolved = 1;
Willy Tarreau0622f022017-04-12 22:32:04 +0200249 new_al = arg_list_add(al, arg, pos);
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200250
Willy Tarreau496aa012012-06-01 10:38:29 +0200251 /* fall through */
Willy Tarreau2ac57182012-04-19 15:24:50 +0200252 case ARGT_STR:
253 /* all types that must be resolved are stored as strings
254 * during the parsing. The caller must at one point resolve
255 * them and free the string.
256 */
Willy Tarreau338c6702020-02-14 11:34:35 +0100257 arg->data.str.area = my_strndup(trash.area, trash.data);
258 arg->data.str.data = trash.data;
259 arg->data.str.size = trash.data + 1;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200260 break;
261
262 case ARGT_IPV4:
Willy Tarreau338c6702020-02-14 11:34:35 +0100263 if (!trash.data) // empty address
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200264 goto empty_err;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200265
Willy Tarreau338c6702020-02-14 11:34:35 +0100266 if (inet_pton(AF_INET, trash.area, &arg->data.ipv4) <= 0)
Willy Tarreau2ac57182012-04-19 15:24:50 +0200267 goto parse_err;
268 break;
269
270 case ARGT_MSK4:
Willy Tarreau338c6702020-02-14 11:34:35 +0100271 if (!trash.data) // empty mask
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200272 goto empty_err;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200273
Willy Tarreau338c6702020-02-14 11:34:35 +0100274 if (!str2mask(trash.area, &arg->data.ipv4))
Willy Tarreau2ac57182012-04-19 15:24:50 +0200275 goto parse_err;
276
277 arg->type = ARGT_IPV4;
278 break;
279
280 case ARGT_IPV6:
Willy Tarreau338c6702020-02-14 11:34:35 +0100281 if (!trash.data) // empty address
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200282 goto empty_err;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200283
Willy Tarreau338c6702020-02-14 11:34:35 +0100284 if (inet_pton(AF_INET6, trash.area, &arg->data.ipv6) <= 0)
Willy Tarreau2ac57182012-04-19 15:24:50 +0200285 goto parse_err;
286 break;
287
Tim Duesterhusb814da62018-01-25 16:24:50 +0100288 case ARGT_MSK6:
Willy Tarreau338c6702020-02-14 11:34:35 +0100289 if (!trash.data) // empty mask
Tim Duesterhusb814da62018-01-25 16:24:50 +0100290 goto empty_err;
291
Willy Tarreau338c6702020-02-14 11:34:35 +0100292 if (!str2mask6(trash.area, &arg->data.ipv6))
Tim Duesterhusb814da62018-01-25 16:24:50 +0100293 goto parse_err;
294
295 arg->type = ARGT_IPV6;
296 break;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200297
298 case ARGT_TIME:
Willy Tarreau338c6702020-02-14 11:34:35 +0100299 if (!trash.data) // empty time
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200300 goto empty_err;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200301
Willy Tarreau338c6702020-02-14 11:34:35 +0100302 ptr_err = parse_time_err(trash.area, &uint, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +0200303 if (ptr_err) {
304 if (ptr_err == PARSE_TIME_OVER || ptr_err == PARSE_TIME_UNDER)
Willy Tarreau338c6702020-02-14 11:34:35 +0100305 ptr_err = trash.area;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200306 goto parse_err;
Willy Tarreau9faebe32019-06-07 19:00:37 +0200307 }
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200308 arg->data.sint = uint;
309 arg->type = ARGT_SINT;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200310 break;
311
312 case ARGT_SIZE:
Willy Tarreau338c6702020-02-14 11:34:35 +0100313 if (!trash.data) // empty size
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200314 goto empty_err;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200315
Willy Tarreau338c6702020-02-14 11:34:35 +0100316 ptr_err = parse_size_err(trash.area, &uint);
Willy Tarreau2ac57182012-04-19 15:24:50 +0200317 if (ptr_err)
318 goto parse_err;
319
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200320 arg->data.sint = uint;
321 arg->type = ARGT_SINT;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200322 break;
323
Frédéric Lécaille3a463c92019-02-25 15:20:35 +0100324 case ARGT_PBUF_FNUM:
Willy Tarreau338c6702020-02-14 11:34:35 +0100325 if (!trash.data)
Frédéric Lécaille756d97f2019-03-04 19:03:48 +0100326 goto empty_err;
327
Willy Tarreau338c6702020-02-14 11:34:35 +0100328 if (!parse_dotted_uints(trash.area, &arg->data.fid.ids, &arg->data.fid.sz))
Frédéric Lécaille3a463c92019-02-25 15:20:35 +0100329 goto parse_err;
330
331 break;
332
Willy Tarreau2ac57182012-04-19 15:24:50 +0200333 /* FIXME: other types need to be implemented here */
334 default:
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200335 goto not_impl;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200336 }
337
338 pos++;
339 arg++;
340
341 /* don't go back to parsing if we reached end */
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100342 if (!len || !*in || *in == ')' || pos >= nbarg)
Willy Tarreau2ac57182012-04-19 15:24:50 +0200343 break;
344
345 /* skip comma */
346 in++; len--;
347 }
348
349 end_parse:
Willy Tarreau2ac57182012-04-19 15:24:50 +0200350 if (pos < min_arg) {
351 /* not enough arguments */
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200352 memprintf(err_msg,
Willy Tarreaub111d422013-12-13 00:38:47 +0100353 "missing arguments (got %d/%d), type '%s' expected",
Willy Tarreau3d241e72015-01-19 18:44:07 +0100354 pos, min_arg, arg_type_names[(mask >> (pos * ARGT_BITS)) & ARGT_MASK]);
Willy Tarreau2ac57182012-04-19 15:24:50 +0200355 goto err;
356 }
357
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100358 if (empty) {
359 /* nothing to do */
360 } else if (*in == ')') {
361 /* skip the expected closing parenthesis */
362 in++;
363 } else {
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200364 /* the caller is responsible for freeing this message */
Willy Tarreau338c6702020-02-14 11:34:35 +0100365 char *word = (len > 0) ? my_strndup(in, len) : (char *)in;
Willy Tarreau3e293a92021-05-06 14:50:30 +0200366
367 if (*word)
368 memprintf(err_msg, "expected ')' before '%s'", word);
369 else
370 memprintf(err_msg, "expected ')'");
371
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100372 if (len > 0)
373 free(word);
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500374 /* when we're missing a right paren, the empty part preceding
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100375 * already created an empty arg, adding one to the position, so
376 * let's fix the reporting to avoid being confusing.
377 */
378 if (pos > 1)
379 pos--;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200380 goto err;
381 }
382
383 /* note that pos might be < nbarg and this is not an error, it's up to the
384 * caller to decide what to do with optional args.
385 */
Willy Tarreau2ac57182012-04-19 15:24:50 +0200386 if (err_arg)
387 *err_arg = pos;
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100388 if (end_ptr)
389 *end_ptr = in;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200390 return pos;
391
Willy Tarreau2ac57182012-04-19 15:24:50 +0200392 err:
Willy Tarreau0622f022017-04-12 22:32:04 +0200393 if (new_al == al) {
394 /* only free the arg area if we have not queued unresolved args
395 * still pointing to it.
396 */
Willy Tarreau69a23ae2021-07-17 18:36:43 +0200397 free_args(*argp);
Willy Tarreau0622f022017-04-12 22:32:04 +0200398 free(*argp);
399 }
Willy Tarreau681e49d2013-12-06 15:30:05 +0100400 *argp = NULL;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200401 if (err_arg)
402 *err_arg = pos;
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100403 if (end_ptr)
404 *end_ptr = in;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200405 return -1;
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200406
407 empty_err:
Willy Tarreau75fd2ff2020-07-21 15:44:38 +0200408 /* If we've only got an empty set of parenthesis with nothing
409 * in between, there is no arg at all.
410 */
411 if (!pos) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100412 ha_free(argp);
Willy Tarreau75fd2ff2020-07-21 15:44:38 +0200413 }
414
Willy Tarreau77e463f2020-02-28 16:41:29 +0100415 if (pos >= min_arg)
416 goto end_parse;
417
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200418 memprintf(err_msg, "expected type '%s' at position %d, but got nothing",
Willy Tarreau3d241e72015-01-19 18:44:07 +0100419 arg_type_names[(mask >> (pos * ARGT_BITS)) & ARGT_MASK], pos + 1);
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200420 goto err;
421
422 parse_err:
Willy Tarreau338c6702020-02-14 11:34:35 +0100423 /* come here with the word attempted to parse in trash */
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200424 memprintf(err_msg, "failed to parse '%s' as type '%s' at position %d",
Willy Tarreau338c6702020-02-14 11:34:35 +0100425 trash.area, arg_type_names[(mask >> (pos * ARGT_BITS)) & ARGT_MASK], pos + 1);
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200426 goto err;
427
428 not_impl:
Willy Tarreaueb6cead2012-09-20 19:43:14 +0200429 memprintf(err_msg, "parsing for type '%s' was not implemented, please report this bug",
Willy Tarreau3d241e72015-01-19 18:44:07 +0100430 arg_type_names[(mask >> (pos * ARGT_BITS)) & ARGT_MASK]);
Willy Tarreau4e6336f2012-04-27 16:32:26 +0200431 goto err;
Willy Tarreau338c6702020-02-14 11:34:35 +0100432
433 buffer_err:
434 memprintf(err_msg, "too small buffer size to store decoded argument %d, increase bufsize ?",
435 pos + 1);
436 goto err;
Willy Tarreauef21fac2020-02-14 13:37:20 +0100437
438 unquote_err:
439 /* come here with the parsed part in <trash.area>:<trash.data> and the
440 * unparsable part in <in>.
441 */
442 trash.area[trash.data] = 0;
443 memprintf(err_msg, "failed to parse '%s' after '%s' as type '%s' at position %d",
444 in, trash.area, arg_type_names[(mask >> (pos * ARGT_BITS)) & ARGT_MASK], pos + 1);
445 goto err;
446
Remi Tricot-Le Breton17acbab2021-05-19 12:00:54 +0200447alloc_err:
448 memprintf(err_msg, "out of memory");
449 goto err;
Willy Tarreau2ac57182012-04-19 15:24:50 +0200450}
Willy Tarreauab213a52021-07-16 10:13:00 +0200451
452/* Free all args of an args array, taking care of unresolved arguments as well.
453 * It stops at the ARGT_STOP, which must be present. The array itself is not
454 * freed, it's up to the caller to do it. However it is returned, allowing to
455 * call free(free_args(argptr)). It is valid to call it with a NULL args, and
456 * nothing will be done).
457 */
458struct arg *free_args(struct arg *args)
459{
460 struct arg *arg;
461
462 for (arg = args; arg && arg->type != ARGT_STOP; arg++) {
463 if (arg->type == ARGT_STR || arg->unresolved)
464 chunk_destroy(&arg->data.str);
465 else if (arg->type == ARGT_REG)
466 regex_free(arg->data.reg);
467 else if (arg->type == ARGT_PBUF_FNUM)
468 ha_free(&arg->data.fid.ids);
469 }
470 return args;
471}