Skip to content

Commit 0450c9b

Browse files
committed
Make error_traceback never trigger a Lua error
It is called from both Lua and Rust, and any error would hide the error it's trying to generate a traceback for.
1 parent b07557c commit 0450c9b

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

src/util.rs

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -312,14 +312,21 @@ where
312312
// traceback, and if it is a WrappedPanic, does not modify it.
313313
#[cfg_attr(unwind, unwind)]
314314
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+
};
323330

324331
let error = pop_wrapped_error(state).unwrap();
325332
push_wrapped_error(
@@ -330,14 +337,18 @@ pub unsafe extern "C" fn error_traceback(state: *mut ffi::lua_State) -> c_int {
330337
},
331338
);
332339
} 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+
}
341352
}
342353
1
343354
}

0 commit comments

Comments
 (0)