Skip to content

Commit a4efac4

Browse files
committed
Safety comments
1 parent 13de00e commit a4efac4

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

libdd-crashtracker/src/collector/assert_interceptor.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ unsafe fn cstr_to_str(ptr: *const libc::c_char, fallback: &str) -> &str {
6262
if ptr.is_null() {
6363
return fallback;
6464
}
65+
// SAFETY: caller guarantees `ptr` is null or points to a valid
66+
// NUL-terminated C string. The null case is handled above.
6567
unsafe { core::ffi::CStr::from_ptr(ptr) }
6668
.to_str()
6769
.unwrap_or(fallback)
@@ -88,6 +90,9 @@ unsafe extern "C" fn hook_assert_fail(
8890
line: libc::c_uint,
8991
function: *const libc::c_char,
9092
) -> ! {
93+
// SAFETY: these pointers come from libc's `__assert_fail` contract:
94+
// `assertion`, `file`, and `function` are valid NUL-terminated C
95+
// strings (or null), and remain valid for the duration of this call.
9196
let assertion_str = unsafe { cstr_to_str(assertion, "<unknown>") };
9297
let file_str = unsafe { cstr_to_str(file, "<unknown>") };
9398
let function_str = unsafe { cstr_to_str(function, "") };
@@ -97,9 +102,18 @@ unsafe extern "C" fn hook_assert_fail(
97102

98103
let orig = ORIG_ASSERT_FN.load(core::sync::atomic::Ordering::Acquire);
99104
if orig != 0 {
105+
// SAFETY: `orig` was stored by `install_assert_hook` from a
106+
// successful `hook_symbol` call, which resolved the real
107+
// `__assert_fail` address by `dlsym`/GOT lookup. The address
108+
// points to libc's `__assert_fail` which has the `AssertFailFn`
109+
// signature.
100110
let func: AssertFailFn = unsafe { core::mem::transmute::<usize, AssertFailFn>(orig) };
111+
// SAFETY: `func` is the original `__assert_fail` with matching
112+
// signature, and the arguments are forwarded unchanged from our
113+
// caller
101114
unsafe { func(assertion, file, line, function) }
102115
} else {
116+
// SAFETY: `abort` is always safe to call; it raises SIGABRT.
103117
unsafe { libc::abort() }
104118
}
105119
}

0 commit comments

Comments
 (0)