Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix panic when history size set to 0 #653

Merged
merged 2 commits into from
Jan 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading