Skip to content

Commit

Permalink
rlp: fix bug in decodeLength (#2081)
Browse files Browse the repository at this point in the history
Fixes bug in RLP `decodeLength()` by ensuring that calculations of length should use units of bytes instead of bits.

category: bug 
ticket: #2076
  • Loading branch information
xenowits authored Apr 12, 2023
1 parent ed5d746 commit d8c3e1c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
10 changes: 5 additions & 5 deletions eth2util/rlp/rlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ func decodeLength(item []byte) (offset int, length int, err error) {
}

if prefix < 0xc0 {
length = int(prefix - 0xb7)
if length > 63 || length < 0 {
return 0, 0, errors.New("invalid length prefix")
length = int(prefix - 0xb7) // length of the string in bytes in binary form
if length > 8 || length <= 0 { // This is impossible based on outer if else checks
panic("length not in expected range [1,8]")
}

offset := 1 + length
Expand All @@ -125,8 +125,8 @@ func decodeLength(item []byte) (offset int, length int, err error) {
}

length = int(prefix - 0xf7)
if length > 63 || length < 0 {
return 0, 0, errors.New("invalid length prefix")
if length > 8 || length <= 0 { // This is impossible based on outer if else checks
panic("length not in expected range [1,8]")
}

offset = 1 + length
Expand Down
30 changes: 30 additions & 0 deletions eth2util/rlp/rlp_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright © 2022-2023 Obol Labs Inc. Licensed under the terms of a Business Source License 1.1

package rlp

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestDecodeLength(t *testing.T) {
t.Run("ok", func(t *testing.T) {
items := []byte{0xb8, 0x01, 0x0a}
offset, length, err := decodeLength(items)
require.NoError(t, err)
require.Equal(t, 2, offset)
require.Equal(t, 1, length)
})

t.Run("prefix absent", func(t *testing.T) {
_, _, err := decodeLength(nil)
require.ErrorContains(t, err, "input too short")
})

t.Run("prefix provided but length absent", func(t *testing.T) {
items := []byte{0xbf}
_, _, err := decodeLength(items)
require.ErrorContains(t, err, "input too short")
})
}

0 comments on commit d8c3e1c

Please sign in to comment.