Skip to content

Commit

Permalink
Fix overflow when the scope is pop while the index is 0 and add a tes…
Browse files Browse the repository at this point in the history
…t for it
  • Loading branch information
Sellig6792 committed Dec 31, 2022
1 parent a5b13b2 commit 5bc82ef
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/evaluation/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,13 @@ mod tests {
brainfuck.evaluate(None, Some(false));
assert_eq!(String::from_utf8(brainfuck.output_buffer).unwrap(), "AA");
}

#[test]
fn test_overflow_scope_pop() {
let program = String::from("{´}=");
let mut parser = ast::Parser::new(program);
let instructions = parser.parse();
let mut evaluator = Evaluator::new(instructions);
evaluator.evaluate(None, Some(false));
}
}
4 changes: 3 additions & 1 deletion src/evaluation/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ where

pub fn pop(&mut self) {
self.scopes.pop();
self.scope_index -= 1;
if self.scope_index > 0 {
self.scope_index -= 1;
}
}
}

Expand Down

0 comments on commit 5bc82ef

Please sign in to comment.