Skip to content

Commit 5bbbf0d

Browse files
committed
fix negative operator logic
1 parent 75e1e0e commit 5bbbf0d

File tree

1 file changed

+56
-23
lines changed

1 file changed

+56
-23
lines changed

correlation/cache/operators.go

Lines changed: 56 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cache
22

33
import (
4+
"fmt"
45
"net"
56
"regexp"
67
"strconv"
@@ -9,17 +10,18 @@ import (
910
"github.com/tidwall/gjson"
1011
)
1112

12-
func inCIDR(addr, network string) bool {
13+
func inCIDR(addr, network string) (bool, error) {
1314
_, subnet, err := net.ParseCIDR(network)
1415
if err == nil {
1516
ip := net.ParseIP(addr)
1617
if ip != nil {
1718
if subnet.Contains(ip) {
18-
return true
19+
return true, nil
1920
}
2021
}
22+
return false, fmt.Errorf("invalid IP address")
2123
}
22-
return false
24+
return false, err
2325
}
2426

2527
func equal(val1, val2 string) bool {
@@ -52,27 +54,26 @@ func endWith(str, suff string) bool {
5254
return strings.HasSuffix(str, suff)
5355
}
5456

55-
func expresion(exp, str string) bool {
57+
func expresion(exp, str string) (bool, error) {
5658
re, err := regexp.Compile(exp)
5759
if err == nil {
5860
if re.MatchString(str) {
59-
return true
61+
return true, nil
6062
}
6163
}
62-
return false
64+
return false, err
6365
}
6466

65-
func minThan(min, may string) bool {
66-
minN, err := strconv.ParseFloat(min, 64)
67-
if err != nil {
68-
return false
67+
func parseFloats(val1, val2 string) (float64, float64, error) {
68+
f1, err1 := strconv.ParseFloat(val1, 64)
69+
if err1 != nil {
70+
return 0, 0, err1
6971
}
70-
mayN, err := strconv.ParseFloat(may, 64)
71-
if err != nil {
72-
return false
72+
f2, err2 := strconv.ParseFloat(val2, 64)
73+
if err2 != nil {
74+
return 0, 0, err2
7375
}
74-
75-
return minN < mayN
76+
return f1, f2, nil
7677
}
7778

7879
func compare(operator, val1, val2 string) bool {
@@ -104,23 +105,55 @@ func compare(operator, val1, val2 string) bool {
104105
case "not end with":
105106
return !endWith(val1, val2)
106107
case "regexp":
107-
return expresion(val2, val1)
108+
matched, err := expresion(val2, val1)
109+
if err != nil {
110+
return false
111+
}
112+
return matched
108113
case "not regexp":
109-
return !expresion(val2, val1)
114+
matched, err := expresion(val2, val1)
115+
if err != nil {
116+
return false
117+
}
118+
return matched
110119
case "<":
111-
return minThan(val1, val2)
120+
f1, f2, err := parseFloats(val1, val2)
121+
if err != nil {
122+
return false
123+
}
124+
return f1 < f2
112125
case ">":
113-
return !minThan(val1, val2)
126+
f1, f2, err := parseFloats(val1, val2)
127+
if err != nil {
128+
return false
129+
}
130+
return f1 > f2
114131
case "<=":
115-
return equal(val1, val2) || minThan(val1, val2)
132+
f1, f2, err := parseFloats(val1, val2)
133+
if err != nil {
134+
return false
135+
}
136+
return f1 <= f2
116137
case ">=":
117-
return equal(val1, val2) || !minThan(val1, val2)
138+
f1, f2, err := parseFloats(val1, val2)
139+
if err != nil {
140+
return false
141+
}
142+
return f1 >= f2
118143
case "exist":
119144
return true
120145
case "in cidr":
121-
return inCIDR(val1, val2)
146+
matched, err := inCIDR(val1, val2)
147+
if err == nil {
148+
return matched
149+
}
150+
return false
122151
case "not in cidr":
123-
return !inCIDR(val1, val2)
152+
matched, err := inCIDR(val1, val2)
153+
if err == nil {
154+
return !matched
155+
}
156+
return false
124157
default:
125158
return false
126159
}

0 commit comments

Comments
 (0)