From fc9fc1a04858998d3c6e96cbe473f2dbd62b6eb3 Mon Sep 17 00:00:00 2001 From: ddidderr Date: Fri, 16 Jun 2023 16:57:25 +0200 Subject: [PATCH] (fix) unsafe array access in skipf and skipb skipf was accessed at index 81 (len only 81) skipb was accessed at index -1 (u64::MAX) I still don't know, why the skipb case didn't crash. --- src/main.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index ab091b4..0208f17 100644 --- a/src/main.rs +++ b/src/main.rs @@ -51,7 +51,7 @@ impl SField { fixed_count } - let mut skipf = vec![0; NUM_FIELDS]; + let mut skipf = vec![0; NUM_FIELDS + 1]; for (idx, nr) in field.iter().enumerate() { match nr { 0 => skipf[idx] = *nr, @@ -59,11 +59,11 @@ impl SField { } } - let mut skipb = vec![0; NUM_FIELDS]; + let mut skipb = vec![0; NUM_FIELDS + 1]; for (idx, nr) in field.iter().enumerate().rev() { match nr { - 0 => skipb[idx] = *nr, - _ => skipb[idx] = find_fixed_streak_backward(idx, &field), + 0 => skipb[idx + 1] = *nr, + _ => skipb[idx + 1] = find_fixed_streak_backward(idx, &field), } } @@ -257,7 +257,7 @@ impl SField { } fn prev(&mut self) -> bool { - let new_pos = self.pos - 1 - unsafe { *self.skipb.get_unchecked(self.pos - 1) as usize }; + let new_pos = self.pos - 1 - unsafe { *self.skipb.get_unchecked(self.pos) as usize }; if new_pos >= NUM_FIELDS { return false;