MCPcopy Create free account
hub / github.com/Serial-Studio/Serial-Studio / openSafeLibs

Function openSafeLibs

app/src/MQTT/PublisherScript.cpp:25–52  ·  view source on GitHub ↗

* @brief Loads a sandboxed library set into a fresh Lua state. */

Source from the content-addressed store, hash-verified

23 * @brief Loads a sandboxed library set into a fresh Lua state.
24 */
25static 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.

Callers 1

compileMethod · 0.70

Calls 5

luaL_requirefFunction · 0.85
lua_pushnilFunction · 0.85
lua_setglobalFunction · 0.85
lua_getglobalFunction · 0.85
lua_setfieldFunction · 0.85

Tested by

no test coverage detected