[PATCH] rust: list: add SAFETY comment for HasListLinks in impl_has_list_links_self_ptr!

Alexander Smoliyaninov <[email protected]>
Newsgroups org.kernel.vger.rust-for-linux,org.kernel.vger.linux-kernel
Message-ID <CAD4eaOyQ+MH0gpa=UgECPyWpWMDLC87Fnp=TumzW0hCUW8p6-A@mail.gmail.com>
Document the safety rationale for the `unsafe impl HasListLinks`
generated by `impl_has_list_links_self_ptr!`.

The implementation of `raw_get_list_links` computes a pointer to the
`ListLinksSelfPtr` field and casts it to a `ListLinks` pointer. This is
sound because `ListLinksSelfPtr` is `#[repr(C)]` with `inner: ListLinks`
as its first field, and the method only compiles if the field has the
expected `ListLinksSelfPtr` type.

Suggested-by: Miguel Ojeda <[email protected]>
Link: https://github.com/Rust-for-Linux/linux/issues/351
Signed-off-by: Alex Smolya <[email protected]>
---
 rust/kernel/list/impl_list_item_mod.rs | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/rust/kernel/list/impl_list_item_mod.rs
b/rust/kernel/list/impl_list_item_mod.rs
index 5a3eac9f3..d84aedfdc 100644
--- a/rust/kernel/list/impl_list_item_mod.rs
+++ b/rust/kernel/list/impl_list_item_mod.rs
@@ -86,7 +86,9 @@ macro_rules! impl_has_list_links_self_ptr {
         // right type.
         unsafe impl$(<$($generics)*>)?
$crate::list::HasSelfPtr<$item_type $(, $id)?> for $self {}

-        // SAFETY: TODO.
+        // SAFETY: `raw_get_list_links` only compiles if the field has type
+        // `ListLinksSelfPtr<$item_type, $id>`, which is `repr(C)`
and has `ListLinks` as its first
+        // field.
         unsafe impl$(<$($generics)*>)?
$crate::list::HasListLinks$(<$id>)? for $self {
             #[inline]
             unsafe fn raw_get_list_links(ptr: *mut Self) -> *mut
$crate::list::ListLinks$(<$id>)? {
-- 
2.55.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.