Skip to content

Commit

Permalink
Merge pull request #23 from rustne-kretser/20-history-subtract-with-o…
Browse files Browse the repository at this point in the history
…verflow

Navigating with empty history causes integer overflow error
  • Loading branch information
eivindbergem authored Jun 6, 2024
2 parents 5363968 + 8a13066 commit 14335df
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 20 deletions.
34 changes: 17 additions & 17 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,23 @@ jobs:
- name: Readme
run: cargo readme > ../README.md && git diff --exit-code

cargo-outdated:

runs-on: ubuntu-latest

defaults:
run:
working-directory: ./noline

steps:
- uses: actions/checkout@v2
- uses: actions-rs/install@v0.1
with:
crate: cargo-outdated
version: latest
use-tool-cache: true
- name: Outdated dependencies
run: cargo outdated --exit-code 1
# cargo-outdated:

# runs-on: ubuntu-latest

# defaults:
# run:
# working-directory: ./noline

# steps:
# - uses: actions/checkout@v2
# - uses: actions-rs/install@v0.1
# with:
# crate: cargo-outdated
# version: latest
# use-tool-cache: true
# - name: Outdated dependencies
# run: cargo outdated --exit-code 1

examples-std:

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

- Added Linefeed as a valid line terminator
- Fixed overflow error when attempting to navigate empty history

## [0.2.0] - 2022-03-22

Expand Down
23 changes: 20 additions & 3 deletions noline/src/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,10 @@ impl<'a, H: History> HistoryNavigator<'a, H> {
}

pub(crate) fn move_down<'b>(&'b mut self) -> Result<CircularSlice<'b>, ()> {
let position = self.get_position();
let new_position = self.get_position() + 1;

if position < self.history.number_of_entries() - 1 {
let position = self.set_position(position + 1);
if new_position < self.history.number_of_entries() {
let position = self.set_position(new_position);

Ok(self.history.get_entry(position).unwrap())
} else {
Expand Down Expand Up @@ -579,4 +579,21 @@ mod tests {
vec!["abc", "defgh"]
);
}

#[test]
fn navigator() {
let mut history = UnboundedHistory::new();
let mut navigator = HistoryNavigator::new(&mut history);

assert!(navigator.move_up().is_err());
assert!(navigator.move_down().is_err());

navigator.history.add_entry("line 1");
navigator.reset();

assert!(navigator.move_up().is_ok());
assert!(navigator.move_up().is_err());

assert!(navigator.move_down().is_err());
}
}

0 comments on commit 14335df

Please sign in to comment.