rust: alloc: add missing invariant in Vec::set_len()
authorDanilo Krummrich <dakr@kernel.org>
Sat, 15 Mar 2025 15:43:02 +0000 (16:43 +0100)
committerDanilo Krummrich <dakr@kernel.org>
Mon, 7 Apr 2025 12:02:56 +0000 (14:02 +0200)
When setting a new length, we have to justify that the set length
represents the exact number of elements stored in the vector.

Reviewed-by: Benno Lossin <benno.lossin@proton.me>
Reported-by: Alice Ryhl <aliceryhl@google.com>
Closes: https://lore.kernel.org/rust-for-linux/20250311-iov-iter-v1-4-f6c9134ea824@google.com
Fixes: 2aac4cd7dae3 ("rust: alloc: implement kernel `Vec` type")
Link: https://lore.kernel.org/r/20250315154436.65065-2-dakr@kernel.org
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
rust/kernel/alloc/kvec.rs

index ae9d072741cedbb34bed0be0c20cc75472aa53be..b01dabfe35aa44830b4e6255a4f8531ff4ce42f2 100644 (file)
@@ -193,6 +193,9 @@ where
     #[inline]
     pub unsafe fn set_len(&mut self, new_len: usize) {
         debug_assert!(new_len <= self.capacity());
+
+        // INVARIANT: By the safety requirements of this method `new_len` represents the exact
+        // number of elements stored within `self`.
         self.len = new_len;
     }