Skip to content

Commit cc03af5

Browse files
committed
cbor: add range checks for ints
1 parent 9ba4022 commit cc03af5

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

cbor/cborDecoderTerminals.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@ package cbor
22

33
import (
44
"encoding/binary"
5+
"errors"
56
"fmt"
67
"math"
78
)
89

10+
const (
11+
maxUint = ^uint(0)
12+
maxInt = int(maxUint >> 1)
13+
)
14+
915
func (d *Decoder) decodeFloat(majorByte byte) (f float64, err error) {
1016
var bs []byte
1117
switch majorByte {
@@ -66,8 +72,12 @@ func (d *Decoder) decodeNegInt(majorByte byte) (i int64, err error) {
6672
if err != nil {
6773
return 0, err
6874
}
69-
// TODO needs overflow check
70-
return -1 - int64(ui), nil
75+
pos := ui + 1
76+
if pos > uint64(-math.MinInt64) {
77+
return -1, errors.New("cbor: negative integer out of rage of int64 type")
78+
}
79+
80+
return -int64(pos), nil
7181
}
7282

7383
// Decode expecting a positive integer.
@@ -80,7 +90,9 @@ func (d *Decoder) decodeLen(majorByte byte) (i int, err error) {
8090
if err != nil {
8191
return 0, err
8292
}
83-
// TODO needs overflow check
93+
if ui > uint64(maxInt) {
94+
return 0, errors.New("cbor: positive integer is out of length")
95+
}
8496
return int(ui), nil
8597
}
8698

0 commit comments

Comments
 (0)