Skip to content

Commit

Permalink
Prevent divide by zero errors in alloy syntax
Browse files Browse the repository at this point in the history
Also include previous prior failed fuzz test to validate it doesn't
continue to fail.
  • Loading branch information
kelnage committed Feb 18, 2025
1 parent 5e37a9a commit c427e18
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
go test fuzz v1
string("types=0+0^100%000")
string("0")
string("\xa9\xc1\xd3\xff\xf8\xf8,\xd3r")
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
go test fuzz v1
string("types=100/00/00.00")
string("0\x00\x80")
string("\x06")
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
go test fuzz v1
string("\n\t\tforward_to = []\n\t\tredact_with = \"<ALLOY-REDACTED-SECRET:$SECRET_NAME>\"\n\t")
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")
string("{\n\t\t\t\"message\": \"This is a simple log message with a secret value fakeSecret99999 !\n\t\t}")
47 changes: 47 additions & 0 deletions syntax/vm/op_binary.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package vm

import (
"errors"
"fmt"
"math"
"reflect"
Expand Down Expand Up @@ -104,6 +105,29 @@ func evalBinop(lhs value.Value, op token.Token, rhs value.Value) (value.Value, e

case token.DIV: // number / number
lhsNum, rhsNum := lhs.Number(), rhs.Number()
switch rhsNum.Kind() {
case value.NumberKindUint:
if rhsNum.Uint() == uint64(0) {
return value.Null, value.Error{
Value: origRHS,
Inner: errors.New("divide by zero error"),
}
}
case value.NumberKindInt:
if rhsNum.Int() == int64(0) {
return value.Null, value.Error{
Value: origRHS,
Inner: errors.New("divide by zero error"),
}
}
case value.NumberKindFloat:
if rhsNum.Float() == float64(0) {
return value.Null, value.Error{
Value: origRHS,
Inner: errors.New("divide by zero error"),
}
}
}
switch fitNumberKinds(lhsNum.Kind(), rhsNum.Kind()) {
case value.NumberKindUint:
return value.Uint(lhsNum.Uint() / rhsNum.Uint()), nil
Expand All @@ -115,6 +139,29 @@ func evalBinop(lhs value.Value, op token.Token, rhs value.Value) (value.Value, e

case token.MOD: // number % number
lhsNum, rhsNum := lhs.Number(), rhs.Number()
switch rhsNum.Kind() {
case value.NumberKindUint:
if rhsNum.Uint() == uint64(0) {
return value.Null, value.Error{
Value: origRHS,
Inner: errors.New("divide by zero error"),
}
}
case value.NumberKindInt:
if rhsNum.Int() == int64(0) {
return value.Null, value.Error{
Value: origRHS,
Inner: errors.New("divide by zero error"),
}
}
case value.NumberKindFloat:
if rhsNum.Float() == float64(0) {
return value.Null, value.Error{
Value: origRHS,
Inner: errors.New("divide by zero error"),
}
}
}
switch fitNumberKinds(lhsNum.Kind(), rhsNum.Kind()) {
case value.NumberKindUint:
return value.Uint(lhsNum.Uint() % rhsNum.Uint()), nil
Expand Down

0 comments on commit c427e18

Please sign in to comment.