Re: [PATCH v3 1/7] rust: firmware: add request_into_buf()
Timur Tabi <[email protected]>
| Newsgroups | dev.linux.lists.nova-gpu,dev.linux.lists.driver-core,org.kernel.vger.rust-for-linux |
|---|---|
| Message-ID | <[email protected]> |
On Fri, 2026-07-03 at 10:51 +0800, Alvin Sun wrote:
> > +pub fn request_into_buf(name: &CStr, dev: &Device, buf: &mut [u8]) -> Result {
> > + // `as_mut_ptr()` on an empty slice returns a non-NULL pointer to
> > + // memory which the loader does not own. Passing that pointer with `size == 0`
> > + // makes the loader believe that it is buffer it allocated itself, so when
> > + // `release_firmware()` is called, it will vfree the pointer and trigger a
> > + // bug. Reject empty slices to avoid this situation.
> > + if buf.is_empty() {
> > + return Err(crate::error::code::EINVAL);
>
> `EINVAL` is already in prelude, you can use it directly.
I would have to add it to the list of imports.
> > + // SAFETY: `pfw` is a valid pointer to a NULL initialized `bindings::firmware` pointer.
> > + // `name` and `dev` are valid as by their type invariants. `buf` is a valid writable
> > + // buffer of `buf.len()` bytes.
> > + let ret = unsafe {
> > + bindings::request_firmware_into_buf(
> > + pfw,
> > + name.as_char_ptr(),
> > + dev.as_raw(),
> > + buf.as_mut_ptr().cast(),
> > + buf.len(),
> > + )
> > + };
> > + if ret != 0 {
> > + return Err(Error::from_errno(ret));
> > + }
>
> `to_result` can be used here to simplify.
Will add in v4.