Skip to content

Commit c427e18

Browse files
committed
Prevent divide by zero errors in alloy syntax
Also include previous prior failed fuzz test to validate it doesn't continue to fail.
1 parent 5e37a9a commit c427e18

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
go test fuzz v1
2+
string("types=0+0^100%000")
3+
string("0")
4+
string("\xa9\xc1\xd3\xff\xf8\xf8,\xd3r")
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
go test fuzz v1
2+
string("types=100/00/00.00")
3+
string("0\x00\x80")
4+
string("\x06")
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
go test fuzz v1
2+
string("\n\t\tforward_to = []\n\t\tredact_with = \"<ALLOY-REDACTED-SECRET:$SECRET_NAME>\"\n\t")
3+
string("\n\t\ttitle = \"gitleaks custom config\"\n\n\t\t[[rules]]\n\t\tid = \"my-fake-secret\tregex = '''(?i)\\b(fakeSecied a fake secret\"\n\t\tregex = '''(?i)\\b(?:['|\\\"|\\n|\\r|\\s|\\x60|;]|$)'''\n\t\t[rules.allowlist]\n\t\tregexes = [\"abc\\\\d{3}\", \"fakeSecret[9]{5}\"]\nS")
4+
string("{\n\t\t\t\"message\": \"This is a simple log message with a secret value fakeSecret99999 !\n\t\t}")

syntax/vm/op_binary.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package vm
22

33
import (
4+
"errors"
45
"fmt"
56
"math"
67
"reflect"
@@ -104,6 +105,29 @@ func evalBinop(lhs value.Value, op token.Token, rhs value.Value) (value.Value, e
104105

105106
case token.DIV: // number / number
106107
lhsNum, rhsNum := lhs.Number(), rhs.Number()
108+
switch rhsNum.Kind() {
109+
case value.NumberKindUint:
110+
if rhsNum.Uint() == uint64(0) {
111+
return value.Null, value.Error{
112+
Value: origRHS,
113+
Inner: errors.New("divide by zero error"),
114+
}
115+
}
116+
case value.NumberKindInt:
117+
if rhsNum.Int() == int64(0) {
118+
return value.Null, value.Error{
119+
Value: origRHS,
120+
Inner: errors.New("divide by zero error"),
121+
}
122+
}
123+
case value.NumberKindFloat:
124+
if rhsNum.Float() == float64(0) {
125+
return value.Null, value.Error{
126+
Value: origRHS,
127+
Inner: errors.New("divide by zero error"),
128+
}
129+
}
130+
}
107131
switch fitNumberKinds(lhsNum.Kind(), rhsNum.Kind()) {
108132
case value.NumberKindUint:
109133
return value.Uint(lhsNum.Uint() / rhsNum.Uint()), nil
@@ -115,6 +139,29 @@ func evalBinop(lhs value.Value, op token.Token, rhs value.Value) (value.Value, e
115139

116140
case token.MOD: // number % number
117141
lhsNum, rhsNum := lhs.Number(), rhs.Number()
142+
switch rhsNum.Kind() {
143+
case value.NumberKindUint:
144+
if rhsNum.Uint() == uint64(0) {
145+
return value.Null, value.Error{
146+
Value: origRHS,
147+
Inner: errors.New("divide by zero error"),
148+
}
149+
}
150+
case value.NumberKindInt:
151+
if rhsNum.Int() == int64(0) {
152+
return value.Null, value.Error{
153+
Value: origRHS,
154+
Inner: errors.New("divide by zero error"),
155+
}
156+
}
157+
case value.NumberKindFloat:
158+
if rhsNum.Float() == float64(0) {
159+
return value.Null, value.Error{
160+
Value: origRHS,
161+
Inner: errors.New("divide by zero error"),
162+
}
163+
}
164+
}
118165
switch fitNumberKinds(lhsNum.Kind(), rhsNum.Kind()) {
119166
case value.NumberKindUint:
120167
return value.Uint(lhsNum.Uint() % rhsNum.Uint()), nil

0 commit comments

Comments
 (0)