Skip to content

Commit e79e5cf

Browse files
committed
feat(native-tracer): fix error message lifetimes
1 parent 800965f commit e79e5cf

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Create CString objects for error strings before passing them to rb_raise

gems/codetracer-ruby-recorder/ext/native_tracer/src/lib.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -685,18 +685,24 @@ unsafe extern "C" fn flush_trace(self_val: VALUE, out_dir: VALUE, format: VALUE)
685685
match std::str::from_utf8(slice) {
686686
Ok(path_str) => {
687687
if let Err(e) = flush_to_dir(&recorder.tracer, Path::new(path_str), fmt) {
688+
let msg = std::ffi::CString::new(e.to_string())
689+
.unwrap_or_else(|_| std::ffi::CString::new("unknown error").unwrap());
688690
rb_raise(
689691
rb_eIOError,
690692
b"Failed to flush trace: %s\0".as_ptr() as *const c_char,
691-
e.to_string().as_ptr() as *const c_char,
693+
msg.as_ptr(),
692694
);
693695
}
694696
}
695-
Err(e) => rb_raise(
696-
rb_eIOError,
697-
b"Invalid UTF-8 in path: %s\0".as_ptr() as *const c_char,
698-
e.to_string().as_ptr() as *const c_char,
699-
),
697+
Err(e) => {
698+
let msg = std::ffi::CString::new(e.to_string())
699+
.unwrap_or_else(|_| std::ffi::CString::new("invalid utf8").unwrap());
700+
rb_raise(
701+
rb_eIOError,
702+
b"Invalid UTF-8 in path: %s\0".as_ptr() as *const c_char,
703+
msg.as_ptr(),
704+
)
705+
},
700706
}
701707

702708
Qnil.into()

0 commit comments

Comments
 (0)