Skip to main content

luaur_vm/macros/
stacklimitreached.rs

1use crate::records::lua_state::lua_State;
2use crate::type_aliases::t_value::TValue;
3
4#[allow(non_snake_case)]
5pub fn stacklimitreached(L: *mut lua_State, n: core::ffi::c_int) -> bool {
6    unsafe {
7        let stack_last = (*L).stack_last as *mut core::ffi::c_char;
8        let top = (*L).top as *mut core::ffi::c_char;
9        // C++ does SIGNED pointer subtraction (`ptrdiff_t`): once `top` passes
10        // `stack_last`, the difference goes negative → the limit is reached. The
11        // original `as usize` subtraction underflowed when `top > stack_last` —
12        // a panic with overflow-checks (fuzz build), and in release it wraps to a
13        // huge value, wrongly reporting "limit NOT reached". Compute it signed to
14        // match C++. (Found by the `splice` fuzz target — a deeply recursive
15        // program pushes `top` past `stack_last`.)
16        let diff = (stack_last as isize) - (top as isize);
17        let threshold = (n as isize) * (core::mem::size_of::<TValue>() as isize);
18        diff <= threshold
19    }
20}