Skip to content

Commit 3ac6aef

Browse files
committed
Refactor error handling and rename function for clarity
Improved error handling in several functions to make logic clearer and avoid potential issues. Renamed `expresion` to `expression` for correctness and updated related references. Consolidated redundant error paths and adjusted logic for better readability and maintainability.
1 parent f807c27 commit 3ac6aef

File tree

1 file changed

+38
-29
lines changed

1 file changed

+38
-29
lines changed

correlation/cache/operators.go

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,20 @@ import (
1212

1313
func inCIDR(addr, network string) (bool, error) {
1414
_, subnet, err := net.ParseCIDR(network)
15-
if err == nil {
16-
ip := net.ParseIP(addr)
17-
if ip != nil {
18-
if subnet.Contains(ip) {
19-
return true, nil
20-
}
21-
}
15+
if err != nil {
16+
return false, fmt.Errorf("invalid CIDR")
17+
}
18+
19+
ip := net.ParseIP(addr)
20+
if ip == nil {
2221
return false, fmt.Errorf("invalid IP address")
2322
}
24-
return false, err
23+
24+
if subnet.Contains(ip) {
25+
return true, nil
26+
}
27+
28+
return false, nil
2529
}
2630

2731
func equal(val1, val2 string) bool {
@@ -54,25 +58,30 @@ func endWith(str, suff string) bool {
5458
return strings.HasSuffix(str, suff)
5559
}
5660

57-
func expresion(exp, str string) (bool, error) {
61+
func expression(exp, str string) (bool, error) {
5862
re, err := regexp.Compile(exp)
59-
if err == nil {
60-
if re.MatchString(str) {
61-
return true, nil
62-
}
63+
if err != nil {
64+
return false, err
6365
}
64-
return false, err
66+
67+
if re.MatchString(str) {
68+
return true, nil
69+
}
70+
71+
return false, nil
6572
}
6673

6774
func parseFloats(val1, val2 string) (float64, float64, error) {
68-
f1, err1 := strconv.ParseFloat(val1, 64)
69-
if err1 != nil {
70-
return 0, 0, err1
75+
f1, err := strconv.ParseFloat(val1, 64)
76+
if err != nil {
77+
return 0, 0, err
7178
}
72-
f2, err2 := strconv.ParseFloat(val2, 64)
73-
if err2 != nil {
74-
return 0, 0, err2
79+
80+
f2, err := strconv.ParseFloat(val2, 64)
81+
if err != nil {
82+
return 0, 0, err
7583
}
84+
7685
return f1, f2, nil
7786
}
7887

@@ -105,17 +114,17 @@ func compare(operator, val1, val2 string) bool {
105114
case "not end with":
106115
return !endWith(val1, val2)
107116
case "regexp":
108-
matched, err := expresion(val2, val1)
117+
matched, err := expression(val2, val1)
109118
if err != nil {
110119
return false
111120
}
112121
return matched
113122
case "not regexp":
114-
matched, err := expresion(val2, val1)
123+
matched, err := expression(val2, val1)
115124
if err != nil {
116125
return false
117126
}
118-
return matched
127+
return !matched
119128
case "<":
120129
f1, f2, err := parseFloats(val1, val2)
121130
if err != nil {
@@ -144,16 +153,16 @@ func compare(operator, val1, val2 string) bool {
144153
return true
145154
case "in cidr":
146155
matched, err := inCIDR(val1, val2)
147-
if err == nil {
148-
return matched
156+
if err != nil {
157+
return false
149158
}
150-
return false
159+
return matched
151160
case "not in cidr":
152161
matched, err := inCIDR(val1, val2)
153-
if err == nil {
154-
return !matched
162+
if err != nil {
163+
return false
155164
}
156-
return false
165+
return !matched
157166
default:
158167
return false
159168
}

0 commit comments

Comments
 (0)