Skip to content

Commit

Permalink
fix(location_reader/life_path): index out of bounds
Browse files Browse the repository at this point in the history
close #1
  • Loading branch information
LynMoe committed Dec 21, 2023
1 parent 6557af4 commit 8825824
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/location_reader/life_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ impl LocationReaderLiftPath {
impl LocationReaderBase for LocationReaderLiftPath {
fn get_location(&mut self, timestamp: i32) -> Option<LocationReaderResult> {
let timestamp = timestamp - self.param.time_offset;
let pos = self.find_closest_position(timestamp)?;
let pos = self.find_closest_position(timestamp);
if pos.is_none() {
return None;
}
let pos = pos.unwrap();

self.file.seek(SeekFrom::Start(pos.0)).unwrap();
let mut rdr = ReaderBuilder::new()
Expand Down Expand Up @@ -108,6 +112,10 @@ impl LocationReaderBase for LocationReaderLiftPath {

impl LocationReaderLiftPath {
fn find_closest_position(&self, timestamp: i32) -> Option<(u64, u64)> {
if self.index.len() <= 2 {
return None;
}

let mut left = 0;
let mut right = self.index.len() - 1;

Expand All @@ -120,9 +128,11 @@ impl LocationReaderLiftPath {
}
}

if left == 0 {
return Some((self.index[0].1, self.index[1].1));
}

let diff2 = (timestamp - self.index[left - 1].0).abs();

let mut result = (left, left - 1);

if left + 1 < self.index.len() {
Expand Down

0 comments on commit 8825824

Please sign in to comment.