mirror of
https://gitlab.freedesktop.org/pipewire/wireplumber.git
synced 2025-12-20 15:50:03 +01:00
_wplua_pcall: avoid Lua stack overflow
C code must ensure that the Lua stack does not overflow. Ensure there are enough slots for both the error handler and for the return values.
This commit is contained in:
parent
db755a6a19
commit
2ea068de1b
1 changed files with 22 additions and 1 deletions
|
|
@ -70,9 +70,30 @@ _wplua_errhandler (lua_State *L)
|
|||
int
|
||||
_wplua_pcall (lua_State *L, int nargs, int nret)
|
||||
{
|
||||
int hpos = lua_gettop (L) - nargs;
|
||||
int slots = lua_gettop (L);
|
||||
int ret = LUA_OK;
|
||||
/* 1 stack slot needed for error handler. */
|
||||
int hpos, stack_slots = 1;
|
||||
|
||||
if (nargs < 0)
|
||||
g_error ("negative number of arguments");
|
||||
/* Need nargs + 1 stack slots for function and its arguments. */
|
||||
if (slots <= nargs)
|
||||
g_error ("not enough stack slots for arguments and function");
|
||||
if (nret != LUA_MULTRET) {
|
||||
if (nret - nargs > 1) {
|
||||
/* Need more stack slots: 1 for the error handler and (nret - (nargs + 1))
|
||||
* for the return values (after popping the function and its arguments). */
|
||||
stack_slots = nret - nargs;
|
||||
} else if (nret < 0)
|
||||
g_error ("negative number of return values");
|
||||
}
|
||||
if (!lua_checkstack (L, stack_slots)) {
|
||||
wp_critical ("_wplua_pcall: cannot grow Lua stack");
|
||||
return LUA_ERRMEM;
|
||||
}
|
||||
|
||||
hpos = slots - nargs;
|
||||
lua_pushcfunction (L, _wplua_errhandler);
|
||||
lua_insert (L, hpos);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue