Skip to content

Commit a4ff7ea

Browse files
author
Li2CO3
authored
fix: fix generic write int range check (#1861)
1 parent 37b9260 commit a4ff7ea

File tree

2 files changed

+88
-6
lines changed

2 files changed

+88
-6
lines changed

pkg/generic/thrift/write.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"encoding/base64"
2222
"encoding/json"
2323
"fmt"
24+
"math"
2425

2526
"github.com/cloudwego/gopkg/protocol/thrift"
2627
"github.com/cloudwego/gopkg/protocol/thrift/base"
@@ -339,7 +340,7 @@ func writeInt16(ctx context.Context, val interface{}, out *thrift.BufferWriter,
339340
i := val.(int16)
340341
switch t.Type {
341342
case descriptor.I08:
342-
if i&0xff != i {
343+
if i < math.MinInt8 || i > math.MaxInt8 {
343344
return fmt.Errorf("value is beyond range of i8: %v", i)
344345
}
345346
return out.WriteByte(int8(i))
@@ -358,12 +359,12 @@ func writeInt32(ctx context.Context, val interface{}, out *thrift.BufferWriter,
358359
i := val.(int32)
359360
switch t.Type {
360361
case descriptor.I08:
361-
if i&0xff != i {
362+
if i < math.MinInt8 || i > math.MaxInt8 {
362363
return fmt.Errorf("value is beyond range of i8: %v", i)
363364
}
364365
return out.WriteByte(int8(i))
365366
case descriptor.I16:
366-
if i&0xffff != i {
367+
if i < math.MinInt16 || i > math.MaxInt16 {
367368
return fmt.Errorf("value is beyond range of i16: %v", i)
368369
}
369370
return out.WriteI16(int16(i))
@@ -380,17 +381,17 @@ func writeInt64(ctx context.Context, val interface{}, out *thrift.BufferWriter,
380381
i := val.(int64)
381382
switch t.Type {
382383
case descriptor.I08:
383-
if i&0xff != i {
384+
if i < math.MinInt8 || i > math.MaxInt8 {
384385
return fmt.Errorf("value is beyond range of i8: %v", i)
385386
}
386387
return out.WriteByte(int8(i))
387388
case descriptor.I16:
388-
if i&0xffff != i {
389+
if i < math.MinInt16 || i > math.MaxInt16 {
389390
return fmt.Errorf("value is beyond range of i16: %v", i)
390391
}
391392
return out.WriteI16(int16(i))
392393
case descriptor.I32:
393-
if i&0xffffffff != i {
394+
if i < math.MinInt32 || i > math.MaxInt32 {
394395
return fmt.Errorf("value is beyond range of i32: %v", i)
395396
}
396397
return out.WriteI32(int32(i))

pkg/generic/thrift/write_test.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,21 @@ func Test_nextWriter(t *testing.T) {
107107
},
108108
false,
109109
},
110+
{
111+
"nextWriteri64 Success",
112+
args{
113+
val: int64(-1),
114+
t: &descriptor.TypeDescriptor{
115+
Type: descriptor.I64,
116+
Struct: &descriptor.StructDescriptor{},
117+
},
118+
opt: &writerOption{
119+
requestBase: &base.Base{},
120+
binaryWithBase64: false,
121+
},
122+
},
123+
false,
124+
},
110125
{
111126
"nextWriterbool Success",
112127
args{
@@ -429,6 +444,17 @@ func Test_writeInt16(t *testing.T) {
429444
},
430445
false,
431446
},
447+
{
448+
"writeInt16toInt8 Negative Success",
449+
args{
450+
val: int16(-1),
451+
t: &descriptor.TypeDescriptor{
452+
Type: descriptor.I08,
453+
Struct: &descriptor.StructDescriptor{},
454+
},
455+
},
456+
false,
457+
},
432458
{
433459
"writeInt16toInt8 Failed",
434460
args{
@@ -529,6 +555,17 @@ func Test_writeInt32(t *testing.T) {
529555
},
530556
false,
531557
},
558+
{
559+
"writeInt32ToInt8 Negative Success",
560+
args{
561+
val: int32(-1),
562+
t: &descriptor.TypeDescriptor{
563+
Type: descriptor.I08,
564+
Struct: &descriptor.StructDescriptor{},
565+
},
566+
},
567+
false,
568+
},
532569
{
533570
"writeInt32ToInt8 Failed",
534571
args{
@@ -551,6 +588,17 @@ func Test_writeInt32(t *testing.T) {
551588
},
552589
false,
553590
},
591+
{
592+
"writeInt32ToInt16 Negative success",
593+
args{
594+
val: int32(-1),
595+
t: &descriptor.TypeDescriptor{
596+
Type: descriptor.I16,
597+
Struct: &descriptor.StructDescriptor{},
598+
},
599+
},
600+
false,
601+
},
554602
{
555603
"writeInt32ToInt16 Failed",
556604
args{
@@ -628,6 +676,17 @@ func Test_writeInt64(t *testing.T) {
628676
},
629677
false,
630678
},
679+
{
680+
"writeInt64ToInt8 Negative Success",
681+
args{
682+
val: int64(-1),
683+
t: &descriptor.TypeDescriptor{
684+
Type: descriptor.I08,
685+
Struct: &descriptor.StructDescriptor{},
686+
},
687+
},
688+
false,
689+
},
631690
{
632691
"writeInt64ToInt8 failed",
633692
args{
@@ -650,6 +709,17 @@ func Test_writeInt64(t *testing.T) {
650709
},
651710
false,
652711
},
712+
{
713+
"writeInt64ToInt16 Negative Success",
714+
args{
715+
val: int64(-1),
716+
t: &descriptor.TypeDescriptor{
717+
Type: descriptor.I16,
718+
Struct: &descriptor.StructDescriptor{},
719+
},
720+
},
721+
false,
722+
},
653723
{
654724
"writeInt64ToInt16 failed",
655725
args{
@@ -672,6 +742,17 @@ func Test_writeInt64(t *testing.T) {
672742
},
673743
false,
674744
},
745+
{
746+
"writeInt64ToInt32 Negative Success",
747+
args{
748+
val: int64(-1),
749+
t: &descriptor.TypeDescriptor{
750+
Type: descriptor.I32,
751+
Struct: &descriptor.StructDescriptor{},
752+
},
753+
},
754+
false,
755+
},
675756
{
676757
"writeInt64ToInt32 failed",
677758
args{

0 commit comments

Comments
 (0)