From f0806a6d6255ee440d703ea472a033c8d4eed881 Mon Sep 17 00:00:00 2001 From: Alex Orlenko Date: Wed, 13 Aug 2025 22:49:40 +0100 Subject: [PATCH] Lower fastpath table creation limit to 1 << 26 When Lua is configured without memory restrictions, we use fastpath for table creation (unprotected mode). In generally it's safe as long as we `abort()` on allocation failure. However some Lua versions have additional restrictions on table size that we need to adhere in mlua too. Probably Luau has the lowest limits. Fixes #627 --- src/util/mod.rs | 2 +- tests/table.rs | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/util/mod.rs b/src/util/mod.rs index b69a867..4d07d3c 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -122,7 +122,7 @@ pub(crate) unsafe fn push_table( ) -> Result<()> { let narr: c_int = narr.try_into().unwrap_or(c_int::MAX); let nrec: c_int = nrec.try_into().unwrap_or(c_int::MAX); - if protect || narr >= const { 1 << 30 } || nrec >= const { 1 << 27 } { + if protect || narr >= const { 1 << 26 } || nrec >= const { 1 << 26 } { protect_lua!(state, 0, 1, |state| ffi::lua_createtable(state, narr, nrec)) } else { ffi::lua_createtable(state, narr, nrec); diff --git a/tests/table.rs b/tests/table.rs index 3d4f5ef..922da8d 100644 --- a/tests/table.rs +++ b/tests/table.rs @@ -61,6 +61,15 @@ fn test_table() -> Result<()> { Ok(()) } +#[test] +#[cfg(target_os = "linux")] // Linux allow overcommiting the memory (relevant for CI) +fn test_table_with_large_capacity() { + let lua = Lua::new(); + + let t = lua.create_table_with_capacity(1 << 26, 1 << 26); + assert!(t.is_ok()); +} + #[test] fn test_table_push_pop() -> Result<()> { let lua = Lua::new();