Browse Source

ensure int conversion correctness

pull/7185/head
chrislu 1 month ago
parent
commit
d572443c79
  1. 7
      weed/query/engine/aggregations.go
  2. 29
      weed/query/engine/string_functions.go

7
weed/query/engine/aggregations.go

@ -3,6 +3,7 @@ package engine
import ( import (
"context" "context"
"fmt" "fmt"
"math"
"strconv" "strconv"
"strings" "strings"
@ -356,6 +357,9 @@ func (e *SQLEngine) executeAggregationQueryWithPlan(ctx context.Context, hybridS
if stmt.Limit != nil && stmt.Limit.Rowcount != nil { if stmt.Limit != nil && stmt.Limit.Rowcount != nil {
if limitExpr, ok := stmt.Limit.Rowcount.(*SQLVal); ok && limitExpr.Type == IntVal { if limitExpr, ok := stmt.Limit.Rowcount.(*SQLVal); ok && limitExpr.Type == IntVal {
if limit64, err := strconv.ParseInt(string(limitExpr.Val), 10, 64); err == nil { if limit64, err := strconv.ParseInt(string(limitExpr.Val), 10, 64); err == nil {
if limit64 > int64(math.MaxInt) || limit64 < 0 {
return nil, fmt.Errorf("LIMIT value %d is out of range", limit64)
}
limit = int(limit64) limit = int(limit64)
} }
} }
@ -363,6 +367,9 @@ func (e *SQLEngine) executeAggregationQueryWithPlan(ctx context.Context, hybridS
if stmt.Limit != nil && stmt.Limit.Offset != nil { if stmt.Limit != nil && stmt.Limit.Offset != nil {
if offsetExpr, ok := stmt.Limit.Offset.(*SQLVal); ok && offsetExpr.Type == IntVal { if offsetExpr, ok := stmt.Limit.Offset.(*SQLVal); ok && offsetExpr.Type == IntVal {
if offset64, err := strconv.ParseInt(string(offsetExpr.Val), 10, 64); err == nil { if offset64, err := strconv.ParseInt(string(offsetExpr.Val), 10, 64); err == nil {
if offset64 > int64(math.MaxInt) || offset64 < 0 {
return nil, fmt.Errorf("OFFSET value %d is out of range", offset64)
}
offset = int(offset64) offset = int(offset64)
} }
} }

29
weed/query/engine/string_functions.go

@ -147,11 +147,16 @@ func (e *SQLEngine) Substring(value *schema_pb.Value, start *schema_pb.Value, le
if lengthVal <= 0 { if lengthVal <= 0 {
result = "" result = ""
} else { } else {
endIdx := startIdx + int(lengthVal)
if endIdx > len(str) {
endIdx = len(str)
if lengthVal > int64(math.MaxInt) {
// If length is too large, take substring from startIdx to end
result = str[startIdx:]
} else {
endIdx := startIdx + int(lengthVal)
if endIdx > len(str) {
endIdx = len(str)
}
result = str[startIdx:endIdx]
} }
result = str[startIdx:endIdx]
} }
} else { } else {
result = str[startIdx:] result = str[startIdx:]
@ -266,7 +271,13 @@ func (e *SQLEngine) Left(value *schema_pb.Value, length *schema_pb.Value) (*sche
}, nil }, nil
} }
if lengthVal > int64(len(str)) || lengthVal > int64(math.MaxInt) {
if lengthVal > int64(len(str)) {
return &schema_pb.Value{
Kind: &schema_pb.Value_StringValue{StringValue: str},
}, nil
}
if lengthVal > int64(math.MaxInt) {
return &schema_pb.Value{ return &schema_pb.Value{
Kind: &schema_pb.Value_StringValue{StringValue: str}, Kind: &schema_pb.Value_StringValue{StringValue: str},
}, nil }, nil
@ -299,7 +310,13 @@ func (e *SQLEngine) Right(value *schema_pb.Value, length *schema_pb.Value) (*sch
}, nil }, nil
} }
if lengthVal > int64(len(str)) || lengthVal > int64(math.MaxInt) {
if lengthVal > int64(len(str)) {
return &schema_pb.Value{
Kind: &schema_pb.Value_StringValue{StringValue: str},
}, nil
}
if lengthVal > int64(math.MaxInt) {
return &schema_pb.Value{ return &schema_pb.Value{
Kind: &schema_pb.Value_StringValue{StringValue: str}, Kind: &schema_pb.Value_StringValue{StringValue: str},
}, nil }, nil

Loading…
Cancel
Save