File tree 2 files changed +22
-3
lines changed 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) {
144
144
145
145
// set all builtin metatables to read-only
146
146
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
+ }
150
153
151
154
// set globals to readonly and activate safeenv since the env is immutable
152
155
lua_setreadonly ( L , LUA_GLOBALSINDEX , enabled) ;
Original file line number Diff line number Diff line change @@ -275,6 +275,22 @@ fn test_sandbox() -> Result<()> {
275
275
Ok ( ( ) )
276
276
}
277
277
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
+
278
294
#[ test]
279
295
fn test_sandbox_threads ( ) -> Result < ( ) > {
280
296
let lua = Lua :: new ( ) ;
You can’t perform that action at this time.
0 commit comments