@@ -312,14 +312,21 @@ where
312
312
// traceback, and if it is a WrappedPanic, does not modify it.
313
313
#[ cfg_attr( unwind, unwind) ]
314
314
pub unsafe extern "C" fn error_traceback ( state : * mut ffi:: lua_State ) -> c_int {
315
- ffi:: luaL_checkstack ( state, 2 , ptr:: null ( ) ) ;
316
-
317
- if is_wrapped_error ( state, 1 ) {
318
- ffi:: luaL_traceback ( state, state, ptr:: null ( ) , 0 ) ;
319
- let traceback = CStr :: from_ptr ( ffi:: lua_tostring ( state, -1 ) )
320
- . to_string_lossy ( )
321
- . into_owned ( ) ;
322
- ffi:: lua_pop ( state, 1 ) ;
315
+ if ffi:: lua_checkstack ( state, 2 ) == 0 {
316
+ // If we don't have enough stack space to even check the error type, do nothing
317
+ } else if is_wrapped_error ( state, 1 ) {
318
+ let traceback = if ffi:: lua_checkstack ( state, 11 ) != 0 {
319
+ gc_guard ( state, || {
320
+ ffi:: luaL_traceback ( state, state, ptr:: null ( ) , 0 ) ;
321
+ } ) ;
322
+ let traceback = CStr :: from_ptr ( ffi:: lua_tostring ( state, -1 ) )
323
+ . to_string_lossy ( )
324
+ . into_owned ( ) ;
325
+ ffi:: lua_pop ( state, 1 ) ;
326
+ traceback
327
+ } else {
328
+ "not enough stack space for traceback" . to_owned ( )
329
+ } ;
323
330
324
331
let error = pop_wrapped_error ( state) . unwrap ( ) ;
325
332
push_wrapped_error (
@@ -330,14 +337,18 @@ pub unsafe extern "C" fn error_traceback(state: *mut ffi::lua_State) -> c_int {
330
337
} ,
331
338
) ;
332
339
} else if !is_wrapped_panic ( state, 1 ) {
333
- let s = ffi:: lua_tostring ( state, 1 ) ;
334
- let s = if s. is_null ( ) {
335
- cstr ! ( "<unprintable Rust panic>" )
336
- } else {
337
- s
338
- } ;
339
- ffi:: luaL_traceback ( state, state, s, 0 ) ;
340
- ffi:: lua_remove ( state, -2 ) ;
340
+ if ffi:: lua_checkstack ( state, 11 ) != 0 {
341
+ gc_guard ( state, || {
342
+ let s = ffi:: lua_tostring ( state, 1 ) ;
343
+ let s = if s. is_null ( ) {
344
+ cstr ! ( "<unprintable lua error>" )
345
+ } else {
346
+ s
347
+ } ;
348
+ ffi:: luaL_traceback ( state, state, s, 0 ) ;
349
+ ffi:: lua_remove ( state, -2 ) ;
350
+ } ) ;
351
+ }
341
352
}
342
353
1
343
354
}
0 commit comments