Skip to contentSkip to conversation

rust: alloc: use `if` instead of `match` in VecExt<T>::reserve()

PATCH Applied Danilo Krummrich 2 participants 2 messages May 7, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rust: alloc: use `if` instead of `match` in VecExt<T>::reserve()

In commit 1161057f53f6 ("rust: alloc: fix dangling pointer in VecExt<T>::reserve()") the check for zero of a vector's capacity has been implemented using a match statement. Using an if statement is the preferred style, hence change that.

---
Sending a separate patch since v3 has been merged [1] already.

Please feel free to melt this patch into the original commit.

[1] https://lore.kernel.org/rust-for-linux/CANiq72nSQAE7c8ypWQA__BMoaBYqiEjXvHpuzn-=CsCky9uu7A@mail.gmail.com/

---
 rust/kernel/alloc/vec_ext.rs | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
rust/kernel/alloc/vec_ext.rs +1 -4
index 8f16b919bf4d..e9a81052728a 100644
@@ -141,10 +141,7 @@ fn reserve(&mut self, additional: usize, flags: Flags) -> Result<(), AllocError>
         // `krealloc_aligned`. A `Vec<T>`'s `ptr` value is not guaranteed to be NULL and might be
         // dangling after being created with `Vec::new`. Instead, we can rely on `Vec<T>`'s capacity
         // to be zero if no memory has been allocated yet.
-        let ptr = match cap {
-            0 => ptr::null_mut(),
-            _ => old_ptr,
-        };
+        let ptr = if cap == 0 { ptr::null_mut() } else { old_ptr };
 
         // SAFETY: `ptr` is valid because it's either NULL or comes from a previous call to
         // `krealloc_aligned`. We also verified that the type is not a ZST.

base-commit: 1161057f53f643b63060835d46461ef09e8abe2f

Signature · 1 line
2.44.0
Applied
#1 lore raw
Quoted 8 lines · Danilo Krummrich

In commit 1161057f53f6 ("rust: alloc: fix dangling pointer in VecExt<T>::reserve()") the check for zero of a vector's capacity has been implemented using a match statement. Using an if statement is the preferred style, hence change that.

Applied to rust-next as fixup for "[PATCH v3] rust: alloc: fix dangling pointer in VecExt<T>::reserve()" -- thanks!

Cheers,
Miguel