[v2,02/12] rust: error: improve unsafe code in example

Message ID 20240123150112.124084-3-kernel@valentinobst.de
State New
Headers
Series rust: kernel: documentation improvements |

Commit Message

Valentin Obst Jan. 23, 2024, 3 p.m. UTC
  The `from_err_ptr` function is safe. There is no need for the call to it
to be inside the unsafe block.

Reword the SAFETY comment to provide a better justification of why the
FFI call is safe.

Signed-off-by: Valentin Obst <kernel@valentinobst.de>
---
 rust/kernel/error.rs | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
  

Comments

Martin Rodriguez Reboredo Jan. 27, 2024, 3:10 p.m. UTC | #1
On 1/23/24 12:00, Valentin Obst wrote:
> The `from_err_ptr` function is safe. There is no need for the call to it
> to be inside the unsafe block.
> 
> Reword the SAFETY comment to provide a better justification of why the
> FFI call is safe.
> 
> Signed-off-by: Valentin Obst <kernel@valentinobst.de>
> ---
> [...]

Reviewed-by: Martin Rodriguez Reboredo <yakoyoku@gmail.com>
  

Patch

diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs
index 4f0c1edd63b7..4786d3ee1e92 100644
--- a/rust/kernel/error.rs
+++ b/rust/kernel/error.rs
@@ -264,13 +264,9 @@  pub fn to_result(err: core::ffi::c_int) -> Result {
 ///     pdev: &mut PlatformDevice,
 ///     index: u32,
 /// ) -> Result<*mut core::ffi::c_void> {
-///     // SAFETY: FFI call.
-///     unsafe {
-///         from_err_ptr(bindings::devm_platform_ioremap_resource(
-///             pdev.to_ptr(),
-///             index,
-///         ))
-///     }
+///     // SAFETY: `pdev` points to a valid platform device. There are no safety requirements
+///     // on `index`.
+///     from_err_ptr(unsafe { bindings::devm_platform_ioremap_resource(pdev.to_ptr(), index) })
 /// }
 /// ```
 // TODO: Remove `dead_code` marker once an in-kernel client is available.