File tree Expand file tree Collapse file tree 2 files changed +22
-3
lines changed
Expand file tree Collapse file tree 2 files changed +22
-3
lines changed Original file line number Diff line number Diff line change @@ -144,9 +144,12 @@ pub unsafe fn luaL_sandbox(L: *mut lua_State, enabled: c_int) {
144144
145145 // set all builtin metatables to read-only
146146 lua_pushliteral ( L , "" ) ;
147- lua_getmetatable ( L , -1 ) ;
148- lua_setreadonly ( L , -1 , enabled) ;
149- lua_pop ( L , 2 ) ;
147+ if lua_getmetatable ( L , -1 ) != 0 {
148+ lua_setreadonly ( L , -1 , enabled) ;
149+ lua_pop ( L , 2 ) ;
150+ } else {
151+ lua_pop ( L , 1 ) ;
152+ }
150153
151154 // set globals to readonly and activate safeenv since the env is immutable
152155 lua_setreadonly ( L , LUA_GLOBALSINDEX , enabled) ;
Original file line number Diff line number Diff line change @@ -275,6 +275,22 @@ fn test_sandbox() -> Result<()> {
275275 Ok ( ( ) )
276276}
277277
278+ #[ test]
279+ fn test_sandbox_nolibs ( ) -> Result < ( ) > {
280+ let lua = Lua :: new_with ( StdLib :: NONE , LuaOptions :: default ( ) ) . unwrap ( ) ;
281+
282+ lua. sandbox ( true ) ?;
283+ lua. load ( "global = 123" ) . exec ( ) ?;
284+ let n: i32 = lua. load ( "return global" ) . eval ( ) ?;
285+ assert_eq ! ( n, 123 ) ;
286+ assert_eq ! ( lua. globals( ) . get:: <_, Option <i32 >>( "global" ) ?, Some ( 123 ) ) ;
287+
288+ lua. sandbox ( false ) ?;
289+ assert_eq ! ( lua. globals( ) . get:: <_, Option <i32 >>( "global" ) ?, None ) ;
290+
291+ Ok ( ( ) )
292+ }
293+
278294#[ test]
279295fn test_sandbox_threads ( ) -> Result < ( ) > {
280296 let lua = Lua :: new ( ) ;
You can’t perform that action at this time.
0 commit comments