[PATCH 1/2] rust: arc: add explicit `drop()` around `Box::from_raw()`

Miguel Ojeda posted 2 patches 2 years, 3 months ago
[PATCH 1/2] rust: arc: add explicit `drop()` around `Box::from_raw()`
Posted by Miguel Ojeda 2 years, 3 months ago
`Box::from_raw()` is `#[must_use]`, which means the result cannot
go unused.

In Rust 1.71.0, this was not detected because the block expression
swallows the diagnostic [1]:

    unsafe { Box::from_raw(self.ptr.as_ptr()) };

It would have been detected, however, if the line had been instead:

    unsafe { Box::from_raw(self.ptr.as_ptr()); }

i.e. the semicolon being inside the `unsafe` block, rather than
outside.

In Rust 1.72.0, the compiler started warning about this [2], so
without this patch we will get:

        error: unused return value of `alloc::boxed::Box::<T>::from_raw` that must be used
        --> rust/kernel/sync/arc.rs:302:22
        |
    302 |             unsafe { Box::from_raw(self.ptr.as_ptr()) };
        |                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        |
        = note: call `drop(Box::from_raw(ptr))` if you intend to drop the `Box`
        = note: `-D unused-must-use` implied by `-D warnings`
    help: use `let _ = ...` to ignore the resulting value
        |
    302 |             unsafe { let _ = Box::from_raw(self.ptr.as_ptr()); };
        |                      +++++++                                 +

Thus add an add an explicit `drop()` as the `#[must_use]`'s
annotation suggests (instead of the more general help line).

Link: https://github.com/rust-lang/rust/issues/104253 [1]
Link: https://github.com/rust-lang/rust/pull/112529 [2]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
---
 rust/kernel/sync/arc.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs
index 172f563976a9..4c14d540a581 100644
--- a/rust/kernel/sync/arc.rs
+++ b/rust/kernel/sync/arc.rs
@@ -299,7 +299,7 @@ fn drop(&mut self) {
             // The count reached zero, we must free the memory.
             //
             // SAFETY: The pointer was initialised from the result of `Box::leak`.
-            unsafe { Box::from_raw(self.ptr.as_ptr()) };
+            unsafe { drop(Box::from_raw(self.ptr.as_ptr())) };
         }
     }
 }
-- 
2.42.0
Re: [PATCH 1/2] rust: arc: add explicit `drop()` around `Box::from_raw()`
Posted by Alice Ryhl 2 years, 3 months ago
On Wed, Aug 23, 2023 at 6:03 PM Miguel Ojeda <ojeda@kernel.org> wrote:
> `Box::from_raw()` is `#[must_use]`, which means the result cannot
> go unused.
>
> In Rust 1.71.0, this was not detected because the block expression
> swallows the diagnostic [1]:
>
>     unsafe { Box::from_raw(self.ptr.as_ptr()) };
>
> It would have been detected, however, if the line had been instead:
>
>     unsafe { Box::from_raw(self.ptr.as_ptr()); }
>
> i.e. the semicolon being inside the `unsafe` block, rather than
> outside.
>
> In Rust 1.72.0, the compiler started warning about this [2], so
> without this patch we will get:
>
>         error: unused return value of `alloc::boxed::Box::<T>::from_raw` that must be used
>         --> rust/kernel/sync/arc.rs:302:22
>         |
>     302 |             unsafe { Box::from_raw(self.ptr.as_ptr()) };
>         |                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>         |
>         = note: call `drop(Box::from_raw(ptr))` if you intend to drop the `Box`
>         = note: `-D unused-must-use` implied by `-D warnings`
>     help: use `let _ = ...` to ignore the resulting value
>         |
>     302 |             unsafe { let _ = Box::from_raw(self.ptr.as_ptr()); };
>         |                      +++++++                                 +
>
> Thus add an add an explicit `drop()` as the `#[must_use]`'s
> annotation suggests (instead of the more general help line).
>
> Link: https://github.com/rust-lang/rust/issues/104253 [1]
> Link: https://github.com/rust-lang/rust/pull/112529 [2]
> Signed-off-by: Miguel Ojeda <ojeda@kernel.org>

Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Re: [PATCH 1/2] rust: arc: add explicit `drop()` around `Box::from_raw()`
Posted by Andreas Hindborg 2 years, 3 months ago
Miguel Ojeda <ojeda@kernel.org> writes:

> `Box::from_raw()` is `#[must_use]`, which means the result cannot
> go unused.
>
> In Rust 1.71.0, this was not detected because the block expression
> swallows the diagnostic [1]:
>
>     unsafe { Box::from_raw(self.ptr.as_ptr()) };
>
> It would have been detected, however, if the line had been instead:
>
>     unsafe { Box::from_raw(self.ptr.as_ptr()); }
>
> i.e. the semicolon being inside the `unsafe` block, rather than
> outside.
>
> In Rust 1.72.0, the compiler started warning about this [2], so
> without this patch we will get:
>
>         error: unused return value of `alloc::boxed::Box::<T>::from_raw` that must be used
>         --> rust/kernel/sync/arc.rs:302:22
>         |
>     302 |             unsafe { Box::from_raw(self.ptr.as_ptr()) };
>         |                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>         |
>         = note: call `drop(Box::from_raw(ptr))` if you intend to drop the `Box`
>         = note: `-D unused-must-use` implied by `-D warnings`
>     help: use `let _ = ...` to ignore the resulting value
>         |
>     302 |             unsafe { let _ = Box::from_raw(self.ptr.as_ptr()); };
>         |                      +++++++                                 +
>
> Thus add an add an explicit `drop()` as the `#[must_use]`'s
> annotation suggests (instead of the more general help line).
>
> Link: https://protect2.fireeye.com/v1/url?k=9e9028e1-ff1b3dd7-9e91a3ae-74fe485cbff1-f2bfda5f7702c68e&q=1&e=66bd90a6-86db-48e7-8538-c118c3f45baa&u=https%3A%2F%2Fgithub.com%2Frust-lang%2Frust%2Fissues%2F104253 [1]
> Link: https://protect2.fireeye.com/v1/url?k=89300613-e8bb1325-89318d5c-74fe485cbff1-fcf76e46b008b25a&q=1&e=66bd90a6-86db-48e7-8538-c118c3f45baa&u=https%3A%2F%2Fgithub.com%2Frust-lang%2Frust%2Fpull%2F112529 [2]
> Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
> ---

Reviewed-by: Andreas Hindborg <a.hindborg@samsung.com>

>  rust/kernel/sync/arc.rs | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs
> index 172f563976a9..4c14d540a581 100644
> --- a/rust/kernel/sync/arc.rs
> +++ b/rust/kernel/sync/arc.rs
> @@ -299,7 +299,7 @@ fn drop(&mut self) {
>              // The count reached zero, we must free the memory.
>              //
>              // SAFETY: The pointer was initialised from the result of `Box::leak`.
> -            unsafe { Box::from_raw(self.ptr.as_ptr()) };
> +            unsafe { drop(Box::from_raw(self.ptr.as_ptr())) };
>          }
>      }
>  }
Re: [PATCH 1/2] rust: arc: add explicit `drop()` around `Box::from_raw()`
Posted by Martin Rodriguez Reboredo 2 years, 3 months ago
On 8/23/23 13:02, Miguel Ojeda wrote:
> `Box::from_raw()` is `#[must_use]`, which means the result cannot
> go unused.
> 
> In Rust 1.71.0, this was not detected because the block expression
> swallows the diagnostic [1]:
> 
>      unsafe { Box::from_raw(self.ptr.as_ptr()) };
> 
> It would have been detected, however, if the line had been instead:
> 
>      unsafe { Box::from_raw(self.ptr.as_ptr()); }
> 
> i.e. the semicolon being inside the `unsafe` block, rather than
> outside.
> 
> In Rust 1.72.0, the compiler started warning about this [2], so
> without this patch we will get:
> 
>          error: unused return value of `alloc::boxed::Box::<T>::from_raw` that must be used
>          --> rust/kernel/sync/arc.rs:302:22
>          |
>      302 |             unsafe { Box::from_raw(self.ptr.as_ptr()) };
>          |                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>          |
>          = note: call `drop(Box::from_raw(ptr))` if you intend to drop the `Box`
>          = note: `-D unused-must-use` implied by `-D warnings`
>      help: use `let _ = ...` to ignore the resulting value
>          |
>      302 |             unsafe { let _ = Box::from_raw(self.ptr.as_ptr()); };
>          |                      +++++++                                 +
> 
> Thus add an add an explicit `drop()` as the `#[must_use]`'s
> annotation suggests (instead of the more general help line).
> 
> Link: https://github.com/rust-lang/rust/issues/104253 [1]
> Link: https://github.com/rust-lang/rust/pull/112529 [2]
> Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
> ---
> [...]
Reviewed-by: Martin Rodriguez Reboredo <yakoyoku@gmail.com>
Re: [PATCH 1/2] rust: arc: add explicit `drop()` around `Box::from_raw()`
Posted by Gary Guo 2 years, 3 months ago
On Wed, 23 Aug 2023 18:02:42 +0200
Miguel Ojeda <ojeda@kernel.org> wrote:

> `Box::from_raw()` is `#[must_use]`, which means the result cannot
> go unused.
> 
> In Rust 1.71.0, this was not detected because the block expression
> swallows the diagnostic [1]:
> 
>     unsafe { Box::from_raw(self.ptr.as_ptr()) };
> 
> It would have been detected, however, if the line had been instead:
> 
>     unsafe { Box::from_raw(self.ptr.as_ptr()); }
> 
> i.e. the semicolon being inside the `unsafe` block, rather than
> outside.
> 
> In Rust 1.72.0, the compiler started warning about this [2], so
> without this patch we will get:
> 
>         error: unused return value of `alloc::boxed::Box::<T>::from_raw` that must be used
>         --> rust/kernel/sync/arc.rs:302:22  
>         |
>     302 |             unsafe { Box::from_raw(self.ptr.as_ptr()) };
>         |                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>         |
>         = note: call `drop(Box::from_raw(ptr))` if you intend to drop the `Box`
>         = note: `-D unused-must-use` implied by `-D warnings`
>     help: use `let _ = ...` to ignore the resulting value
>         |
>     302 |             unsafe { let _ = Box::from_raw(self.ptr.as_ptr()); };
>         |                      +++++++                                 +
> 
> Thus add an add an explicit `drop()` as the `#[must_use]`'s
> annotation suggests (instead of the more general help line).
> 
> Link: https://github.com/rust-lang/rust/issues/104253 [1]
> Link: https://github.com/rust-lang/rust/pull/112529 [2]
> Signed-off-by: Miguel Ojeda <ojeda@kernel.org>

Reviewed-by: Gary Guo <gary@garyguo.net>

> ---
>  rust/kernel/sync/arc.rs | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs
> index 172f563976a9..4c14d540a581 100644
> --- a/rust/kernel/sync/arc.rs
> +++ b/rust/kernel/sync/arc.rs
> @@ -299,7 +299,7 @@ fn drop(&mut self) {
>              // The count reached zero, we must free the memory.
>              //
>              // SAFETY: The pointer was initialised from the result of `Box::leak`.
> -            unsafe { Box::from_raw(self.ptr.as_ptr()) };
> +            unsafe { drop(Box::from_raw(self.ptr.as_ptr())) };
>          }
>      }
>  }
Re: [PATCH 1/2] rust: arc: add explicit `drop()` around `Box::from_raw()`
Posted by Björn Roy Baron 2 years, 3 months ago
On Wednesday, August 23rd, 2023 at 18:02, Miguel Ojeda <ojeda@kernel.org> wrote:

> `Box::from_raw()` is `#[must_use]`, which means the result cannot
> go unused.
> 
> In Rust 1.71.0, this was not detected because the block expression
> swallows the diagnostic [1]:
> 
>     unsafe { Box::from_raw(self.ptr.as_ptr()) };
> 
> It would have been detected, however, if the line had been instead:
> 
>     unsafe { Box::from_raw(self.ptr.as_ptr()); }
> 
> i.e. the semicolon being inside the `unsafe` block, rather than
> outside.
> 
> In Rust 1.72.0, the compiler started warning about this [2], so
> without this patch we will get:
> 
>         error: unused return value of `alloc::boxed::Box::<T>::from_raw` that must be used
>         --> rust/kernel/sync/arc.rs:302:22
>         |
>     302 |             unsafe { Box::from_raw(self.ptr.as_ptr()) };
>         |                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>         |
>         = note: call `drop(Box::from_raw(ptr))` if you intend to drop the `Box`
>         = note: `-D unused-must-use` implied by `-D warnings`
>     help: use `let _ = ...` to ignore the resulting value
>         |
>     302 |             unsafe { let _ = Box::from_raw(self.ptr.as_ptr()); };
>         |                      +++++++                                 +
> 
> Thus add an add an explicit `drop()` as the `#[must_use]`'s
> annotation suggests (instead of the more general help line).
> 
> Link: https://github.com/rust-lang/rust/issues/104253 [1]
> Link: https://github.com/rust-lang/rust/pull/112529 [2]
> Signed-off-by: Miguel Ojeda <ojeda@kernel.org>

Reviewed-by: Björn Roy Baron <bjorn3_gh@protonmail.com>

> ---
>  rust/kernel/sync/arc.rs | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs
> index 172f563976a9..4c14d540a581 100644
> --- a/rust/kernel/sync/arc.rs
> +++ b/rust/kernel/sync/arc.rs
> @@ -299,7 +299,7 @@ fn drop(&mut self) {
>              // The count reached zero, we must free the memory.
>              //
>              // SAFETY: The pointer was initialised from the result of `Box::leak`.
> -            unsafe { Box::from_raw(self.ptr.as_ptr()) };
> +            unsafe { drop(Box::from_raw(self.ptr.as_ptr())) };
>          }
>      }
>  }
> --
> 2.42.0