File tree 3 files changed +30
-0
lines changed
3 files changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -1142,6 +1142,17 @@ func TestIssue_nested_closures(t *testing.T) {
1142
1142
require .True (t , output .(bool ))
1143
1143
}
1144
1144
1145
+ func TestIssue138 (t * testing.T ) {
1146
+ env := map [string ]interface {}{}
1147
+
1148
+ _ , err := expr .Compile (`1 / (1 - 1)` , expr .Env (env ))
1149
+ require .Error (t , err )
1150
+ require .Equal (t , "integer divide by zero (1:3)\n | 1 / (1 - 1)\n | ..^" , err .Error ())
1151
+
1152
+ _ , err = expr .Compile (`1 % 0` , expr .Env (env ))
1153
+ require .Error (t , err )
1154
+ }
1155
+
1145
1156
//
1146
1157
// Mock types
1147
1158
//
Original file line number Diff line number Diff line change @@ -5,10 +5,12 @@ import (
5
5
"reflect"
6
6
7
7
. "github.com/antonmedv/expr/ast"
8
+ "github.com/antonmedv/expr/file"
8
9
)
9
10
10
11
type fold struct {
11
12
applied bool
13
+ err * file.Error
12
14
}
13
15
14
16
func (* fold ) Enter (* Node ) {}
@@ -65,12 +67,26 @@ func (fold *fold) Exit(node *Node) {
65
67
case "/" :
66
68
if a , ok := n .Left .(* IntegerNode ); ok {
67
69
if b , ok := n .Right .(* IntegerNode ); ok {
70
+ if b .Value == 0 {
71
+ fold .err = & file.Error {
72
+ Location : (* node ).Location (),
73
+ Message : "integer divide by zero" ,
74
+ }
75
+ return
76
+ }
68
77
patchWithType (& IntegerNode {Value : a .Value / b .Value }, a .Type ())
69
78
}
70
79
}
71
80
case "%" :
72
81
if a , ok := n .Left .(* IntegerNode ); ok {
73
82
if b , ok := n .Right .(* IntegerNode ); ok {
83
+ if b .Value == 0 {
84
+ fold .err = & file.Error {
85
+ Location : (* node ).Location (),
86
+ Message : "integer divide by zero" ,
87
+ }
88
+ return
89
+ }
74
90
patch (& IntegerNode {Value : a .Value % b .Value })
75
91
}
76
92
}
Original file line number Diff line number Diff line change @@ -10,6 +10,9 @@ func Optimize(node *Node, config *conf.Config) error {
10
10
for limit := 1000 ; limit >= 0 ; limit -- {
11
11
fold := & fold {}
12
12
Walk (node , fold )
13
+ if fold .err != nil {
14
+ return fold .err
15
+ }
13
16
if ! fold .applied {
14
17
break
15
18
}
You can’t perform that action at this time.
0 commit comments