rust: error: clarify that `from_err_ptr` can return `Ok(NULL)`
PATCH 1/1 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).
--- rust/kernel/error.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+)
rust/kernel/error.rs +38 -0
@@ -452,6 +452,9 @@ pub fn to_result(err: crate::ffi::c_int) -> Result {/// for errors. This function performs the check and converts the "error pointer" /// to a normal pointer in an idiomatic fashion. /// +/// Note that a `NULL` pointer is not considered an error pointer, and is returned +/// as-is, wrapped in `Ok`. +/// /// # Examples /// /// ```ignore
@@ -466,6 +469,41 @@ pub fn to_result(err: crate::ffi::c_int) -> Result {/// from_err_ptr(unsafe { bindings::devm_platform_ioremap_resource(pdev.to_ptr(), index) }) /// } /// ``` +/// +/// ``` +/// # use kernel::error::from_err_ptr; +/// # mod bindings { +/// # use kernel::prelude::*; +/// # pub(super) unsafe fn einval_err_ptr() -> *mut kernel::ffi::c_void { +/// # let einval = -(kernel::bindings::EINVAL as isize); +/// # // SAFETY: `einval` is a valid error. +/// # unsafe { kernel::bindings::ERR_PTR(einval) } +/// # } +/// # pub(super) unsafe fn null_ptr() -> *mut kernel::ffi::c_void { +/// # core::ptr::null_mut() +/// # } +/// # pub(super) unsafe fn non_null_ptr() -> *mut kernel::ffi::c_void { +/// # 0x1234 as *mut kernel::ffi::c_void +/// # } +/// # } +/// fn einval_err_ptr() { +/// // SAFETY: ... +/// let result = from_err_ptr(unsafe { bindings::einval_err_ptr() }); +/// assert_eq!(result, Err(EINVAL)); +/// } +/// +/// fn null_ptr() { +/// // SAFETY: ... +/// let result = from_err_ptr(unsafe { bindings::null_ptr() }); +/// assert_eq!(result, Ok(core::ptr::null_mut())); +/// } +/// +/// fn non_null_ptr() { +/// // SAFETY: ... +/// let result = from_err_ptr(unsafe { bindings::non_null_ptr() }); +/// assert_eq!(result, Ok(0x1234 as *mut kernel::ffi::c_void)); +/// } +/// ``` pub fn from_err_ptr <T >(ptr: *mut T ) ->Result <*mut T > {// CAST: Casting a pointer to `*const crate::ffi::c_void` is always valid. let const_ptr: *const crate ::ffi::c_void = ptr.cast ();
Signature · 1 line
2.53.0
rust/kernel/error.rs pub fn to_result(err: crate::ffi::c_int) -> Result { · line 475
Quoted 4 lines · Mirko Adzic
rust/kernel/error.rs +3
-0+/// ``` +/// # use kernel::error::from_err_ptr; +/// # mod bindings {
I see you followed the way we provide "dummy bindings" from elsewhere -- that is good, thanks!
rust/kernel/error.rs pub fn to_result(err: crate::ffi::c_int) -> Result { · line 493
Quoted 5 lines
rust/kernel/error.rs +5
-0+/// fn einval_err_ptr() { +/// // SAFETY: ... +/// let result = from_err_ptr(unsafe { bindings::einval_err_ptr() }); +/// assert_eq!(result, Err(EINVAL)); +/// }
Like you yourself mentioned in the GitHub issue, code inside functions is not called, so please move it out -- you can keep the "blocks" separated with a newline so that it is easier to see the different cases.
Quoted 1 line
+/// assert_eq!(result,
Ok(0x1234 as *mut kernel::ffi::c_void));
Since you are asserting the value, but the bindings are hidden, it may be best to just assert it is not null.
Thanks for the patch! Please feel free to send a v2 fixing those.
Cheers,
Miguel
Hi, thank you for the review.
Quoted 10 lines · Miguel Ojeda
rust/kernel/error.rs pub fn to_result(err: crate::ffi::c_int) -> Result { · line 493
Quoted 5 lines
rust/kernel/error.rs +5
-0+/// fn einval_err_ptr() { +/// // SAFETY: ... +/// let result = from_err_ptr(unsafe { bindings::einval_err_ptr() }); +/// assert_eq!(result, Err(EINVAL)); +/// } Like you yourself mentioned in the GitHub issue, code inside functions is not called, so please move it out -- you can keep the "blocks" separated with a newline so that it is easier to see the different cases.
Will do, thanks.
Quoted 4 lines
Quoted 1 line
+/// assert_eq!(result,
Ok(0x1234 as *mut kernel::ffi::c_void));Since you are asserting the value, but the bindings are hidden, it may be best to just assert it is not null.
Makes total sense, will be addressed in v2.
Best,
Mirko