Skip to content

Commit 7ccef29

Browse files
Mirko-Aojeda
authored andcommitted
rust: error: clarify that from_err_ptr can return Ok(NULL)
Improve the doc comment of `from_err_ptr` by explicitly stating that it will return `Ok(NULL)` when passed a null pointer, as it isn't an error value. Add a doctest case that tests the behavior described above, as well as other scenarios (non-null/non-error pointer, error value). Suggested-by: Miguel Ojeda <ojeda@kernel.org> Link: https://lore.kernel.org/rust-for-linux/20260322193830.89324-1-ojeda@kernel.org/ Link: #1231 Signed-off-by: Mirko Adzic <adzicmirko97@gmail.com> Reviewed-by: Alice Ryhl <aliceryhl@google.com> Link: https://patch.msgid.link/20260329104319.131057-1-adzicmirko97@gmail.com [ - Added `expect` for `clippy::missing_safety_doc`. - Simplified and removed unsafe block using `Error::to_ptr()`. - Added intra-doc link. - Miguel ] Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent 3418d86 commit 7ccef29

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

rust/kernel/error.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,9 @@ pub fn to_result(err: crate::ffi::c_int) -> Result {
452452
/// for errors. This function performs the check and converts the "error pointer"
453453
/// to a normal pointer in an idiomatic fashion.
454454
///
455+
/// Note that a `NULL` pointer is not considered an error pointer, and is returned
456+
/// as-is, wrapped in [`Ok`].
457+
///
455458
/// # Examples
456459
///
457460
/// ```ignore
@@ -466,6 +469,34 @@ pub fn to_result(err: crate::ffi::c_int) -> Result {
466469
/// from_err_ptr(unsafe { bindings::devm_platform_ioremap_resource(pdev.to_ptr(), index) })
467470
/// }
468471
/// ```
472+
///
473+
/// ```
474+
/// # use kernel::error::from_err_ptr;
475+
/// # mod bindings {
476+
/// # #![expect(clippy::missing_safety_doc)]
477+
/// # use kernel::prelude::*;
478+
/// # pub(super) unsafe fn einval_err_ptr() -> *mut kernel::ffi::c_void {
479+
/// # EINVAL.to_ptr()
480+
/// # }
481+
/// # pub(super) unsafe fn null_ptr() -> *mut kernel::ffi::c_void {
482+
/// # core::ptr::null_mut()
483+
/// # }
484+
/// # pub(super) unsafe fn non_null_ptr() -> *mut kernel::ffi::c_void {
485+
/// # 0x1234 as *mut kernel::ffi::c_void
486+
/// # }
487+
/// # }
488+
/// // SAFETY: ...
489+
/// let einval_err = from_err_ptr(unsafe { bindings::einval_err_ptr() });
490+
/// assert_eq!(einval_err, Err(EINVAL));
491+
///
492+
/// // SAFETY: ...
493+
/// let null_ok = from_err_ptr(unsafe { bindings::null_ptr() });
494+
/// assert_eq!(null_ok, Ok(core::ptr::null_mut()));
495+
///
496+
/// // SAFETY: ...
497+
/// let non_null = from_err_ptr(unsafe { bindings::non_null_ptr() }).unwrap();
498+
/// assert_ne!(non_null, core::ptr::null_mut());
499+
/// ```
469500
pub fn from_err_ptr<T>(ptr: *mut T) -> Result<*mut T> {
470501
// CAST: Casting a pointer to `*const crate::ffi::c_void` is always valid.
471502
let const_ptr: *const crate::ffi::c_void = ptr.cast();

0 commit comments

Comments
 (0)