Skip to content

Commit

Permalink
Merge pull request #690 from Dieterbe/pre-allocated-decode-errors
Browse files Browse the repository at this point in the history
pre-allocate decoding errors
  • Loading branch information
eapache authored Jul 5, 2016
2 parents 88a4afb + 70f640e commit 1a3d124
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions real_decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import (
"math"
)

var errInvalidArrayLength = PacketDecodingError{"invalid array length"}
var errInvalidByteSliceLength = PacketDecodingError{"invalid byteslice length"}
var errInvalidStringLength = PacketDecodingError{"invalid string length"}
var errInvalidSubsetSize = PacketDecodingError{"invalid subset size"}

type realDecoder struct {
raw []byte
off int
Expand Down Expand Up @@ -64,7 +69,7 @@ func (rd *realDecoder) getArrayLength() (int, error) {
rd.off = len(rd.raw)
return -1, ErrInsufficientData
} else if tmp > 2*math.MaxUint16 {
return -1, PacketDecodingError{"invalid array length"}
return -1, errInvalidArrayLength
}
return tmp, nil
}
Expand All @@ -82,7 +87,7 @@ func (rd *realDecoder) getBytes() ([]byte, error) {

switch {
case n < -1:
return nil, PacketDecodingError{"invalid byteslice length"}
return nil, errInvalidByteSliceLength
case n == -1:
return nil, nil
case n == 0:
Expand All @@ -108,7 +113,7 @@ func (rd *realDecoder) getString() (string, error) {

switch {
case n < -1:
return "", PacketDecodingError{"invalid string length"}
return "", errInvalidStringLength
case n == -1:
return "", nil
case n == 0:
Expand Down Expand Up @@ -141,7 +146,7 @@ func (rd *realDecoder) getInt32Array() ([]int32, error) {
}

if n < 0 {
return nil, PacketDecodingError{"invalid array length"}
return nil, errInvalidArrayLength
}

ret := make([]int32, n)
Expand Down Expand Up @@ -170,7 +175,7 @@ func (rd *realDecoder) getInt64Array() ([]int64, error) {
}

if n < 0 {
return nil, PacketDecodingError{"invalid array length"}
return nil, errInvalidArrayLength
}

ret := make([]int64, n)
Expand All @@ -194,7 +199,7 @@ func (rd *realDecoder) getStringArray() ([]string, error) {
}

if n < 0 {
return nil, PacketDecodingError{"invalid array length"}
return nil, errInvalidArrayLength
}

ret := make([]string, n)
Expand All @@ -216,7 +221,7 @@ func (rd *realDecoder) remaining() int {

func (rd *realDecoder) getSubset(length int) (packetDecoder, error) {
if length < 0 {
return nil, PacketDecodingError{"invalid subset size"}
return nil, errInvalidSubsetSize
} else if length > rd.remaining() {
rd.off = len(rd.raw)
return nil, ErrInsufficientData
Expand Down

0 comments on commit 1a3d124

Please sign in to comment.