* [PATCH] rust: firmware: avoid UB in example by passing parameter
@ 2026-07-11 18:02 Miguel Ojeda
2026-07-12 0:08 ` Gary Guo
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Miguel Ojeda @ 2026-07-11 18:02 UTC (permalink / raw)
To: Luis Chamberlain, Russ Weight, Danilo Krummrich, Miguel Ojeda
Cc: driver-core, Boqun Feng, Gary Guo, Björn Roy Baron,
Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
Daniel Almeida, Tamir Duberstein, Alexandre Courbot,
Onur Özkan, rust-for-linux
The `Firmware` example crafts an `ARef<Device>` using a null pointer,
which breaks the safety requirements of `Device::get_device()`.
Instead, pass an `ARef` via a parameter, which is simpler, avoids UB
and removes an `unsafe` block.
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
---
rust/kernel/firmware.rs | 7 ++-----
1 file changed, 2 insertions(+), 5 deletions(-)
diff --git a/rust/kernel/firmware.rs b/rust/kernel/firmware.rs
index 71168d8004e2..3aa9de3c4a04 100644
--- a/rust/kernel/firmware.rs
+++ b/rust/kernel/firmware.rs
@@ -51,12 +51,9 @@ fn request_nowarn() -> Self {
/// # Examples
///
/// ```no_run
-/// # use kernel::{device::Device, firmware::Firmware};
-///
-/// # fn no_run() -> Result<(), Error> {
-/// # // SAFETY: *NOT* safe, just for the example to get an `ARef<Device>` instance
-/// # let dev = unsafe { Device::get_device(core::ptr::null_mut()) };
+/// # use kernel::{device::Device, firmware::Firmware, sync::aref::ARef};
///
+/// # fn no_run(dev: ARef<Device>) -> Result<(), Error> {
/// let fw = Firmware::request(c"path/to/firmware.bin", &dev)?;
/// let blob = fw.data();
///
base-commit: 8cdeaa50eae8dad34885515f62559ee83e7e8dda
--
2.55.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] rust: firmware: avoid UB in example by passing parameter
2026-07-11 18:02 [PATCH] rust: firmware: avoid UB in example by passing parameter Miguel Ojeda
@ 2026-07-12 0:08 ` Gary Guo
2026-07-12 0:15 ` Alexandre Courbot
2026-07-12 0:15 ` Danilo Krummrich
2 siblings, 0 replies; 4+ messages in thread
From: Gary Guo @ 2026-07-12 0:08 UTC (permalink / raw)
To: Miguel Ojeda, Luis Chamberlain, Russ Weight, Danilo Krummrich
Cc: driver-core, Boqun Feng, Gary Guo, Björn Roy Baron,
Benno Lossin, Andreas Hindborg, Alice Ryhl, Trevor Gross,
Daniel Almeida, Tamir Duberstein, Alexandre Courbot,
Onur Özkan, rust-for-linux
On Sat Jul 11, 2026 at 7:02 PM BST, Miguel Ojeda wrote:
> The `Firmware` example crafts an `ARef<Device>` using a null pointer,
> which breaks the safety requirements of `Device::get_device()`.
>
> Instead, pass an `ARef` via a parameter, which is simpler, avoids UB
> and removes an `unsafe` block.
>
> Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
Reviewed-by: Gary Guo <gary@garyguo.net>
> ---
> rust/kernel/firmware.rs | 7 ++-----
> 1 file changed, 2 insertions(+), 5 deletions(-)
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] rust: firmware: avoid UB in example by passing parameter
2026-07-11 18:02 [PATCH] rust: firmware: avoid UB in example by passing parameter Miguel Ojeda
2026-07-12 0:08 ` Gary Guo
@ 2026-07-12 0:15 ` Alexandre Courbot
2026-07-12 0:15 ` Danilo Krummrich
2 siblings, 0 replies; 4+ messages in thread
From: Alexandre Courbot @ 2026-07-12 0:15 UTC (permalink / raw)
To: Miguel Ojeda
Cc: Luis Chamberlain, Russ Weight, Danilo Krummrich, driver-core,
Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin,
Andreas Hindborg, Alice Ryhl, Trevor Gross, Daniel Almeida,
Tamir Duberstein, Onur Özkan, rust-for-linux
On Sun Jul 12, 2026 at 3:02 AM JST, Miguel Ojeda wrote:
> The `Firmware` example crafts an `ARef<Device>` using a null pointer,
> which breaks the safety requirements of `Device::get_device()`.
>
> Instead, pass an `ARef` via a parameter, which is simpler, avoids UB
> and removes an `unsafe` block.
>
> Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] rust: firmware: avoid UB in example by passing parameter
2026-07-11 18:02 [PATCH] rust: firmware: avoid UB in example by passing parameter Miguel Ojeda
2026-07-12 0:08 ` Gary Guo
2026-07-12 0:15 ` Alexandre Courbot
@ 2026-07-12 0:15 ` Danilo Krummrich
2 siblings, 0 replies; 4+ messages in thread
From: Danilo Krummrich @ 2026-07-12 0:15 UTC (permalink / raw)
To: Miguel Ojeda
Cc: Luis Chamberlain, Russ Weight, driver-core, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
Trevor Gross, Daniel Almeida, Tamir Duberstein, Alexandre Courbot,
Onur Özkan, rust-for-linux
On Sat Jul 11, 2026 at 8:02 PM CEST, Miguel Ojeda wrote:
> diff --git a/rust/kernel/firmware.rs b/rust/kernel/firmware.rs
> index 71168d8004e2..3aa9de3c4a04 100644
> --- a/rust/kernel/firmware.rs
> +++ b/rust/kernel/firmware.rs
> @@ -51,12 +51,9 @@ fn request_nowarn() -> Self {
> /// # Examples
> ///
> /// ```no_run
> -/// # use kernel::{device::Device, firmware::Firmware};
> -///
> -/// # fn no_run() -> Result<(), Error> {
> -/// # // SAFETY: *NOT* safe, just for the example to get an `ARef<Device>` instance
> -/// # let dev = unsafe { Device::get_device(core::ptr::null_mut()) };
> +/// # use kernel::{device::Device, firmware::Firmware, sync::aref::ARef};
> ///
We could take the chance to also drop this empty line.
> +/// # fn no_run(dev: ARef<Device>) -> Result<(), Error> {
> /// let fw = Firmware::request(c"path/to/firmware.bin", &dev)?;
> /// let blob = fw.data();
> ///
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-07-12 0:16 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-11 18:02 [PATCH] rust: firmware: avoid UB in example by passing parameter Miguel Ojeda
2026-07-12 0:08 ` Gary Guo
2026-07-12 0:15 ` Alexandre Courbot
2026-07-12 0:15 ` Danilo Krummrich
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox