From 6957b1aca45c82e976d50c8dc13911284d44743d Mon Sep 17 00:00:00 2001 From: Mika Vilpas Date: Mon, 16 Jun 2025 21:16:56 +0300 Subject: [PATCH] fix: out-of-bounds error with empty Backstack and `L` (`forward`) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit **Issue:** After starting yazi, and pressing `L` to go forward to the next visited directory, yazi crashes with the following message: ```rs ❯ RUST_BACKTRACE=full yazi Backtrace (most recent call first): File "", line 0, in __mh_execute_header File "", line 0, in __mh_execute_header File "", line 0, in __mh_execute_header File "", line 0, in __mh_execute_header File "", line 0, in __mh_execute_header File "", line 0, in __mh_execute_header File "", line 0, in __mh_execute_header File "", line 0, in __mh_execute_header File "", line 0, in __mh_execute_header File "", line 0, in __mh_execute_header File "", line 0, in __mh_execute_header File "", line 0, in __mh_execute_header File "", line 0, in __mh_execute_header File "", line 0, in __mh_execute_header File "", line 0, in __mh_execute_header File "", line 0, in __mh_execute_header The application panicked (crashed). index out of bounds: the len is 0 but the index is 1 in yazi-core/src/tab/backstack.rs, line 48 thread: main ``` **Solution:** Fix the out-of-bounds error by accounting for the case where the `Backstack` is empty. --- yazi-core/src/tab/backstack.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/yazi-core/src/tab/backstack.rs b/yazi-core/src/tab/backstack.rs index bb9ffd5e..4bd8afa6 100644 --- a/yazi-core/src/tab/backstack.rs +++ b/yazi-core/src/tab/backstack.rs @@ -41,7 +41,7 @@ impl Backstack { } pub fn shift_forward(&mut self) -> Option<&T> { - if self.cursor + 1 == self.stack.len() { + if self.cursor + 1 >= self.stack.len() { None } else { self.cursor += 1; @@ -56,7 +56,9 @@ mod tests { #[test] fn test_backstack() { - let mut bs = Backstack::default(); + let mut bs: Backstack = Backstack::default(); + assert_eq!(bs.shift_forward(), None); + bs.push(&1); assert_eq!(bs.stack[bs.cursor], 1);