From d0b3416a54a65fe090d63d608a761543ba4b3291 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Mon, 7 Jun 2021 14:23:43 +0530 Subject: [PATCH] levels: index out of bounds --- src/defense.rs | 12 ++++++++---- src/mcaptcha.rs | 2 +- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/defense.rs b/src/defense.rs index f127c85..32c39d1 100644 --- a/src/defense.rs +++ b/src/defense.rs @@ -183,14 +183,14 @@ impl Defense { /// tighten up defense. Increases defense level by a factor of one. /// When defense is at max level, calling this method will have no effect 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; } } /// 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. pub fn loosen_up(&mut self) { - if self.current_visitor_threshold != 0 { + if self.current_visitor_threshold > 0 { self.current_visitor_threshold -= 1; } } @@ -206,8 +206,12 @@ impl Defense { } /// Get current level's visitor threshold - pub fn visitor_threshold(&self) -> u32 { - self.levels[self.current_visitor_threshold].visitor_threshold + pub fn visitor_threshold(&self) -> Option { + if let Some(level) = self.levels.get(self.current_visitor_threshold) { + Some(level.visitor_threshold) + } else { + None + } } } diff --git a/src/mcaptcha.rs b/src/mcaptcha.rs index a5919c2..d653c53 100644 --- a/src/mcaptcha.rs +++ b/src/mcaptcha.rs @@ -83,7 +83,7 @@ impl MCaptcha { #[inline] pub fn add_visitor(&mut self) { 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(); } else { self.defense.loosen_up();