From 0987069e373f6ae4f7391c3d2a00f76e416f2000 Mon Sep 17 00:00:00 2001 From: Klaus Post Date: Tue, 8 Sep 2020 15:56:11 -0700 Subject: [PATCH] select: Fix integer conversion overflow (#10437) Do not convert float value to integer if it will over/underflow. The comparison cannot be `<=` since rounding may overflow it. Fixes #10436 --- pkg/s3select/select_test.go | 23 +++++++++++++++++++++-- pkg/s3select/sql/value.go | 2 +- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/pkg/s3select/select_test.go b/pkg/s3select/select_test.go index 2c5ae6e88..851d1d178 100644 --- a/pkg/s3select/select_test.go +++ b/pkg/s3select/select_test.go @@ -18,6 +18,7 @@ package s3select import ( "bytes" + "encoding/xml" "fmt" "io" "io/ioutil" @@ -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[*]`, @@ -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 { @@ -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 { diff --git a/pkg/s3select/sql/value.go b/pkg/s3select/sql/value.go index d2c85d1aa..a74b239f1 100644 --- a/pkg/s3select/sql/value.go +++ b/pkg/s3select/sql/value.go @@ -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)