Skip to content

Commit e99e62d

Browse files
authored
Merge pull request #344 from orisano/fix/#343
fix: to care ints minimum values
2 parents d496803 + f714c39 commit e99e62d

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

decode_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,6 +1317,12 @@ var unmarshalTests = []unmarshalTest{
13171317
ptr: new(string),
13181318
out: "hello\ufffd\ufffd\ufffd\ufffd\ufffd\ufffdworld",
13191319
},
1320+
{in: "-128", ptr: new(int8), out: int8(-128)},
1321+
{in: "127", ptr: new(int8), out: int8(127)},
1322+
{in: "-32768", ptr: new(int16), out: int16(-32768)},
1323+
{in: "32767", ptr: new(int16), out: int16(32767)},
1324+
{in: "-2147483648", ptr: new(int32), out: int32(-2147483648)},
1325+
{in: "2147483647", ptr: new(int32), out: int32(2147483647)},
13201326
}
13211327

13221328
type All struct {

internal/decoder/int.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,15 +192,15 @@ func (d *intDecoder) DecodeStream(s *Stream, depth int64, p unsafe.Pointer) erro
192192
}
193193
switch d.kind {
194194
case reflect.Int8:
195-
if i64 <= -1*(1<<7) || (1<<7) <= i64 {
195+
if i64 < -1*(1<<7) || (1<<7) <= i64 {
196196
return d.typeError(bytes, s.totalOffset())
197197
}
198198
case reflect.Int16:
199-
if i64 <= -1*(1<<15) || (1<<15) <= i64 {
199+
if i64 < -1*(1<<15) || (1<<15) <= i64 {
200200
return d.typeError(bytes, s.totalOffset())
201201
}
202202
case reflect.Int32:
203-
if i64 <= -1*(1<<31) || (1<<31) <= i64 {
203+
if i64 < -1*(1<<31) || (1<<31) <= i64 {
204204
return d.typeError(bytes, s.totalOffset())
205205
}
206206
}
@@ -225,15 +225,15 @@ func (d *intDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, p unsafe.P
225225
}
226226
switch d.kind {
227227
case reflect.Int8:
228-
if i64 <= -1*(1<<7) || (1<<7) <= i64 {
228+
if i64 < -1*(1<<7) || (1<<7) <= i64 {
229229
return 0, d.typeError(bytes, cursor)
230230
}
231231
case reflect.Int16:
232-
if i64 <= -1*(1<<15) || (1<<15) <= i64 {
232+
if i64 < -1*(1<<15) || (1<<15) <= i64 {
233233
return 0, d.typeError(bytes, cursor)
234234
}
235235
case reflect.Int32:
236-
if i64 <= -1*(1<<31) || (1<<31) <= i64 {
236+
if i64 < -1*(1<<31) || (1<<31) <= i64 {
237237
return 0, d.typeError(bytes, cursor)
238238
}
239239
}

0 commit comments

Comments
 (0)