lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <20260131080614.264242-1-shivamklr@cock.li>
Date: Sat, 31 Jan 2026 13:36:13 +0530
From: Shivam Kalra <shivamklr@...k.li>
To: dakr@...nel.org,
	cmllamas@...gle.com,
	gregkh@...uxfoundation.org
Cc: aliceryhl@...gle.com,
	rust-for-linux@...r.kernel.org,
	linux-kernel@...r.kernel.org,
	Shivam Kalra <shivamklr@...k.li>
Subject: [PATCH v1 2/3] rust: alloc: add KUnit tests for Vec shrinking

Add comprehensive KUnit tests for Vec::shrink_to() and
Vec::shrink_to_fit() to verify that capacity is reduced correctly.

The tests cover:
- shrinking from excess capacity
- correct handling of empty vectors
- no-op behavior when capacity is already optimal
- respecting the minimum capacity limit for shrink_to()
- no growth when the requested minimum exceeds current capacity

Testing:
- KUnit tests pass locally (command above).
- Built with CONFIG_RUST=y on x86_64 (user-mode testing via --arch um).

Signed-off-by: Shivam Kalra <shivamklr@...k.li>
---
 rust/kernel/alloc/kvec.rs | 65 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 65 insertions(+)

diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs
index 9c02734ced88f..5ab78d6c03e40 100644
--- a/rust/kernel/alloc/kvec.rs
+++ b/rust/kernel/alloc/kvec.rs
@@ -1509,4 +1509,69 @@ fn add(value: &mut [bool]) {
             func.push_within_capacity(false).unwrap();
         }
     }
+
+    #[test]
+    fn test_shrink_to_fit() {
+        // Create a vector with excess capacity.
+        let mut v: KVec<u32> = KVec::with_capacity(100, GFP_KERNEL).unwrap();
+        v.push(1, GFP_KERNEL).unwrap();
+        v.push(2, GFP_KERNEL).unwrap();
+        v.push(3, GFP_KERNEL).unwrap();
+
+        assert!(v.capacity() >= 100);
+        assert_eq!(v.len(), 3);
+
+        // Shrink with 0 (equivalent to shrink_to_fit).
+        v.shrink_to(0, GFP_KERNEL).unwrap();
+
+        // Capacity should now equal length.
+        assert_eq!(v.capacity(), 3);
+        assert_eq!(v.len(), 3);
+        assert_eq!(&v, &[1, 2, 3]);
+
+        // Shrink empty vector.
+        let mut v: KVec<u32> = KVec::with_capacity(50, GFP_KERNEL).unwrap();
+        v.shrink_to(0, GFP_KERNEL).unwrap();
+        assert_eq!(v.capacity(), 0);
+
+        // Shrink already optimal (no-op).
+        let mut v: KVec<u32> = KVec::new();
+        v.push(1, GFP_KERNEL).unwrap();
+        v.shrink_to(0, GFP_KERNEL).unwrap();
+        assert!(v.capacity() >= 1);
+    }
+
+    #[test]
+    fn test_shrink_to_min() {
+        let mut v: KVec<u32> = KVec::with_capacity(100, GFP_KERNEL).unwrap();
+        for i in 0..10 {
+            v.push(i, GFP_KERNEL).unwrap();
+        }
+        assert_eq!(v.len(), 10);
+        assert!(v.capacity() >= 100);
+
+        // Shrink to a specific minimum higher than len.
+        v.shrink_to(50, GFP_KERNEL).unwrap();
+        assert!(v.capacity() >= 50);
+        assert!(v.capacity() < 100); // Should have shrunk
+        assert_eq!(v.len(), 10);
+        assert_eq!(v[0], 0);
+        assert_eq!(v[9], 9);
+
+        // Shrink to a minimum lower than len (should stop at len).
+        v.shrink_to(5, GFP_KERNEL).unwrap();
+        assert_eq!(v.capacity(), 10);
+        assert_eq!(v.len(), 10);
+    }
+
+    #[test]
+    fn test_shrink_to_no_growth() {
+        let mut v: KVec<u32> = KVec::with_capacity(10, GFP_KERNEL).unwrap();
+        v.push(1, GFP_KERNEL).unwrap();
+        let cap = v.capacity();
+
+        // Requesting larger capacity should NOT grow the vector.
+        v.shrink_to(100, GFP_KERNEL).unwrap();
+        assert_eq!(v.capacity(), cap);
+    }
 }
-- 
2.43.0


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ