BUG/MINOR: hlua: enforce proper running context for register_x functions
register_{init, converters, fetches, action, service, cli, filter} are
meant to run exclusively from body context according to the
documentation (unlike register_task which is designed to work from both
init and runtime contexts)
A quick code inspection confirms that only register_task implements
the required precautions to make it safe out of init context.
Trying to use those register_* functions from a runtime lua task will
lead to a program crash since they all assume that they are running from
the main lua context and with no concurrent runs:
core.register_task(function()
core.register_init(function()
end)
end)
When loaded from the config, the above example would segfault.
To prevent this undefined behavior, we now report an explicit error if
the user tries to use such functions outside of init/body context.
This should be backported in every stable versions.
[prior to 2.5 lua filter API did not exist, the hlua_register_filter()
part should be skipped]
diff --git a/src/hlua.c b/src/hlua.c
index 2ea15b9..2ad52d1 100644
--- a/src/hlua.c
+++ b/src/hlua.c
@@ -8607,6 +8607,11 @@
MAY_LJMP(check_args(L, 1, "register_init"));
+ if (hlua_gethlua(L)) {
+ /* runtime processing */
+ WILL_LJMP(luaL_error(L, "register_init: not available outside of body context"));
+ }
+
ref = MAY_LJMP(hlua_checkfunction(L, 1));
init = calloc(1, sizeof(*init));
@@ -8974,6 +8979,11 @@
MAY_LJMP(check_args(L, 2, "register_converters"));
+ if (hlua_gethlua(L)) {
+ /* runtime processing */
+ WILL_LJMP(luaL_error(L, "register_converters: not available outside of body context"));
+ }
+
/* First argument : converter name. */
name = MAY_LJMP(luaL_checkstring(L, 1));
@@ -9053,6 +9063,11 @@
MAY_LJMP(check_args(L, 2, "register_fetches"));
+ if (hlua_gethlua(L)) {
+ /* runtime processing */
+ WILL_LJMP(luaL_error(L, "register_fetches: not available outside of body context"));
+ }
+
/* First argument : sample-fetch name. */
name = MAY_LJMP(luaL_checkstring(L, 1));
@@ -9894,6 +9909,11 @@
if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
+ if (hlua_gethlua(L)) {
+ /* runtime processing */
+ WILL_LJMP(luaL_error(L, "register_action: not available outside of body context"));
+ }
+
/* First argument : converter name. */
name = MAY_LJMP(luaL_checkstring(L, 1));
@@ -10060,6 +10080,11 @@
MAY_LJMP(check_args(L, 3, "register_service"));
+ if (hlua_gethlua(L)) {
+ /* runtime processing */
+ WILL_LJMP(luaL_error(L, "register_service: not available outside of body context"));
+ }
+
/* First argument : converter name. */
name = MAY_LJMP(luaL_checkstring(L, 1));
@@ -10337,6 +10362,11 @@
MAY_LJMP(check_args(L, 3, "register_cli"));
+ if (hlua_gethlua(L)) {
+ /* runtime processing */
+ WILL_LJMP(luaL_error(L, "register_cli: not available outside of body context"));
+ }
+
/* First argument : an array of maximum 5 keywords. */
if (!lua_istable(L, 1))
WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
@@ -11154,6 +11184,11 @@
MAY_LJMP(check_args(L, 3, "register_filter"));
+ if (hlua_gethlua(L)) {
+ /* runtime processing */
+ WILL_LJMP(luaL_error(L, "register_filter: not available outside of body context"));
+ }
+
/* First argument : filter name. */
name = MAY_LJMP(luaL_checkstring(L, 1));