Skip to content

Commit

Permalink
remove maxalloc nonsense
Browse files Browse the repository at this point in the history
  • Loading branch information
lucix-aws committed Feb 20, 2024
1 parent 1b85cfd commit c19d741
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 20 deletions.
4 changes: 0 additions & 4 deletions encoding/cbor/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,3 @@ const (
major7Float32 = minorArg4
major7Float64 = minorArg8
)

// we allocate for 256 items in containers to start at most to prevent huge
// arguments from blowing up memory
const maxAlloc = 0xff
25 changes: 9 additions & 16 deletions encoding/cbor/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,15 @@ func decodeList(p []byte) (List, int, error) {
}
p = p[off:]

l := make(List, min(alen, maxAlloc))
l := List{}
for i := 0; i < int(alen); i++ {
item, n, err := decode(p)
if err != nil {
return nil, 0, fmt.Errorf("decode item: %w", err)
}
p = p[n:]

l[i] = item
l = append(l, item)
off += n
}

Expand Down Expand Up @@ -160,7 +160,7 @@ func decodeMap(p []byte) (Map, int, error) {
}
p = p[off:]

mp := make(Map, min(maplen, maxAlloc))
mp := Map{}
for i := 0; i < int(maplen); i++ {
if len(p) == 0 {
return nil, 0, fmt.Errorf("unexpected end of payload")
Expand Down Expand Up @@ -280,12 +280,12 @@ func peekMinor(p []byte) byte {
// need to check for the indefinite flag must do so externally
func decodeArgument(p []byte) (uint64, int, error) {
minor := peekMinor(p)
if minor < 24 {
if minor < minorArg1 {
return uint64(minor), 1, nil
}

switch minor {
case 24, 25, 26, 27:
case minorArg1, minorArg2, minorArg4, minorArg8:
argLen := mtol(minor)
if len(p) < argLen+1 {
return 0, 0, fmt.Errorf("arg len %d greater than remaining buf len", argLen)
Expand All @@ -296,13 +296,13 @@ func decodeArgument(p []byte) (uint64, int, error) {
}
}

// minor value to arg len in bytes
// minor value to arg len in bytes, assumes minor was checked to be in [24,27]
func mtol(minor byte) int {
if minor == 24 {
if minor == minorArg1 {
return 1
} else if minor == 25 {
} else if minor == minorArg2 {
return 2
} else if minor == 26 {
} else if minor == minorArg4 {
return 4
}
return 8
Expand All @@ -318,10 +318,3 @@ func readArgument(p []byte, len int) uint64 {
}
return uint64(binary.BigEndian.Uint64(p))
}

func min(i, j uint64) uint64 {
if i < j {
return i
}
return j
}

0 comments on commit c19d741

Please sign in to comment.