wireplumber/modules/module-lua-scripting
Demi Marie Obenour f3625bee61 lua: fix SPA POD array and choice builders
These builders had many bugs:

1. They would longjmp() across the destructor of a g_autoptr() if a Lua
   error was thrown.  This will leak the memory in the g_autoptr()
   unless Lua is compiled with C++ exceptions.
2. They depended on the iteration order of numerical keys in Lua tables.
   Lua explicitly does not specify this order.
3. They would produce nonsensical SPA POD array or choice types with
   strings or bytes as values.  These would cause undefined behavior if
   manipulated by naive C code, or assertion failures if
   spa_pod_is_array() and spa_pod_is_choice() are modified to check that
   the contents of arrays and choices have sensible types.
4. They silently accepted extra arguments, potentially causing confusion
   and making it harder to extend the functions in a
   backwards-compatible way.

Solve the first problem by calling functions that can raise a Lua error
in a protected environment (with lua_pcall).  If there is a Lua error,
rethrow it after the g_autoptr() destructor has run.

Solve the second problem by first obtaining the number of keys in the
table and then iterating over the keys that are expected to be present.
If any of the keys are not contiguious integers starting at 1, the range
[1..number of keys] will include a number that is not a table key.  This
will result in lua_rawgeti pushing a nil onto the Lua stack.  An
explicit check throws a useful error in this case.

Solve the third problem by explicitly checking that the type is
reasonable before building an array or choice.  If it is wrong,
a Lua error is thrown.

Solve the fourth problem by using luaL_checktype (L, 2, LUA_TNONE) to
check that no unwanted values were passed.  The C function called with
lua_pcall is passed every argument passed by Lua, followed by a light
userdata that stores a context pointer.  After the light userdata is
popped from the Lua stack, the Lua stack is identical to what Lua
created when it called the outer C function, so the type-checking
functions in the auxillary library can be used to enforce that only the
correct number and type of arguments were passed.
2025-07-24 12:36:29 +03:00
..
api lua: fix SPA POD array and choice builders 2025-07-24 12:36:29 +03:00
wplua lua: Push "_new", not '_' and then "new". 2025-07-24 12:36:29 +03:00
meson.build wplua: move under modules/module-lua-scripting 2022-03-24 09:47:59 +02:00
module.c base-dirs: add XDG_CONFIG/DATA_DIRS and CONFIGURATION & DATA groups 2024-03-04 07:07:56 +00:00
script.c docs: fix various codebase typos Found via codespell -q 3 -S "*.po,./po/*,NEWS.rst" -L bootup,gir,inout 2025-01-28 15:45:54 +01:00
script.h m-lua-scripting: wrap every script in a WpPlugin and expose async activation api 2022-04-23 20:14:59 +03:00