BUG/MINOR: lua: Properly catch alloc errors when parsing lua filter directives
When a lua filter declaration is parsed, some allocation errors were not
properly handled. In addition, we must be sure the filter identifier is defined
in lua to duplicate it when the filter configuration is filled.
This patch fix a defect reported in the issue #1347. It only concerns
2.5-dev. No backport needed.
diff --git a/src/hlua.c b/src/hlua.c
index 717380c..7e68d0b 100644
--- a/src/hlua.c
+++ b/src/hlua.c
@@ -10350,10 +10350,8 @@
/* Initialize the filter ops with default callbacks */
hlua_flt_ops = calloc(1, sizeof(*hlua_flt_ops));
- if (!hlua_flt_ops) {
- memprintf(err, "Lua filter '%s' : Lua out of memory error", reg_flt->name);
- return -1;
- }
+ if (!hlua_flt_ops)
+ goto error;
hlua_flt_ops->init = hlua_filter_init;
hlua_flt_ops->deinit = hlua_filter_deinit;
if (state_id) {
@@ -10396,21 +10394,28 @@
/* Create the filter config */
conf = calloc(1, sizeof(*conf));
- if (!conf) {
- memprintf(err, "Lua filter '%s' : Lua out of memory error", reg_flt->name);
+ if (!conf)
goto error;
- }
conf->reg = reg_flt;
/* duplicate args */
for (pos = 0; *args[*cur_arg + 1 + pos]; pos++);
conf->args = calloc(pos + 1, sizeof(*conf->args));
- for (pos = 0; *args[*cur_arg + 1 + pos]; pos++)
+ if (!conf->args)
+ goto error;
+ for (pos = 0; *args[*cur_arg + 1 + pos]; pos++) {
conf->args[pos] = strdup(args[*cur_arg + 1 + pos]);
+ if (!conf->args[pos])
+ goto error;
+ }
conf->args[pos] = NULL;
*cur_arg += pos + 1;
- fconf->id = strdup(flt_id);
+ if (flt_id) {
+ fconf->id = strdup(flt_id);
+ if (!fconf->id)
+ goto error;
+ }
fconf->flags = flt_flags;
fconf->conf = conf;
fconf->ops = hlua_flt_ops;
@@ -10419,8 +10424,15 @@
return 0;
error:
+ memprintf(err, "Lua filter '%s' : Lua out of memory error", reg_flt->name);
free(hlua_flt_ops);
+ if (conf && conf->args) {
+ for (pos = 0; conf->args[pos]; pos++)
+ free(conf->args[pos]);
+ free(conf->args);
+ }
free(conf);
+ free((char *)fconf->id);
lua_settop(L, 0);
return -1;
}