Skip to content

Commit a015fbb

Browse files
CASSGO-2 Marshal big int returns variable length slice.
The marshalBigInt return 8 bytes slice in all cases except for big.Int, which returns a variable length slice, but should be 8 bytes slice as well. patch by Mykyta Oleksiienko; reviewed by João Reis for CASSGO-2
1 parent 7b7e6af commit a015fbb

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414

1515
### Fixed
1616

17+
- CASSGO-2
18+
1719
## [1.7.0] - 2024-09-23
1820

1921
This release is the first after the donation of gocql to the Apache Software Foundation (ASF)

marshal.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ type Unmarshaler interface {
7777
// tinyint, smallint, int | integer types |
7878
// tinyint, smallint, int | string | formatted as base 10 number
7979
// bigint, counter | integer types |
80-
// bigint, counter | big.Int |
80+
// bigint, counter | big.Int | according to cassandra bigint specification the big.Int value limited to int64 size(an eight-byte two's complement integer.)
8181
// bigint, counter | string | formatted as base 10 number
8282
// float | float32 |
8383
// double | float64 |
@@ -671,7 +671,10 @@ func marshalBigInt(info TypeInfo, value interface{}) ([]byte, error) {
671671
case uint8:
672672
return encBigInt(int64(v)), nil
673673
case big.Int:
674-
return encBigInt2C(&v), nil
674+
if !v.IsInt64() {
675+
return nil, marshalErrorf("marshal bigint: value %v out of range", &v)
676+
}
677+
return encBigInt(v.Int64()), nil
675678
case string:
676679
i, err := strconv.ParseInt(value.(string), 10, 64)
677680
if err != nil {
@@ -773,6 +776,8 @@ func marshalVarint(info TypeInfo, value interface{}) ([]byte, error) {
773776
retBytes = make([]byte, 8)
774777
binary.BigEndian.PutUint64(retBytes, v)
775778
}
779+
case big.Int:
780+
retBytes = encBigInt2C(&v)
776781
default:
777782
retBytes, err = marshalBigInt(info, value)
778783
}

marshal_test.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ import (
3939
"time"
4040

4141
"gopkg.in/inf.v0"
42+
43+
"github.com/stretchr/testify/require"
4244
)
4345

4446
type AliasInt int
@@ -1349,7 +1351,7 @@ func TestMarshal_Encode(t *testing.T) {
13491351
}
13501352
} else {
13511353
if _, err := Marshal(test.Info, test.Value); err != test.MarshalError {
1352-
t.Errorf("unmarshalTest[%d] (%v=>%t): %#v returned error %#v, want %#v.", i, test.Info, test.Value, test.Value, err, test.MarshalError)
1354+
t.Errorf("marshalTest[%d] (%v=>%t): %#v returned error %#v, want %#v.", i, test.Info, test.Value, test.Value, err, test.MarshalError)
13531355
}
13541356
}
13551357
}
@@ -1535,6 +1537,32 @@ func TestMarshalVarint(t *testing.T) {
15351537
}
15361538
}
15371539

1540+
func TestMarshalBigInt(t *testing.T) {
1541+
var testStruct = []struct {
1542+
Info TypeInfo
1543+
Value interface{}
1544+
MarshalError error
1545+
}{
1546+
{
1547+
NativeType{proto: 2, typ: TypeBigInt},
1548+
"-78635384813432117863538481343211",
1549+
MarshalError("can not marshal string to bigint: strconv.ParseInt: parsing \"-78635384813432117863538481343211\": value out of range"),
1550+
},
1551+
{
1552+
NativeType{proto: 2, typ: TypeBigInt},
1553+
"922337203685477692259749625974294",
1554+
MarshalError("can not marshal string to bigint: strconv.ParseInt: parsing \"922337203685477692259749625974294\": value out of range"),
1555+
},
1556+
}
1557+
1558+
t.Run("testMarshalBigInt", func(t *testing.T) {
1559+
for _, tc := range testStruct {
1560+
_, err := Marshal(tc.Info, tc.Value)
1561+
require.Equal(t, tc.MarshalError, err)
1562+
}
1563+
})
1564+
}
1565+
15381566
func equalStringPointerSlice(leftList, rightList []*string) bool {
15391567
if len(leftList) != len(rightList) {
15401568
return false

0 commit comments

Comments
 (0)