Skip to content

Commit

Permalink
Merge pull request torvalds#473 from LeSeulArtichaut/unsafe-ptr-conta…
Browse files Browse the repository at this point in the history
…iner-of

Don't hide unsafety in `$ptr` argument of `container_of!`
  • Loading branch information
ojeda authored Jul 31, 2021
2 parents 08061bd + e0bcc57 commit a033825
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
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

0 comments on commit a033825

Please sign in to comment.