I want to delete io completely The library and os are only part (let’s say I want to keep os.clock() and others)
How can I achieve this from the C API.
Due to the nature of the project, I am not allowed to modify Lua headers and scripts that will be sent to me. These are not under my control. The only thing I can modify is the interpreter.
Do something like this:
lua_pushnil(state_pointer);
lua_setglobal(state_pointer, "os.execute");
Not much help, because in the script the user can call os = require( ‘os’) and get all features
I don’t allow the require function to be disabled, so this will make things harder.
Any ideas?
PS: More curiosity: If I did something similar
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)
Replace
luaL_openlibs(L); (this loads all the libraries)
os = require(‘os’) or io = require(‘io’) Is it still valid?
@Nicol Bolas don’t know what I did 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");< br />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");
< p>In my script, I just made one
os = require('os')
io = require('io')
All work after this os function and io function. os.exit still closes my application, io.write works as usual
won’t help much because in the script the user can call os = require(‘os’) and get all the functions back
< /blockquote>No, it will not. Calling require(os) will only return the os table. You will modify the same table. So there is no problem.
So just modify the table after registration .It will work, it is really easy to test it.
p>
luaopen_base(L);
Please note: luaopen_* is not a regular C function. They are Lua C functions; they are expected to be called through the standard Lua mechanism Functions. You can’t call them directly from C.
In Lua 5.1, you have to use them on the stack and use lua_pcall or similar calling functions to call them. In Lua 5.2, you should Use luaL_requiref, it will put their table in the Lua require registry.
Your code has two problems. First:
lua_setglobal (LuaInstance, "io");
lua_pushnil(LuaInstance);This does not actually change the table. It just removes the reference to the table. If you want to change the table itself, you must change the table You have to get the io table and modify it. Walk the table and set each value in it to zero. Simply replacing the contents of the global variable named io will not work.
But if you want To prevent io from being fully used, it should not be registered as the beginning.
The second question is:
lua_pushnil(LuaInstance);
lua_setglobal(LuaInstance, "os.execute");This will modify the value of the global table, the key of this key is [“os.execute”]. It is equivalent to this Lua code:
_G["os.execute"] = nilThis is different from the following:
os. execute = nil;When you use os.execute in Lua, it means to get the global table (_G), use the key named “os” to find the value, and get it from “os” Find the “execute” key in the table.
When you execute _G [“os.execute”], what you are saying is to get the global table and use the key named “os.execute” to find Value.
See the difference?
What you want to do is get the table stored in the global variable os and modify the table. You can’t use lua_setglobal because the members of the os table are not global; they are members of the table. Yes, The tables they store happen to be global. But you cannot use lua_setglobal to modify the members of the tables stored in the global.
You must do this:
lua_getglobal(L, "os");
lua_pushnil(L);
lua_setfield(L, -2, "execute");
lua_pushnil(L);
lua_setfield(L , -2, "rename");
lua_pushnil(L);
lua_setfield(L, -2, "remove");
lua_pushnil(L);
lua_setfield(L , -2, "exit");
lua_pop(L, 1);
I finished my homework and researched other replies on this topic , But did not solve my specific problem.
I want to completely remove the io library and os only partially (let’s say I want to keep os.clock() and others)
How can I achieve this 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 do The modification is the interpreter.
Do something like this:
lua_pushnil(state_pointer);
lua_setglobal(state_pointer, "os.execute" );
Not much help, because in the script the user can call os = require(‘os’) and get all functions
I don’t allow the require function to be disabled, so this will make Things get harder.
Any ideas?
PS: More curiosity: If I did something similar
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)
Replace
luaL_openlibs(L); (this loads all the libraries)
os = require(‘os’) or io = require(‘io’) Is it still valid?
@Nicol Bolas don’t know what I did 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");< br />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");
< p>In my script, I just made one
os = require('os')
io = require('io')
All work after this os function and io function. os.exit still closes my application, io.write works as usual
won’t help much because in the script the user can call os = require(‘os’) and get all the functions back
No, it won’t. Calling require(os) will only return the os table. The same table you will modify. So there is no problem.
So just modify the table after registration. It will work, and testing it is really easy. < /p>
luaopen_base(L);
Please note: l uaopen_* are not regular C functions. They are Lua C functions; they are functions that are expected to be called through the standard Lua mechanism. You cannot call them directly from C.
In Lua 5.1, you must Use them on the stack and use lua_pcall or similar calling functions to call them. In Lua 5.2, you should use luaL_requiref, which will put their tables in the Lua require registry.
Your code There are two problems. First:
lua_setglobal(LuaInstance, "io");
lua_pushnil(LuaInstance);
This is actually Doesn’t change the table. It just removes the reference to the table. If you want to change the table itself, you have to change the table. You have to get the io table and modify it. Walk the table and set each value in it to zero. Simply replace the name The content of the global variable for io will not work.
However, if you want to prevent io from being fully used, you should not register it as the beginning.
The second question is :
lua_pushnil(LuaInstance);
lua_setglobal(LuaInstance, "os.execute");
This will modify the value of the global table, The key of this key is [“os.execute”]. It is equivalent to this Lua code:
_G["os.execute"] = nil
This is different from the following:
os.execute = nil;
When you use os.execute in Lua, it means getting the global Table (_G), use the key named “os” to find the value, and find the “execute” key in the table obtained from “os”.
When you execute _G [“os.execute” ], what you are talking about is to get the global table and use the key named “os.execute” to look up the value.
See the difference?
What you want to do is get the table stored in the global variable os and modify the table. You can’t use lua_setglobal because the members of the os table are not global; they are members of the table. Yes, The tables they store happen to be global. But you cannot use lua_setglobal to modify the members of the tables stored in the global.
You must do this:
lua_getglobal(L, "os");
lua_pushnil(L);
lua_setfield(L, -2, "execute");
lua_pushnil(L);
lua_setfield(L , -2, "rename");
lua_pushnil(L);
lua_setfield(L, -2, "remove");
lua_pushnil(L);
lua_setfield(L , -2, "exit");
lua_pop(L, 1);