Skip to content

Commit f0806a6

Browse files
committed
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
1 parent 3516f4c commit f0806a6

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/util/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ pub(crate) unsafe fn push_table(
122122
) -> Result<()> {
123123
let narr: c_int = narr.try_into().unwrap_or(c_int::MAX);
124124
let nrec: c_int = nrec.try_into().unwrap_or(c_int::MAX);
125-
if protect || narr >= const { 1 << 30 } || nrec >= const { 1 << 27 } {
125+
if protect || narr >= const { 1 << 26 } || nrec >= const { 1 << 26 } {
126126
protect_lua!(state, 0, 1, |state| ffi::lua_createtable(state, narr, nrec))
127127
} else {
128128
ffi::lua_createtable(state, narr, nrec);

tests/table.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ fn test_table() -> Result<()> {
6161
Ok(())
6262
}
6363

64+
#[test]
65+
#[cfg(target_os = "linux")] // Linux allow overcommiting the memory (relevant for CI)
66+
fn test_table_with_large_capacity() {
67+
let lua = Lua::new();
68+
69+
let t = lua.create_table_with_capacity(1 << 26, 1 << 26);
70+
assert!(t.is_ok());
71+
}
72+
6473
#[test]
6574
fn test_table_push_pop() -> Result<()> {
6675
let lua = Lua::new();

0 commit comments

Comments
 (0)