Skip to content

Fix soundness issue with container_of! macro #474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 31, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions rust/kernel/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ macro_rules! offset_of {
/// # Safety
///
/// Callers must ensure that the pointer to the field is in fact a pointer to the specified field,
/// as opposed to a pointer to another object of the same type.
/// as opposed to a pointer to another object of the same type. If this condition is not met,
/// any dereference of the resulting pointer is UB.
///
/// # Example
///
Expand All @@ -212,7 +213,7 @@ macro_rules! offset_of {
/// fn test() {
/// let test = Test { a: 10, b: 20 };
/// let b_ptr = &test.b;
/// let test_alias = unsafe { container_of!(b_ptr, Test, b) };
/// let test_alias = container_of!(b_ptr, Test, b);
/// // This prints `true`.
/// pr_info!("{}\n", core::ptr::eq(&test, test_alias));
/// }
Expand All @@ -222,6 +223,6 @@ macro_rules! container_of {
($ptr:expr, $type:ty, $($f:tt)*) => {{
let ptr = $ptr as *const _ as *const u8;
let offset = $crate::offset_of!($type, $($f)*);
unsafe { ptr.offset(-offset) as *const $type }
ptr.wrapping_offset(-offset) as *const $type
}}
}