* @brief Loads a sandboxed library set into a fresh Lua state. */
| 23 | * @brief Loads a sandboxed library set into a fresh Lua state. |
| 24 | */ |
| 25 | static void openSafeLibs(lua_State* L) |
| 26 | { |
| 27 | static const luaL_Reg kSafeLibs[] = { |
| 28 | { "_G", luaopen_base}, |
| 29 | { "table", luaopen_table}, |
| 30 | {"string", luaopen_string}, |
| 31 | { "math", luaopen_math}, |
| 32 | { nullptr, nullptr} |
| 33 | }; |
| 34 | |
| 35 | for (const luaL_Reg* lib = kSafeLibs; lib->func; ++lib) { |
| 36 | luaL_requiref(L, lib->name, lib->func, 1); |
| 37 | lua_pop(L, 1); |
| 38 | } |
| 39 | |
| 40 | static const char* const kUnsafe[] = {"dofile", "loadfile", "load"}; |
| 41 | for (const char* name : kUnsafe) { |
| 42 | lua_pushnil(L); |
| 43 | lua_setglobal(L, name); |
| 44 | } |
| 45 | |
| 46 | lua_getglobal(L, "string"); |
| 47 | if (lua_istable(L, -1)) { |
| 48 | lua_pushnil(L); |
| 49 | lua_setfield(L, -2, "dump"); |
| 50 | } |
| 51 | lua_pop(L, 1); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @brief Constructs the script with no language loaded. |
no test coverage detected