Skip to content

Commit

Permalink
select: Fix integer conversion overflow (minio#10437)
Browse files Browse the repository at this point in the history
Do not convert float value to integer if it will over/underflow.

The comparison cannot be `<=` since rounding may overflow it.

Fixes minio#10436
  • Loading branch information
klauspost authored Sep 8, 2020
1 parent 6a0372b commit 0987069
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
23 changes: 21 additions & 2 deletions pkg/s3select/select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package s3select

import (
"bytes"
"encoding/xml"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -93,6 +94,20 @@ func TestJSONQueries(t *testing.T) {
query: `SELECT * from s3object s WHERE 'bar' in s.synonyms[*]`,
wantResult: `{"id":0,"title":"Test Record","desc":"Some text","synonyms":["foo","bar","whatever"]}`,
},
{
name: "bignum-1",
query: `SELECT id from s3object s WHERE s.id <= 9223372036854775807`,
wantResult: `{"id":0}
{"id":1}
{"id":2}
{"id":3}`},
{
name: "bignum-2",
query: `SELECT id from s3object s WHERE s.id >= -9223372036854775808`,
wantResult: `{"id":0}
{"id":1}
{"id":2}
{"id":3}`},
{
name: "donatello-3",
query: `SELECT * from s3object s WHERE 'value' IN s.synonyms[*]`,
Expand Down Expand Up @@ -355,7 +370,9 @@ func TestJSONQueries(t *testing.T) {

testReq := testCase.requestXML
if len(testReq) == 0 {
testReq = []byte(fmt.Sprintf(defRequest, testCase.query))
var escaped bytes.Buffer
xml.EscapeText(&escaped, []byte(testCase.query))
testReq = []byte(fmt.Sprintf(defRequest, escaped.String()))
}
s3Select, err := NewS3Select(bytes.NewReader(testReq))
if err != nil {
Expand Down Expand Up @@ -401,7 +418,9 @@ func TestJSONQueries(t *testing.T) {
}
testReq := testCase.requestXML
if len(testReq) == 0 {
testReq = []byte(fmt.Sprintf(defRequest, testCase.query))
var escaped bytes.Buffer
xml.EscapeText(&escaped, []byte(testCase.query))
testReq = []byte(fmt.Sprintf(defRequest, escaped.String()))
}
s3Select, err := NewS3Select(bytes.NewReader(testReq))
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/s3select/sql/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ func (v Value) CSVString() string {
// floatToValue converts a float into int representation if needed.
func floatToValue(f float64) *Value {
intPart, fracPart := math.Modf(f)
if fracPart == 0 {
if fracPart == 0 && intPart < math.MaxInt64 && intPart > math.MinInt64 {
return FromInt(int64(intPart))
}
return FromFloat(f)
Expand Down

0 comments on commit 0987069

Please sign in to comment.