Skip to contentSkip to conversation

rust: firmware: avoid UB in example by passing parameter

PATCH Applied Miguel Ojeda 4 participants 5 messages Jul 11 – Jul 16

Subsystems (3)
lore
Share & tools
lore

Miguel Ojeda

author maintainer
#0 lore raw

PATCH 1/1 rust: firmware: avoid UB in example by passing parameter

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.

---
 rust/kernel/firmware.rs | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
rust/kernel/firmware.rs +2 -5
index 71168d8004e2..3aa9de3c4a04 100644
@@ -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

Signature · 1 line
2.55.0
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Miguel Ojeda

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.

Quoted 3 lines
---
 rust/kernel/firmware.rs | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
maintainer
#3 lore raw
In reply to Miguel Ojeda
Quoted 5 lines · Miguel Ojeda
rust/kernel/firmware.rs +0 -0
index 71168d8004e2..3aa9de3c4a04 100644

rust/kernel/firmware.rs fn request_nowarn() -> Self { · line 55

Quoted 10 lines
rust/kernel/firmware.rs +1 -5
@@ -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.

Quoted 4 lines
rust/kernel/firmware.rs +1 -0
+/// # fn no_run(dev: ARef<Device>) -> Result<(), Error> {
 /// let fw = Firmware::request(c"path/to/firmware.bin", &dev)?;
 /// let blob = fw.data();
 ///
maintainer Applied
#4 lore raw
In reply to Miguel Ojeda
Quoted 1 line · Miguel Ojeda

[PATCH] rust: firmware: avoid UB in example by passing parameter

Applied, thanks!

Branch: driver-core-testing
Tree: git://git.kernel.org/pub/scm/linux/kernel/git/driver-core/driver-core.git

[1/1] rust: firmware: avoid UB in example by passing parameter
commit: c945c8f37a81

[ Also drop the second superfluous empty line. - Danilo ]

The patch will appear in the next linux-next integration (typically within 24 hours on weekdays).

The patch is in the driver-core-testing branch and will be promoted to driver-core-next after validation.