rust: firmware: avoid UB in example by passing parameter
Subsystems (3)
Miguel Ojeda
maintainerPATCH 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
@@ -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
Quoted 7 lines · Miguel Ojeda
The
Firmwareexample crafts anARef<Device>using a null pointer, which breaks the safety requirements ofDevice::get_device().Instead, pass an
ARefvia a parameter, which is simpler, avoids UB and removes anunsafeblock.Signed-off-by Miguel Ojeda <[email protected]>
Quoted 3 lines
--- rust/kernel/firmware.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)
Quoted 7 lines · Miguel Ojeda
The
Firmwareexample crafts anARef<Device>using a null pointer, which breaks the safety requirements ofDevice::get_device().Instead, pass an
ARefvia a parameter, which is simpler, avoids UB and removes anunsafeblock.Signed-off-by Miguel Ojeda <[email protected]>
Danilo Krummrich
maintainerQuoted 5 lines · Miguel Ojeda
rust/kernel/firmware.rs +0
-0
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(); ///
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.