MINOR: arg: Use chunk_destroy() to release string arguments
authorChristopher Faulet <cfaulet@haproxy.com>
Fri, 7 Aug 2020 09:45:18 +0000 (11:45 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Fri, 11 Sep 2020 08:10:41 +0000 (10:10 +0200)
This way, all fields of the buffer structure are reset when a string argument
(ARGT_STR) is released.  It is also a good way to explicitly specify this kind
of argument is a chunk. So .data and .size fields must be set.

This patch may be backported to ease backports.

(cherry picked from commit 6ad7df423b71470babef545506a1af569074fc59)
Signed-off-by: Willy Tarreau <w@1wt.eu>
(cherry picked from commit 1c67ab418b2adfd99e0c9d2796af382a86ce4a0f)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>

src/acl.c
src/hlua.c
src/map.c
src/payload.c
src/proto_tcp.c
src/sample.c

index 209580a..cb2e75f 100644 (file)
--- a/src/acl.c
+++ b/src/acl.c
@@ -114,9 +114,7 @@ static struct acl_expr *prune_acl_expr(struct acl_expr *expr)
                if (arg->type == ARGT_STOP)
                        break;
                if (arg->type == ARGT_STR || arg->unresolved) {
-                       free(arg->data.str.area);
-                       arg->data.str.area = NULL;
-                       arg->data.str.data = 0;
+                       chunk_destroy(&arg->data.str);
                        unresolved |= arg->unresolved;
                        arg->unresolved = 0;
                }
index ed2955b..f907f0a 100644 (file)
@@ -1595,7 +1595,7 @@ __LJMP static int hlua_map_new(struct lua_State *L)
                lua_pushfstring(L, "'new': %s.", err);
                lua_concat(L, 2);
                free(err);
-               free(args[0].data.str.area);
+               chunk_destroy(&args[0].data.str);
                WILL_LJMP(lua_error(L));
        }
 
index 9595679..ae9dc3a 100644 (file)
--- a/src/map.c
+++ b/src/map.c
@@ -158,7 +158,7 @@ int sample_load_map(struct arg *arg, struct sample_conv *conv,
                                  arg[1].data.str.area);
                        return 0;
                }
-               free(arg[1].data.str.area);
+               chunk_destroy(&arg[1].data.str);
                if (data.type == SMP_T_IPV4) {
                        arg[1].type = ARGT_IPV4;
                        arg[1].data.ipv4 = data.u.ipv4;
@@ -169,7 +169,7 @@ int sample_load_map(struct arg *arg, struct sample_conv *conv,
        }
 
        /* replace the first argument by this definition */
-       free(arg[0].data.str.area);
+       chunk_destroy(&arg[0].data.str);
        arg[0].type = ARGT_MAP;
        arg[0].data.map = desc;
 
index ad6b6ed..9a6ce41 100644 (file)
@@ -1082,7 +1082,7 @@ int val_payload_lv(struct arg *arg, char **err_msg)
                        memprintf(err_msg, "payload offset2 is not a number");
                        return 0;
                }
-               free(arg[2].data.str.area);
+               chunk_destroy(&arg[2].data.str);
                arg[2].type = ARGT_SINT;
                arg[2].data.sint = i;
 
@@ -1284,7 +1284,7 @@ int val_distcc(struct arg *arg, char **err_msg)
        token = (arg[0].data.str.area[0] << 24) + (arg[0].data.str.area[1] << 16) +
                (arg[0].data.str.area[2] << 8) + (arg[0].data.str.area[3] << 0);
 
-       free(arg[0].data.str.area);
+       chunk_destroy(&arg[0].data.str);
        arg[0].type      = ARGT_SINT;
        arg[0].data.sint = token;
 
index 97d3abc..0f8e600 100644 (file)
@@ -1583,12 +1583,12 @@ static int val_fc_time_value(struct arg *args, char **err)
 {
        if (args[0].type == ARGT_STR) {
                if (strcmp(args[0].data.str.area, "us") == 0) {
-                       free(args[0].data.str.area);
+                       chunk_destroy(&args[0].data.str);
                        args[0].type = ARGT_SINT;
                        args[0].data.sint = TIME_UNIT_US;
                }
                else if (strcmp(args[0].data.str.area, "ms") == 0) {
-                       free(args[0].data.str.area);
+                       chunk_destroy(&args[0].data.str);
                        args[0].type = ARGT_SINT;
                        args[0].data.sint = TIME_UNIT_MS;
                }
@@ -1617,7 +1617,7 @@ static int var_fc_counter(struct arg *args, char **err)
        if (args[0].type != ARGT_STOP) {
                ha_warning("no argument supported for 'fc_*' sample expressions returning counters.\n");
                if (args[0].type == ARGT_STR)
-                       free(args[0].data.str.area);
+                       chunk_destroy(&args[0].data.str);
                args[0].type = ARGT_STOP;
        }
 
index 885428b..31b2274 100644 (file)
@@ -1180,8 +1180,7 @@ int smp_resolve_args(struct proxy *p)
                                break;
                        }
 
-                       free(arg->data.str.area);
-                       arg->data.str.area = NULL;
+                       chunk_destroy(&arg->data.str);
                        arg->unresolved = 0;
                        arg->data.srv = srv;
                        break;
@@ -1208,8 +1207,7 @@ int smp_resolve_args(struct proxy *p)
                                break;
                        }
 
-                       free(arg->data.str.area);
-                       arg->data.str.area = NULL;
+                       chunk_destroy(&arg->data.str);
                        arg->unresolved = 0;
                        arg->data.prx = px;
                        break;
@@ -1236,8 +1234,7 @@ int smp_resolve_args(struct proxy *p)
                                break;
                        }
 
-                       free(arg->data.str.area);
-                       arg->data.str.area = NULL;
+                       chunk_destroy(&arg->data.str);
                        arg->unresolved = 0;
                        arg->data.prx = px;
                        break;
@@ -1277,8 +1274,7 @@ int smp_resolve_args(struct proxy *p)
                                t->proxies_list = p;
                        }
 
-                       free(arg->data.str.area);
-                       arg->data.str.area = NULL;
+                       chunk_destroy(&arg->data.str);
                        arg->unresolved = 0;
                        arg->data.t = t;
                        break;
@@ -1307,8 +1303,7 @@ int smp_resolve_args(struct proxy *p)
                                break;
                        }
 
-                       free(arg->data.str.area);
-                       arg->data.str.area = NULL;
+                       chunk_destroy(&arg->data.str);
                        arg->unresolved = 0;
                        arg->data.usr = ul;
                        break;
@@ -1335,8 +1330,7 @@ int smp_resolve_args(struct proxy *p)
                                continue;
                        }
 
-                       free(arg->data.str.area);
-                       arg->data.str.area = NULL;
+                       chunk_destroy(&arg->data.str);
                        arg->unresolved = 0;
                        arg->data.reg = reg;
                        break;
@@ -1405,8 +1399,7 @@ static void release_sample_arg(struct arg *p)
 
        while (p->type != ARGT_STOP) {
                if (p->type == ARGT_STR || p->unresolved) {
-                       free(p->data.str.area);
-                       p->data.str.area = NULL;
+                       chunk_destroy(&p->data.str);
                        p->unresolved = 0;
                }
                else if (p->type == ARGT_REG) {
@@ -1513,7 +1506,7 @@ static int smp_check_debug(struct arg *args, struct sample_conv *conv,
                return 0;
        }
 
-       free(args[1].data.str.area);
+       chunk_destroy(&args[1].data.str);
        args[1].type = ARGT_PTR;
        args[1].data.ptr = sink;
        return 1;
@@ -1951,7 +1944,7 @@ static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv,
                return 0;
        }
 
-       free(arg->data.str.area);
+       chunk_destroy(&arg->data.str);
        arg->type = ARGT_SINT;
        arg->data.sint = type;
        return 1;
@@ -2435,7 +2428,7 @@ static int check_operator(struct arg *args, struct sample_conv *conv,
                return 0;
        }
 
-       free(args[0].data.str.area);
+       chunk_destroy(&args[0].data.str);
        args[0].type = ARGT_SINT;
        args[0].data.sint = i;
        return 1;
@@ -2926,7 +2919,7 @@ static int sample_conv_protobuf_check(struct arg *args, struct sample_conv *conv
                        return 0;
                }
 
-               free(args[1].data.str.area);
+               chunk_destroy(&args[1].data.str);
                args[1].type = ARGT_SINT;
                args[1].data.sint = pbuf_type;
        }
@@ -3019,7 +3012,7 @@ int smp_check_date_unit(struct arg *args, char **err)
                         return 0;
                 }
 
-               free(args[1].data.str.area);
+               chunk_destroy(&args[1].data.str);
                 args[1].type = ARGT_SINT;
                args[1].data.sint = unit;
         }
@@ -3208,14 +3201,14 @@ static int smp_check_const_bool(struct arg *args, char **err)
 {
        if (strcasecmp(args[0].data.str.area, "true") == 0 ||
            strcasecmp(args[0].data.str.area, "1") == 0) {
-               free(args[0].data.str.area);
+               chunk_destroy(&args[0].data.str);
                args[0].type = ARGT_SINT;
                args[0].data.sint = 1;
                return 1;
        }
        if (strcasecmp(args[0].data.str.area, "false") == 0 ||
            strcasecmp(args[0].data.str.area, "0") == 0) {
-               free(args[0].data.str.area);
+               chunk_destroy(&args[0].data.str);
                args[0].type = ARGT_SINT;
                args[0].data.sint = 0;
                return 1;
@@ -3259,7 +3252,7 @@ static int smp_check_const_bin(struct arg *args, char **err)
 
        if (!parse_binary(args[0].data.str.area, &binstr, &binstrlen, err))
                return 0;
-       free(args[0].data.str.area);
+       chunk_destroy(&args[0].data.str);
        args[0].type = ARGT_STR;
        args[0].data.str.area = binstr;
        args[0].data.str.data = binstrlen;
@@ -3282,8 +3275,7 @@ static int smp_check_const_meth(struct arg *args, char **err)
 
        meth = find_http_meth(args[0].data.str.area, args[0].data.str.data);
        if (meth != HTTP_METH_OTHER) {
-               free(args[0].data.str.area);
-
+               chunk_destroy(&args[0].data.str);
                args[0].type = ARGT_SINT;
                args[0].data.sint = meth;
        } else {