Knetic / govaluate

Arbitrary expression evaluation for golang
MIT License
3.7k stars 494 forks source link

Do not panic if parameters argument is null and expression includes a parameter #110

Open renatoathaydes opened 5 years ago

renatoathaydes commented 5 years ago

The following test fails with panic:

func TestEval(t *testing.T) {
    expr, err := govaluate.NewEvaluableExpression("2 * a")
    _, err = expr.Evaluate(nil)
    if err == nil {
        t.Error("Should be an error, but not panic!")
    }
}

I would expect just an error.

Here's the stacktrace:

goroutine 5 [running]:
testing.tRunner.func1(0xc0000ca300)
    /usr/local/go/src/testing/testing.go:792 +0x387
panic(0x1156cc0, 0x12a3890)
    /usr/local/go/src/runtime/panic.go:513 +0x1b9
github.com/Knetic/govaluate.makeParameterStage.func1(0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
    /Users/renato/go/pkg/mod/github.com/!knetic/govaluate@v3.0.0+incompatible/evaluationStage.go:217 +0x2e
github.com/Knetic/govaluate.EvaluableExpression.evaluateStage(0x118a9c1, 0x22, 0x1, 0xc00005c420, 0x3, 0x4, 0xc0000ac370, 0x11808e8, 0x5, 0xc0000ac320, ...)
    /Users/renato/go/pkg/mod/github.com/!knetic/govaluate@v3.0.0+incompatible/EvaluableExpression.go:228 +0x16a
github.com/Knetic/govaluate.EvaluableExpression.evaluateStage(0x118a9c1, 0x22, 0x1, 0xc00005c420, 0x3, 0x4, 0xc0000ac370, 0x11808e8, 0x5, 0xc0000ac370, ...)
    /Users/renato/go/pkg/mod/github.com/!knetic/govaluate@v3.0.0+incompatible/EvaluableExpression.go:201 +0x46d
github.com/Knetic/govaluate.EvaluableExpression.Eval(0x118a9c1, 0x22, 0x1, 0xc00005c420, 0x3, 0x4, 0xc0000ac370, 0x11808e8, 0x5, 0x0, ...)
    /Users/renato/go/pkg/mod/github.com/!knetic/govaluate@v3.0.0+incompatible/EvaluableExpression.go:159 +0xb3
github.com/Knetic/govaluate.EvaluableExpression.Evaluate(0x118a9c1, 0x22, 0x1, 0xc00005c420, 0x3, 0x4, 0xc0000ac370, 0x11808e8, 0x5, 0x0, ...)
    /Users/renato/go/pkg/mod/github.com/!knetic/govaluate@v3.0.0+incompatible/EvaluableExpression.go:134 +0x119
github.com/renatoathaydes/magnanimous/tests.TestEval(0xc0000ca300)
    /Users/renato/programming/projects/go/magnanimous/tests/eval_test.go:35 +0x86
testing.tRunner(0xc0000ca300, 0x118e4c8)
    /usr/local/go/src/testing/testing.go:827 +0xbf
created by testing.(*T).Run
    /usr/local/go/src/testing/testing.go:878 +0x353
victor-simida commented 5 years ago

I think i have the same issue.

func TestVerify(t *testing.T) {
    s, err := govaluate.NewEvaluableExpression("wrongcondition")
    assert.Nil(t, err)
    tt, err := s.Evaluate(nil)
    assert.Nil(t, err)
    t.Log(tt)
}

It's panic too Here's the stacktrace:

panic: runtime error: invalid memory address or nil pointer dereference [recovered]
    panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x43e9f1e]

goroutine 11 [running]:
testing.tRunner.func1(0xc42030a1e0)
    /usr/local/go/src/testing/testing.go:711 +0x2d2
panic(0x4798960, 0x4cab380)
    /usr/local/go/src/runtime/panic.go:491 +0x283
git.garena.com/shopee/platform/spex/vendor/github.com/Knetic/govaluate.makeParameterStage.func1(0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x46d3d01, 0x1140004c60598)
    /Users/vagaa/golang/src/git.garena.com/shopee/platform/spex/vendor/github.com/Knetic/govaluate/evaluationStage.go:217 +0x2e
git.garena.com/shopee/platform/spex/vendor/github.com/Knetic/govaluate.EvaluableExpression.evaluateStage(0x48995dc, 0x22, 0x1, 0xc4200e79a0, 0x1, 0x1, 0xc420089040, 0x488bfd7, 0xe, 0xc420089040, ...)
    /Users/vagaa/golang/src/git.garena.com/shopee/platform/spex/vendor/github.com/Knetic/govaluate/EvaluableExpression.go:228 +0x16e
git.garena.com/shopee/platform/spex/vendor/github.com/Knetic/govaluate.EvaluableExpression.Eval(0x48995dc, 0x22, 0x1, 0xc4200e79a0, 0x1, 0x1, 0xc420089040, 0x488bfd7, 0xe, 0x0, ...)
    /Users/vagaa/golang/src/git.garena.com/shopee/platform/spex/vendor/github.com/Knetic/govaluate/EvaluableExpression.go:159 +0xa1
git.garena.com/shopee/platform/spex/vendor/github.com/Knetic/govaluate.EvaluableExpression.Evaluate(0x48995dc, 0x22, 0x1, 0xc4200e79a0, 0x1, 0x1, 0xc420089040, 0x488bfd7, 0xe, 0x0, ...)
    /Users/vagaa/golang/src/git.garena.com/shopee/platform/spex/vendor/github.com/Knetic/govaluate/EvaluableExpression.go:134 +0xfb
mr-karan commented 5 years ago

+1 Got bitten by the same.