Skip to content

Commit

Permalink
Fix panic when history size set to 0 (#653)
Browse files Browse the repository at this point in the history
* Fix file based history when capacity is set to zero

Signed-off-by: Andrei Stan <[email protected]>

* Fix CI

Signed-off-by: Andrei Stan <[email protected]>

---------

Signed-off-by: Andrei Stan <[email protected]>
  • Loading branch information
andreistan26 authored Jan 21, 2024
1 parent 7255741 commit d9db6a8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/history/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,4 +427,17 @@ mod test {

Ok(())
}

#[cfg(not(any(feature = "sqlite", feature = "sqlite-dynlib")))]
#[test]
fn history_size_zero() -> Result<()> {
let mut history = crate::FileBackedHistory::new(0);
history.save(create_item(1, "/home/me", "cd ~/Downloads", 0))?;
assert_eq!(history.count_all()?, 0);
let _ = history.sync();
history.clear()?;
drop(history);

Ok(())
}
}
5 changes: 4 additions & 1 deletion src/history/file_backed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ impl History for FileBackedHistory {
.back()
.map_or(true, |previous| previous != &entry)
&& !entry.is_empty()
&& self.capacity > 0
{
if self.entries.len() == self.capacity {
// History is "full", so we delete the oldest entry first,
Expand Down Expand Up @@ -234,7 +235,9 @@ impl History for FileBackedHistory {
.collect::<std::io::Result<VecDeque<_>>>()?;
if from_file.len() + own_entries.len() > self.capacity {
(
from_file.split_off(from_file.len() - (self.capacity - own_entries.len())),
from_file.split_off(
from_file.len() - (self.capacity.saturating_sub(own_entries.len())),
),
true,
)
} else {
Expand Down

0 comments on commit d9db6a8

Please sign in to comment.