Skip to content

Commit

Permalink
fix: Handle nil dereference in expressions
Browse files Browse the repository at this point in the history
The code will fail if the second arg is `nil`
then the code will fail with a panic.

Fixes: #79

Before only the left arg was checked for `nil` but not the
right arg.

Signed-off-by: Gekko Wrld <gekkowrld@gmail.com>
  • Loading branch information
gekkowrld committed Mar 21, 2024
1 parent 3b2207a commit 226d721
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
2 changes: 1 addition & 1 deletion evaluator/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func Eval(node ast.Node, env *object.Environment) object.Object {
return left
}
right := Eval(node.Right, env)
if isError(right) {
if isError(right) && right != nil {
return right
}
return evalInfixExpression(node.Operator, left, right, node.Token.Line)
Expand Down
3 changes: 3 additions & 0 deletions evaluator/infix.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (
)

func evalInfixExpression(operator string, left, right object.Object, line int) object.Object {
if right == nil {
return newError("Mstari %d: Umekosea hapa", line)
}
if left == nil {
return newError("Mstari %d: Umekosea hapa", line)
}
Expand Down

0 comments on commit 226d721

Please sign in to comment.