levels: index out of bounds

This commit is contained in:
Aravinth Manivannan 2021-06-07 14:23:43 +05:30
parent 68f95f99c2
commit d0b3416a54
Signed by: realaravinth
GPG Key ID: AD9F0F08E855ED88
2 changed files with 9 additions and 5 deletions

View File

@ -183,14 +183,14 @@ impl Defense {
/// tighten up defense. Increases defense level by a factor of one. /// tighten up defense. Increases defense level by a factor of one.
/// When defense is at max level, calling this method will have no effect /// When defense is at max level, calling this method will have no effect
pub fn tighten_up(&mut self) { pub fn tighten_up(&mut self) {
if self.current_visitor_threshold != self.levels.len() - 1 { if self.current_visitor_threshold < self.levels.len() - 1 {
self.current_visitor_threshold += 1; self.current_visitor_threshold += 1;
} }
} }
/// Loosen up defense. Decreases defense level by a factor of one. /// Loosen up defense. Decreases defense level by a factor of one.
/// When defense is at the lowest level, calling this method will have no effect. /// When defense is at the lowest level, calling this method will have no effect.
pub fn loosen_up(&mut self) { pub fn loosen_up(&mut self) {
if self.current_visitor_threshold != 0 { if self.current_visitor_threshold > 0 {
self.current_visitor_threshold -= 1; self.current_visitor_threshold -= 1;
} }
} }
@ -206,8 +206,12 @@ impl Defense {
} }
/// Get current level's visitor threshold /// Get current level's visitor threshold
pub fn visitor_threshold(&self) -> u32 { pub fn visitor_threshold(&self) -> Option<u32> {
self.levels[self.current_visitor_threshold].visitor_threshold if let Some(level) = self.levels.get(self.current_visitor_threshold) {
Some(level.visitor_threshold)
} else {
None
}
} }
} }

View File

@ -83,7 +83,7 @@ impl MCaptcha {
#[inline] #[inline]
pub fn add_visitor(&mut self) { pub fn add_visitor(&mut self) {
self.visitor_threshold += 1; self.visitor_threshold += 1;
if self.visitor_threshold > self.defense.visitor_threshold() { if self.visitor_threshold > self.defense.visitor_threshold().unwrap() {
self.defense.tighten_up(); self.defense.tighten_up();
} else { } else {
self.defense.loosen_up(); self.defense.loosen_up();