Unit test not showing println due to crash in unsafe code


pub fn test_00() {

Note the todo!() -- this ensures that the code panics, causing it to not exit successfully, thus allowing the println's to show (rather than get captured).

The problem I run into is that if do_stuff() makes an unsafe C call and this call crashes the program, then all of my println output vanishes .Is there a way to somehow still get the println output in this case ?

Seems really unlikely to me.

Once you have gone "unsafe" and the code you run in there messes up the system, scribbling over memory, corrupting the stack, etc, anything can happen. Rust cannot help you. That is what "unsafe" is about.

It's time to breakout a debugger and debug that C code and/or your interface to it.

Panic unwinding across FFI boundary is UB. So you should catch it before crossing the border and handle it properly.

1 Like

Err, sorry, I mean the prinln's that happpen before the unsafe call. I.e.

fn do_stuff() {
  println!("hello world"); // this is not displayed
  unsafe { something_bad_causing_memory_error(); }

If you know that this is happening, you can use the nocapture flag on the test binary to have printing print directly to stdout. To use it, invoke cargo test like:

cargo test -- --nocapture

The extra -- is there so that --nocapture is passed to the actual test binary, not to cargo test.


This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.