Skip to content

Commit

Permalink
fix: allocate new slices for component nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Nov 29, 2022
1 parent 061699f commit a65a17b
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ func DecodeBytes(na ipld.NodeAssembler, src []byte) error {
return fmt.Errorf("protobuf: (PBNode) duplicate Data section")
}

chunk, n := protowire.ConsumeBytes(remaining)
if n < 0 {
return protowire.ParseError(n)
chunk, n, err := consumeBytes(remaining, true)
if err != nil {
return err
}
remaining = remaining[n:]

Expand All @@ -97,9 +97,9 @@ func DecodeBytes(na ipld.NodeAssembler, src []byte) error {
haveData = true

case 2:
chunk, n := protowire.ConsumeBytes(remaining)
if n < 0 {
return protowire.ParseError(n)
chunk, n, err := consumeBytes(remaining, false) // no need to alloc here, unmarshalLink will do that
if err != nil {
return err
}
remaining = remaining[n:]

Expand Down Expand Up @@ -188,9 +188,9 @@ func unmarshalLink(remaining []byte, ma ipld.MapAssembler) error {
return fmt.Errorf("protobuf: (PBLink) wrong wireType (%d) for Hash", wireType)
}

chunk, n := protowire.ConsumeBytes(remaining)
if n < 0 {
return protowire.ParseError(n)
chunk, n, err := consumeBytes(remaining, true)
if err != nil {
return err
}
remaining = remaining[n:]

Expand All @@ -217,9 +217,9 @@ func unmarshalLink(remaining []byte, ma ipld.MapAssembler) error {
return fmt.Errorf("protobuf: (PBLink) wrong wireType (%d) for Name", wireType)
}

chunk, n := protowire.ConsumeBytes(remaining)
if n < 0 {
return protowire.ParseError(n)
chunk, n, err := consumeBytes(remaining, true)
if err != nil {
return err
}
remaining = remaining[n:]

Expand Down Expand Up @@ -264,3 +264,16 @@ func unmarshalLink(remaining []byte, ma ipld.MapAssembler) error {

return nil
}

// consumeBytes will read a Bytes section from the begining of the provided slice, return it and the number
// of bytes consumed in the process and optionally return it as a newly allocated slice
func consumeBytes(byts []byte, alloc bool) ([]byte, int, error) {
chunk, n := protowire.ConsumeBytes(byts)
if n < 0 {
return nil, 0, protowire.ParseError(n)
}
if alloc {
return append(make([]byte, 0, n), chunk...), n, nil
}
return chunk, n, nil
}

0 comments on commit a65a17b

Please sign in to comment.