Skip to content

Commit e30b425

Browse files
committed
Fix crash when initializing Luau sandbox without stdlibs (#361)
1 parent 5129214 commit e30b425

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

mlua-sys/src/luau/lauxlib.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff 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);

tests/luau.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff 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]
279295
fn test_sandbox_threads() -> Result<()> {
280296
let lua = Lua::new();

0 commit comments

Comments
 (0)