BUG/MINOR: hlua: Fix memory leaks on error path when parsing a lua action

hen an error occurred in action_register_lua(), the allocated hlua rule and
arguments must be released to avoid memory leaks.

This patch may be backported in all stable versions.
This commit is contained in:
Christopher Faulet 2021-04-12 14:37:32 +02:00
parent 2567f18382
commit 528526f2cc

View File

@ -7581,7 +7581,7 @@ static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, s
rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
if (!rule->arg.hlua_rule) { if (!rule->arg.hlua_rule) {
memprintf(err, "out of memory error"); memprintf(err, "out of memory error");
return ACT_RET_PRS_ERR; goto error;
} }
/* Memory for arguments. */ /* Memory for arguments. */
@ -7589,7 +7589,7 @@ static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, s
sizeof(*rule->arg.hlua_rule->args)); sizeof(*rule->arg.hlua_rule->args));
if (!rule->arg.hlua_rule->args) { if (!rule->arg.hlua_rule->args) {
memprintf(err, "out of memory error"); memprintf(err, "out of memory error");
return ACT_RET_PRS_ERR; goto error;
} }
/* Reference the Lua function and store the reference. */ /* Reference the Lua function and store the reference. */
@ -7599,12 +7599,12 @@ static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, s
for (i = 0; i < fcn->nargs; i++) { for (i = 0; i < fcn->nargs; i++) {
if (*args[*cur_arg] == '\0') { if (*args[*cur_arg] == '\0') {
memprintf(err, "expect %d arguments", fcn->nargs); memprintf(err, "expect %d arguments", fcn->nargs);
return ACT_RET_PRS_ERR; goto error;
} }
rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]);
if (!rule->arg.hlua_rule->args[i]) { if (!rule->arg.hlua_rule->args[i]) {
memprintf(err, "out of memory error"); memprintf(err, "out of memory error");
return ACT_RET_PRS_ERR; goto error;
} }
(*cur_arg)++; (*cur_arg)++;
} }
@ -7613,6 +7613,17 @@ static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, s
rule->action = ACT_CUSTOM; rule->action = ACT_CUSTOM;
rule->action_ptr = hlua_action; rule->action_ptr = hlua_action;
return ACT_RET_PRS_OK; return ACT_RET_PRS_OK;
error:
if (rule->arg.hlua_rule) {
if (rule->arg.hlua_rule->args) {
for (i = 0; i < fcn->nargs; i++)
ha_free(&rule->arg.hlua_rule->args[i]);
ha_free(&rule->arg.hlua_rule->args);
}
ha_free(&rule->arg.hlua_rule);
}
return ACT_RET_PRS_ERR;
} }
static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,