Skip to content

Commit c5e8f08

Browse files
authored
Fix panic when primitive equality fn not found (#16)
If a primitive equality function cannot be found it returns nil, but currently the code does not check for this condition. This fixes it and adds a test that panics without the change.
1 parent 777ad6e commit c5e8f08

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

evaluate.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package bexpr
22

33
import (
44
"encoding/json"
5+
"errors"
56
"fmt"
67
"reflect"
78
"regexp"
@@ -89,6 +90,9 @@ func doMatchMatches(expression *grammar.MatchExpression, value reflect.Value) (b
8990
func doMatchEqual(expression *grammar.MatchExpression, value reflect.Value) (bool, error) {
9091
// NOTE: see preconditions in evaluategrammar.MatchExpressionRecurse
9192
eqFn := primitiveEqualityFn(value.Kind())
93+
if eqFn == nil {
94+
return false, errors.New("unable to find suitable primitive comparison function for matching")
95+
}
9296
matchValue, err := getMatchExprValue(expression, value.Kind())
9397
if err != nil {
9498
return false, fmt.Errorf("error getting match value in expression: %w", err)
@@ -116,6 +120,9 @@ func doMatchIn(expression *grammar.MatchExpression, value reflect.Value) (bool,
116120
return false, fmt.Errorf("error getting match value in expression: %w", err)
117121
}
118122
eqFn := primitiveEqualityFn(itemType.Kind())
123+
if eqFn == nil {
124+
return false, errors.New(`unable to find suitable primitive comparison function for "in" comparison`)
125+
}
119126

120127
for i := 0; i < value.Len(); i++ {
121128
item := value.Index(i)

evaluate_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ var evaluateTests map[string]expressionTest = map[string]expressionTest{
265265
{expression: "Nested.Map contains nope or (Nested.Map contains bar and Nested.Map.bar == `bazel`) or TopInt != 0", result: true, benchQuick: true},
266266
{expression: "Nested.MapOfStructs.one.Foo == 42", result: true},
267267
{expression: "7 in Nested.SliceOfInts", result: true},
268+
{expression: `"/Nested/SliceOfInts" == "7"`, result: false, err: `unable to find suitable primitive comparison function for matching`},
268269
{expression: "Nested.MapOfStructs is empty or (Nested.SliceOfInts contains 7 and 9 in Nested.SliceOfInts)", result: true, benchQuick: true},
269270
{expression: "Nested.SliceOfStructs.0.X == 1", result: true},
270271
{expression: "Nested.SliceOfStructs.0.Y == 4", result: false},

0 commit comments

Comments
 (0)