Skip to content

Commit 01c65cb

Browse files
committed
Auto merge of #44525 - aidanhs:aphs-no-null-deref, r=alexcrichton
Correctly bubble up errors from libbacktrace Previously the first part of this code didn't check for a null pointer and blindly passed it back down, causing a segfault if libbacktrace failed to initialise. I've changed this to check and bubble up the error if relevant. Suggested diff view: https://github.com/rust-lang/rust/pull/44525/files?w=1
2 parents 4cdb362 + 15887d7 commit 01c65cb

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

src/libstd/sys_common/gnu/libbacktrace.rs

+23-15
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ where F: FnMut(&[u8], libc::c_int) -> io::Result<()>
3030
let ret;
3131
let fileline_count = {
3232
let state = unsafe { init_state() };
33+
if state.is_null() {
34+
return Err(io::Error::new(
35+
io::ErrorKind::Other,
36+
"failed to allocate libbacktrace state")
37+
)
38+
}
3339
let mut fileline_win: &mut [FileLine] = &mut fileline_buf;
3440
let fileline_addr = &mut fileline_win as *mut &mut [FileLine];
3541
ret = unsafe {
@@ -62,23 +68,25 @@ pub fn resolve_symname<F>(frame: Frame,
6268
let symname = {
6369
let state = unsafe { init_state() };
6470
if state.is_null() {
71+
return Err(io::Error::new(
72+
io::ErrorKind::Other,
73+
"failed to allocate libbacktrace state")
74+
)
75+
}
76+
let mut data = ptr::null();
77+
let data_addr = &mut data as *mut *const libc::c_char;
78+
let ret = unsafe {
79+
backtrace_syminfo(state,
80+
frame.symbol_addr as libc::uintptr_t,
81+
syminfo_cb,
82+
error_cb,
83+
data_addr as *mut libc::c_void)
84+
};
85+
if ret == 0 || data.is_null() {
6586
None
6687
} else {
67-
let mut data = ptr::null();
68-
let data_addr = &mut data as *mut *const libc::c_char;
69-
let ret = unsafe {
70-
backtrace_syminfo(state,
71-
frame.symbol_addr as libc::uintptr_t,
72-
syminfo_cb,
73-
error_cb,
74-
data_addr as *mut libc::c_void)
75-
};
76-
if ret == 0 || data.is_null() {
77-
None
78-
} else {
79-
unsafe {
80-
CStr::from_ptr(data).to_str().ok()
81-
}
88+
unsafe {
89+
CStr::from_ptr(data).to_str().ok()
8290
}
8391
}
8492
};

0 commit comments

Comments
 (0)