Skip to content

Commit 3dc53f8

Browse files
committed
Check table requested capacity limits before enabling unprotected mode.
Lua tables have limits and can overflow, which must be captured in protected mode.
1 parent 646827a commit 3dc53f8

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/util/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl Drop for StackGuard {
8888
#[inline(always)]
8989
pub(crate) unsafe fn push_string(state: *mut ffi::lua_State, s: &[u8], protect: bool) -> Result<()> {
9090
// Always use protected mode if the string is too long
91-
if protect || s.len() > (1 << 30) {
91+
if protect || s.len() >= const { 1 << 30 } {
9292
protect_lua!(state, 0, 1, |state| {
9393
ffi::lua_pushlstring(state, s.as_ptr() as *const c_char, s.len());
9494
})
@@ -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 {
125+
if protect || narr >= const { 1 << 30 } || nrec >= const { 1 << 27 } {
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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,3 +473,18 @@ fn test_table_object_like() -> Result<()> {
473473

474474
Ok(())
475475
}
476+
477+
#[cfg(any(feature = "luau", feature = "lua51"))]
478+
#[test]
479+
fn test_too_large_table() -> Result<()> {
480+
let lua = Lua::new();
481+
482+
// Test creating a too large table
483+
let result = lua.create_table_with_capacity(1 << 31, 1 << 27);
484+
assert!(
485+
matches!(&result, Err(Error::RuntimeError(err)) if err.contains("table overflow") || err.contains("memory allocation error")),
486+
"expected a runtime error (table overflow), got: {result:?}",
487+
);
488+
489+
Ok(())
490+
}

0 commit comments

Comments
 (0)