Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Issue-1368]: Panic in serializedPath.HasPrefix #1371

Merged
merged 11 commits into from
Apr 20, 2023
Prev Previous commit
Next Next commit
extra fix
  • Loading branch information
dboehm-avalabs committed Apr 18, 2023
commit 773216697b686eaab35e9ad8a545bc23c5ed1938
7 changes: 4 additions & 3 deletions x/merkledb/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@ func (s SerializedPath) deserialize() path {

// HasPrefix returns true iff [prefix] is a prefix of [s] or equal to it.
func (s SerializedPath) HasPrefix(prefix SerializedPath) bool {
if s.NibbleLength < prefix.NibbleLength {
prefixValue := prefix.Value
prefixLength := len(prefix.Value)
if s.NibbleLength < prefix.NibbleLength || len(s.Value) < prefixLength {
return false
}
prefixValue := prefix.Value
if !prefix.hasOddLength() {
return bytes.HasPrefix(s.Value, prefixValue)
}
reducedSize := len(prefixValue) - 1
reducedSize := prefixLength - 1

// the input was invalid so just return false
if reducedSize < 0 {
Expand Down
4 changes: 4 additions & 0 deletions x/merkledb/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ func Test_SerializedPath_HasPrefix_BadInput(t *testing.T) {
a := SerializedPath{Value: []byte{}}
b := SerializedPath{Value: []byte{}, NibbleLength: 1}
require.False(t, a.HasPrefix(b))

a = SerializedPath{Value: []byte{}, NibbleLength: 10}
b = SerializedPath{Value: []byte{0x10}, NibbleLength: 1}
require.False(t, a.HasPrefix(b))
}

func Test_SerializedPath_Equal(t *testing.T) {
Expand Down