Skip to content

Commit

Permalink
Update lua runtime to open libs after file
Browse files Browse the repository at this point in the history
This stops the user from calling any function, for e.g. 'print', outside
of the functions stated in the documentation
  • Loading branch information
wins1ey committed Jun 5, 2024
1 parent 3ddba87 commit 64005a5
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions src/auto-splitter.c
Original file line number Diff line number Diff line change
Expand Up @@ -389,38 +389,33 @@ void run_auto_splitter_cycle(
void run_auto_splitter()
{
lua_State* L = luaL_newstate();
luaL_openlibs(L);
disable_functions(L, disabled_functions);
lua_pushcfunction(L, read_address);
lua_setglobal(L, "readAddress");
lua_pushcfunction(L, getPid);
lua_setglobal(L, "getPID");

char current_file[PATH_MAX];
strcpy(current_file, auto_splitter_file);

// Load the Lua file
if (luaL_loadfile(L, auto_splitter_file) != LUA_OK) {
// Error loading the file
const char* error_msg = lua_tostring(L, -1);
lua_pop(L, 1); // Remove the error message from the stack
fprintf(stderr, "Lua syntax error: %s\n", error_msg);
fprintf(stderr, "Lua syntax error: %s\n", lua_tostring(L, -1));
lua_close(L);
atomic_store(&auto_splitter_enabled, false);
return;
}

// Execute the Lua file
if (lua_pcall(L, 0, LUA_MULTRET, 0) != LUA_OK) {
// Error executing the file
const char* error_msg = lua_tostring(L, -1);
lua_pop(L, 1); // Remove the error message from the stack
fprintf(stderr, "Lua runtime error: %s\n", error_msg);
fprintf(stderr, "Lua runtime error: %s\n", lua_tostring(L, -1));
lua_close(L);
atomic_store(&auto_splitter_enabled, false);
return;
}

luaL_openlibs(L);
disable_functions(L, disabled_functions);
lua_pushcfunction(L, read_address);
lua_setglobal(L, "readAddress");
lua_pushcfunction(L, getPid);
lua_setglobal(L, "getPID");

char current_file[PATH_MAX];
strcpy(current_file, auto_splitter_file);

bool state_exists = lua_function_exists(L, "state");
bool start_exists = lua_function_exists(L, "start");
bool on_start_exists = lua_function_exists(L, "onStart");
Expand Down

0 comments on commit 64005a5

Please sign in to comment.