@@ -1046,10 +1046,10 @@ impl Lua {
10461046 /// Level `0` is the current running function, whereas level `n+1` is the function that has
10471047 /// called level `n` (except for tail calls, which do not count in the stack).
10481048 pub fn inspect_stack < R > ( & self , level : usize , f : impl FnOnce ( & Debug ) -> R ) -> Option < R > {
1049+ let level = c_int:: try_from ( level) . ok ( ) ?;
10491050 let lua = self . lock ( ) ;
10501051 unsafe {
10511052 let mut ar = mem:: zeroed :: < ffi:: lua_Debug > ( ) ;
1052- let level = level as c_int ;
10531053 #[ cfg( not( feature = "luau" ) ) ]
10541054 if ffi:: lua_getstack ( lua. state ( ) , level, & mut ar) == 0 {
10551055 return None ;
@@ -1076,8 +1076,9 @@ impl Lua {
10761076 Some ( s) => ffi:: lua_pushlstring( state, s. as_ptr( ) as * const c_char, s. len( ) ) ,
10771077 None => ptr:: null( ) ,
10781078 } ;
1079- // `protect_lua` adds it's own call frame, so we need to increase level by 1
1080- ffi:: luaL_traceback( state, state, msg, ( level + 1 ) as c_int) ;
1079+ // `protect_lua` adds its own call frame, leave room for Lua's internal increment.
1080+ let level = level. saturating_add( 1 ) . min( ( c_int:: MAX - 1 ) as usize ) as c_int;
1081+ ffi:: luaL_traceback( state, state, msg, level) ;
10811082 } ) ?;
10821083 Ok ( LuaString ( lua. try_pop_ref ( ) ?) )
10831084 }
0 commit comments