Skip to content

Commit e8a930c

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 e8a930c

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,20 @@ var marshalTests = []struct {
247247
nil,
248248
nil,
249249
},
250+
{
251+
NativeType{proto: 2, typ: TypeBigInt},
252+
[]byte("\xf7\x0c\x6b\x14\x84\x49\x3b\x36\x4a\x36\x1e\x03\x34\x05"),
253+
"-78635384813432117863538481343211",
254+
MarshalError("can not marshal string to bigint: strconv.ParseInt: parsing \"-78635384813432117863538481343211\": value out of range"),
255+
nil,
256+
},
257+
{
258+
NativeType{proto: 2, typ: TypeBigInt},
259+
[]byte("\x20\x45\xce\x3b\x05\xef\x2d\xde\x51\xb9\x28\x76\x6d\x6e"),
260+
"922337203685477692259749625974294",
261+
MarshalError("can not marshal string to bigint: strconv.ParseInt: parsing \"922337203685477692259749625974294\": value out of range"),
262+
nil,
263+
},
250264
{
251265
NativeType{proto: 2, typ: TypeBoolean},
252266
[]byte("\x00"),

0 commit comments

Comments
 (0)