Skip to content

Don't hide unsafety in $ptr argument of container_of! #473

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
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion rust/kernel/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,8 @@ macro_rules! offset_of {
#[macro_export]
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 as *const _ as *const u8).offset(-offset) as *const $type }
unsafe { ptr.offset(-offset) as *const $type }
}}
}
6 changes: 2 additions & 4 deletions rust/kernel/miscdev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,8 @@ impl<T: Sync> FileOpenAdapter for Registration<T> {
type Arg = T;

unsafe fn convert(_inode: *mut bindings::inode, file: *mut bindings::file) -> *const Self::Arg {
// TODO: `SAFETY` comment required here even if `unsafe` is not present,
// because `container_of!` hides it. Ideally we would not allow
// `unsafe` code as parameters to macros.
let reg = crate::container_of!((*file).private_data, Self, mdev);
// SAFETY: the caller must guarantee that `file` is valid.
let reg = crate::container_of!(unsafe { (*file).private_data }, Self, mdev);
unsafe { &(*reg).context }
}
}
Expand Down