MINOR: rules: add a new function new_act_rule() to allocate act_rules
authorWilly Tarreau <w@1wt.eu>
Mon, 11 Oct 2021 06:49:26 +0000 (08:49 +0200)
committerWilly Tarreau <w@1wt.eu>
Tue, 12 Oct 2021 05:38:30 +0000 (07:38 +0200)
Rules are currently allocated using calloc() by their caller, which does
not make it very convenient to pass more information such as the file
name and line number.

This patch introduces new_act_rule() which performs the malloc() and
already takes in argument the ruleset (ACT_F_*), the file name and the
line number. This saves the caller from having to assing ->from, and
will allow to improve the internal storage with more info.

include/haproxy/action.h
src/action.c
src/http_rules.c
src/tcp_rules.c
src/tcpcheck.c

index 9ea5847..5d751ac 100644 (file)
@@ -111,6 +111,7 @@ static inline void release_timeout_action(struct act_rule *rule)
        release_sample_expr(rule->arg.timeout.expr);
 }
 
+struct act_rule *new_act_rule(enum act_from from, const char *file, int linenum);
 void free_act_rules(struct list *rules);
 
 #endif /* _HAPROXY_ACTION_H */
index 1644e0b..a363dea 100644 (file)
@@ -285,6 +285,21 @@ const char *action_suggest(const char *word, const struct list *keywords, const
        return best_ptr;
 }
 
+/* allocates a rule for ruleset <from> (ACT_F_*), from file name <file> and
+ * line <linenum>. <file> and <linenum> may be zero if unknown. Returns the
+ * rule, otherwise NULL in case of memory allocation error.
+ */
+struct act_rule *new_act_rule(enum act_from from, const char *file, int linenum)
+{
+       struct act_rule *rule;
+
+       rule = calloc(1, sizeof(*rule));
+       if (!rule)
+               return NULL;
+       rule->from = from;
+       return rule;
+}
+
 void free_act_rules(struct list *rules)
 {
        struct act_rule *rule, *ruleb;
index 4182958..bcff27b 100644 (file)
@@ -81,12 +81,11 @@ struct act_rule *parse_http_req_cond(const char **args, const char *file, int li
        const struct action_kw *custom = NULL;
        int cur_arg;
 
-       rule = calloc(1, sizeof(*rule));
+       rule = new_act_rule(ACT_F_HTTP_REQ, file, linenum);
        if (!rule) {
                ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
                goto out_err;
        }
-       rule->from = ACT_F_HTTP_REQ;
 
        if (((custom = action_http_req_custom(args[0])) != NULL)) {
                char *errmsg = NULL;
@@ -160,12 +159,11 @@ struct act_rule *parse_http_res_cond(const char **args, const char *file, int li
        const struct action_kw *custom = NULL;
        int cur_arg;
 
-       rule = calloc(1, sizeof(*rule));
+       rule = new_act_rule(ACT_F_HTTP_RES, file, linenum);
        if (!rule) {
                ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
                goto out_err;
        }
-       rule->from = ACT_F_HTTP_RES;
 
        if (((custom = action_http_res_custom(args[0])) != NULL)) {
                char *errmsg = NULL;
@@ -240,12 +238,11 @@ struct act_rule *parse_http_after_res_cond(const char **args, const char *file,
        const struct action_kw *custom = NULL;
        int cur_arg;
 
-       rule = calloc(1, sizeof(*rule));
+       rule = new_act_rule(ACT_F_HTTP_RES, file, linenum);
        if (!rule) {
                ha_alert("parsing [%s:%d]: out of memory.\n", file, linenum);
                goto out_err;
        }
-       rule->from = ACT_F_HTTP_RES;
 
        if (((custom = action_http_after_res_custom(args[0])) != NULL)) {
                char *errmsg = NULL;
index 9af3005..375da6b 100644 (file)
@@ -1060,7 +1060,7 @@ static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx,
                return 0;
        }
 
-       rule = calloc(1, sizeof(*rule));
+       rule = new_act_rule(ACT_F_TCP_RES_CNT, file, line);
        if (!rule) {
                memprintf(err, "parsing [%s:%d] : out of memory", file, line);
                return -1;
@@ -1076,7 +1076,6 @@ static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx,
                        where |= SMP_VAL_FE_RES_CNT;
                if (curpx->cap & PR_CAP_BE)
                        where |= SMP_VAL_BE_RES_CNT;
-               rule->from = ACT_F_TCP_RES_CNT;
                if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
                        goto error;
 
@@ -1178,7 +1177,7 @@ static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
                return 0;
        }
 
-       rule = calloc(1, sizeof(*rule));
+       rule = new_act_rule(0, file, line);
        if (!rule) {
                memprintf(err, "parsing [%s:%d] : out of memory", file, line);
                return -1;
index 6109f5a..aa6db67 100644 (file)
@@ -2336,13 +2336,12 @@ struct tcpcheck_rule *parse_tcpcheck_action(char **args, int cur_arg, struct pro
        struct tcpcheck_rule *chk = NULL;
        struct act_rule *actrule = NULL;
 
-       actrule = calloc(1, sizeof(*actrule));
+       actrule = new_act_rule(ACT_F_TCP_CHK, file, line);
        if (!actrule) {
                memprintf(errmsg, "out of memory");
                goto error;
        }
        actrule->kw = kw;
-       actrule->from = ACT_F_TCP_CHK;
 
        cur_arg++;
        if (kw->parse((const char **)args, &cur_arg, px, actrule, errmsg) == ACT_RET_PRS_ERR) {