hlua_ctx_renew() is called from unsafe places where the caller doesn't
expect it to LJMP.. however hlua_ctx_renew() makes use of Lua library
function that could potentially raise errors, such as lua_newthread(),
and it does nothing to catch errors. Because of this, haproxy could
unexpectedly crash. This was discovered and reported by GH user
@JB0925 on #2745.
To fix the issue, let's simply make hlua_ctx_renew() safe by applying
the same logic implemented for hlua_ctx_init() or hlua_ctx_destroy(),
which is catching Lua errors by leveraging SET_SAFE_LJMP_PARENT() helper.
It should be backported to all stable versions.
lua_State *T;
int new_ref;
+ if (!SET_SAFE_LJMP_PARENT(lua))
+ return 0;
+
/* New Lua coroutine. */
T = lua_newthread(hlua_states[lua->state_id]);
- if (!T)
+ if (!T) {
+ RESET_SAFE_LJMP_PARENT(lua);
return 0;
+ }
/* Copy last error message. */
if (keep_msg)
lua->T = T;
lua->Tref = luaL_ref(hlua_states[lua->state_id], LUA_REGISTRYINDEX);
+ RESET_SAFE_LJMP_PARENT(lua);
+
/* Set context. */
hlua_sethlua(lua);