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: <20251107050700.1086059-1-m18080292938@163.com>
Date: Fri,  7 Nov 2025 05:06:56 +0000
From: Hang Shu <m18080292938@....com>
To: ojeda@...nel.org
Cc: Hang Shu <hangshu847@...il.com>,
	Alex Gaynor <alex.gaynor@...il.com>,
	Boqun Feng <boqun.feng@...il.com>,
	Gary Guo <gary@...yguo.net>,
	Björn Roy Baron <bjorn3_gh@...tonmail.com>,
	Benno Lossin <lossin@...nel.org>,
	Andreas Hindborg <a.hindborg@...nel.org>,
	Alice Ryhl <aliceryhl@...gle.com>,
	Trevor Gross <tmgross@...ch.edu>,
	Danilo Krummrich <dakr@...nel.org>,
	Hang Shu <m18080292938@....com>,
	Charalampos Mitrodimas <charmitro@...teo.net>,
	Borys Tyran <borys.tyran@...tonmail.com>,
	Onur Özkan <work@...rozkan.dev>,
	Daniel Sedlak <daniel@...lak.dev>,
	Tamir Duberstein <tamird@...il.com>,
	Matt Gilbride <mattgilbride@...gle.com>,
	rust-for-linux@...r.kernel.org,
	linux-kernel@...r.kernel.org
Subject: [PATCH] rust: rbtree: fix cursor method lifetimes to match tree lifetime

From: Hang Shu <hangshu847@...il.com>

The returned keys and values of cursor methods should be bound by
the lifetime of the rbtree itself ('a), not the lifetime of the cursor.

Without this adjustment, examples like the following fail to compile:

fn test_rbtree_cursor(rbtree: &mut RBTree<i32, i32>) -> &i32 {
    rbtree.try_create_and_insert(1, 1, GFP_KERNEL).unwrap();
    let mut cursor = rbtree.cursor_front().unwrap();
    // compile error
    // cannot return value referencing local variable `cursor`
    cursor.peek_next().unwrap().1
}

This modification ensures that references to tree elements remain valid
independently of the cursor's scope,
aligning with the actual lifetime dependencies in the data structure.

The changes will be applied to multiple similar methods
throughout the Cursor implementation to maintain consistency.

Fixes: 98c14e40e07a ("rust: rbtree: add cursor")
Signed-off-by: Hang Shu <hangshu847@...il.com>
---
 rust/kernel/rbtree.rs | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/rust/kernel/rbtree.rs b/rust/kernel/rbtree.rs
index 9e178dacddf1..702a1b6ef7a9 100644
--- a/rust/kernel/rbtree.rs
+++ b/rust/kernel/rbtree.rs
@@ -742,7 +742,7 @@ unsafe impl<'a, K: Sync, V: Sync> Sync for Cursor<'a, K, V> {}
 
 impl<'a, K, V> Cursor<'a, K, V> {
     /// The current node
-    pub fn current(&self) -> (&K, &V) {
+    pub fn current(&self) -> (&'a K, &'a V) {
         // SAFETY:
         // - `self.current` is a valid node by the type invariants.
         // - We have an immutable reference by the function signature.
@@ -750,7 +750,7 @@ pub fn current(&self) -> (&K, &V) {
     }
 
     /// The current node, with a mutable value
-    pub fn current_mut(&mut self) -> (&K, &mut V) {
+    pub fn current_mut(&mut self) -> (&'a K, &'a mut V) {
         // SAFETY:
         // - `self.current` is a valid node by the type invariants.
         // - We have an mutable reference by the function signature.
@@ -831,16 +831,16 @@ fn mv(self, direction: Direction) -> Option<Self> {
     }
 
     /// Access the previous node without moving the cursor.
-    pub fn peek_prev(&self) -> Option<(&K, &V)> {
+    pub fn peek_prev(&self) -> Option<(&'a K, &'a V)> {
         self.peek(Direction::Prev)
     }
 
     /// Access the next node without moving the cursor.
-    pub fn peek_next(&self) -> Option<(&K, &V)> {
+    pub fn peek_next(&self) -> Option<(&'a K, &'a V)> {
         self.peek(Direction::Next)
     }
 
-    fn peek(&self, direction: Direction) -> Option<(&K, &V)> {
+    fn peek(&self, direction: Direction) -> Option<(&'a K, &'a V)> {
         self.get_neighbor_raw(direction).map(|neighbor| {
             // SAFETY:
             // - `neighbor` is a valid tree node.
@@ -850,16 +850,16 @@ fn peek(&self, direction: Direction) -> Option<(&K, &V)> {
     }
 
     /// Access the previous node mutably without moving the cursor.
-    pub fn peek_prev_mut(&mut self) -> Option<(&K, &mut V)> {
+    pub fn peek_prev_mut(&mut self) -> Option<(&'a K, &'a mut V)> {
         self.peek_mut(Direction::Prev)
     }
 
     /// Access the next node mutably without moving the cursor.
-    pub fn peek_next_mut(&mut self) -> Option<(&K, &mut V)> {
+    pub fn peek_next_mut(&mut self) -> Option<(&'a K, &'a mut V)> {
         self.peek_mut(Direction::Next)
     }
 
-    fn peek_mut(&mut self, direction: Direction) -> Option<(&K, &mut V)> {
+    fn peek_mut(&mut self, direction: Direction) -> Option<(&'a K, &'a mut V)> {
         self.get_neighbor_raw(direction).map(|neighbor| {
             // SAFETY:
             // - `neighbor` is a valid tree node.
-- 
2.43.0


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ