Skip to content

Commit

Permalink
fix(ipld/plugin): verify that data written to namespaceHasher has leg…
Browse files Browse the repository at this point in the history
…it size
  • Loading branch information
Wondertan committed Aug 2, 2022
1 parent 9e45ada commit 7f191a4
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions ipld/plugin/nmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,25 @@ func NewNamespaceHasher(hasher *nmt.Hasher) hash.Hash {
}

func (n *namespaceHasher) Write(data []byte) (int, error) {
n.tp = data[0]
ln, nln, hln := len(data), int(n.NamespaceLen), n.Hash.Size()
innerNodeSize, leafNodeSize := (nln*2+hln)*2, nln+consts.ShareSize
switch ln {
default:
return 0, fmt.Errorf("wrong data size")
// inner node
case innerNodeSize + 1: // with type byte
fallthrough
case innerNodeSize:
n.tp = nmt.NodePrefix
// leaf data node
case leafNodeSize:
fallthrough
case leafNodeSize + 1: // with type byte
n.tp = nmt.LeafPrefix
}

n.data = data[1:]
return len(data), nil
return ln, nil
}

func (n *namespaceHasher) Sum([]byte) []byte {
Expand Down

0 comments on commit 7f191a4

Please sign in to comment.