Skip to content

Commit a033825

Browse files
authored
Merge pull request #473 from LeSeulArtichaut/unsafe-ptr-container-of
Don't hide unsafety in `$ptr` argument of `container_of!`
2 parents 08061bd + e0bcc57 commit a033825

File tree

2 files changed

+4
-5
lines changed

2 files changed

+4
-5
lines changed

rust/kernel/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,8 @@ macro_rules! offset_of {
220220
#[macro_export]
221221
macro_rules! container_of {
222222
($ptr:expr, $type:ty, $($f:tt)*) => {{
223+
let ptr = $ptr as *const _ as *const u8;
223224
let offset = $crate::offset_of!($type, $($f)*);
224-
unsafe { ($ptr as *const _ as *const u8).offset(-offset) as *const $type }
225+
unsafe { ptr.offset(-offset) as *const $type }
225226
}}
226227
}

rust/kernel/miscdev.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,8 @@ impl<T: Sync> FileOpenAdapter for Registration<T> {
8585
type Arg = T;
8686

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

0 commit comments

Comments
 (0)