MINOR: rules: add a new function new_act_rule() to allocate act_rules

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.
This commit is contained in:
Willy Tarreau 2021-10-11 08:49:26 +02:00
parent e972c0acde
commit d535f807bb
5 changed files with 22 additions and 11 deletions

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

View 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;

View 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;

View 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;

View 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) {