Fix out of bounds errors in Machine.Pop

This commit is contained in:
Sasha Koshka 2022-08-28 18:22:11 -04:00
parent b4bd86ad1d
commit 850de8a2bf
1 changed files with 4 additions and 0 deletions

View File

@ -224,6 +224,10 @@ func (machine *Machine) Push(word int) {
// Pop pops the last word off of the stack, and returns it, decreasing the stack
// pointer and reallocating the stack if necessary.
func (machine *Machine) Pop() (word int) {
if machine.pointer <= 0 || machine.pointer >= len(machine.stack) {
return
}
word = machine.stack[machine.pointer]
machine.pointer--