I have done my homework and studied other responses on this topic but none address my particular issue.
I want to remove the io library completely and the os only partially (let’s say i want to keep os.clock() and others)
How can I achieve this only from the C API.
Due to the nature of the project I am not allowed to modify the Lua headers and the scripts that will be sent to me. These are not under my control. The only thing I can modify is the interpreter.
doing something like this:
lua_pushnil(state_pointer);
lua_setglobal(state_pointer, "os.execute");
won’t help much because in the script the user can call os = require(‘os’) and get all the functions back
I am not allowed to disable the require function so this makes things harder.
Any ideas?
PS:More of a curiosity: if I do something like
luaopen_base(L);
luaopen_table(L);
luaopen_string(L);
luaopen_math(L);
luaopen_loadlib(L); (basically i'm loading every library by hand except os and io)
instead of
luaL_openlibs(L); (this loads all the libraries)
would os = require(‘os’) or io = require(‘io’) still work?
@Nicol Bolas don’t know if i’m doing something wrong but os = require(‘os’) & require(‘io’) just brings everything back.
my code:
luaL_openlibs(LuaInstance); /* load the libs */
lua_pushnil(LuaInstance);
lua_setglobal(LuaInstance, "io");
lua_pushnil(LuaInstance);
lua_setglobal(LuaInstance, "os.execute");
lua_pushnil(LuaInstance);
lua_setglobal(LuaInstance, "os.rename");
lua_pushnil(LuaInstance);
lua_setglobal(LuaInstance, "os.remove");
lua_pushnil(LuaInstance);
lua_setglobal(LuaInstance, "os.exit");
In my script i just do a
os = require('os')
io = require('io')
after this os functions and io functions all work. os.exit still closes my app and io.write works as usual
No, it won’t. Calling
require(os)will simply return theostable. The same table you will have modified. So there’s no problem.So just modify the table after you register it. It will work, and it’s really easy to test that it does.
Be advised:
luaopen_*are not regular C functions. They are Lua C functions; they’re functions that expect to be called via the standard Lua mechanisms. You can’t call them directly from C.In Lua 5.1, you must use push them on the stack and use
lua_pcallor similar calling functions to call them. In Lua 5.2, you should useluaL_requiref, which will put their tables into the Luarequireregistry.Your code has two problems. First:
This does not actually change the table. It simply removes the reference to the table. If you want to change the table itself, then you must change the table. You have to get the
iotable and modify it. Walk the table and set each value in it tonil. Simply replacing the contents of the global variable callediowill do nothing.However, if you want to prevent
iofrom being used entirely, you shouldn’t register it to begin with.The second problem is this:
This modifies the value of the global table who’s key is
["os.execute"]. It is the equivalent of this Lua code:This is not the same as:
When you use
os.executein Lua, that means to take the global table (_G), find the value with the key named"os"and find the"execute"key within the table fetched from"os".When you do
_G["os.execute"], what you’re saying is to take the global table and find the value with the key named"os.execute".See the difference?
What you want to do is get the table stored in the global variable
osand modify that table. You can’t uselua_setglobal, because the members of theostable are not globals; they’re members of a table. Yes, the table that they are stored in just so happens to be a global. But you can’t modify the members of a table stored in a global withlua_setglobal.You have to do this: