filter: Fix runtime panic on negative shift amount

This commit is contained in:
Palapeli
2026-12-02 17:00:53 -05:00
parent 6676c2ece4
commit 784de7805c
4 changed files with 15 additions and 11 deletions

View File

@@ -1,7 +1,7 @@
package database
import (
"errors"
"fmt"
"strconv"
"strings"
"wwfc/filter"
@@ -19,7 +19,7 @@ func createSqlFilter(conn *pgconn.PgConn, basenode *filter.TreeNode) (value stri
defer func() {
if str := recover(); str != nil {
value = ""
err = errors.New(str.(string))
err = fmt.Errorf("%v", str)
}
}()

View File

@@ -2,7 +2,7 @@
package filter
import (
"errors"
"fmt"
"regexp"
"strconv"
"strings"
@@ -19,7 +19,7 @@ func Eval(basenode *TreeNode, context map[string]string, queryGame string) (valu
defer func() {
if str := recover(); str != nil {
value = 0
err = errors.New(str.(string))
err = fmt.Errorf("%v", str)
}
}()
@@ -314,10 +314,16 @@ func (e *expression) evalMathXor(val1, val2 int64) int64 {
}
func (e *expression) evalMathLShift(val1, val2 int64) int64 {
if val2 < 0 {
panic("negative shift amount")
}
return val1 << val2
}
func (e *expression) evalMathRShift(val1, val2 int64) int64 {
if val2 < 0 {
panic("negative shift amount")
}
return val1 >> val2
}

View File

@@ -1,26 +1,25 @@
package serverbrowser
package filter
import (
"fmt"
"testing"
"wwfc/filter"
)
func parseFilter(t *testing.T, expression string) {
_, err := filter.Parse(expression)
_, err := Parse(expression)
if err != nil {
t.Error(err)
}
}
func evalFilter(t *testing.T, expression string, queryGame string, context map[string]string) {
tree, err := filter.Parse(expression)
tree, err := Parse(expression)
if err != nil {
t.Error(err)
return
}
ret, err := filter.Eval(tree, context, queryGame)
ret, err := Eval(tree, context, queryGame)
if err != nil {
t.Error(err)
return

View File

@@ -2,7 +2,6 @@
package filter
import (
"errors"
"fmt"
"strings"
"unicode"
@@ -22,7 +21,7 @@ func Parse(input string) (root *TreeNode, err error) {
defer func() {
if str := recover(); str != nil {
root = nil
err = errors.New(str.(string))
err = fmt.Errorf("%v", str)
}
}()