fix: out-of-bounds error with empty Backstack and L (forward)

**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 "<unknown>", line 0, in __mh_execute_header
  File "<unknown>", line 0, in __mh_execute_header
  File "<unknown>", line 0, in __mh_execute_header
  File "<unknown>", line 0, in __mh_execute_header
  File "<unknown>", line 0, in __mh_execute_header
  File "<unknown>", line 0, in __mh_execute_header
  File "<unknown>", line 0, in __mh_execute_header
  File "<unknown>", line 0, in __mh_execute_header
  File "<unknown>", line 0, in __mh_execute_header
  File "<unknown>", line 0, in __mh_execute_header
  File "<unknown>", line 0, in __mh_execute_header
  File "<unknown>", line 0, in __mh_execute_header
  File "<unknown>", line 0, in __mh_execute_header
  File "<unknown>", line 0, in __mh_execute_header
  File "<unknown>", line 0, in __mh_execute_header
  File "<unknown>", 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.
This commit is contained in:
Mika Vilpas 2025-06-16 21:16:56 +03:00
parent a0ab614108
commit 6957b1aca4

View file

@ -41,7 +41,7 @@ impl<T: Eq + Clone> Backstack<T> {
}
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<u32> = Backstack::default();
assert_eq!(bs.shift_forward(), None);
bs.push(&1);
assert_eq!(bs.stack[bs.cursor], 1);