Skip to content

Commit

Permalink
Add size check on the bitfield before allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
willscott committed Jan 26, 2023
1 parent ad0a9a1 commit 91b3d39
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
5 changes: 4 additions & 1 deletion hamt/shardeddir.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ func NewUnixFSHAMTShard(ctx context.Context, substrate dagpb.PBNode, data data.U
return nil, err
}
shardCache := make(map[ipld.Link]*_UnixFSHAMTShard, substrate.FieldLinks().Length())
bf := bitField(data)
bf, err := bitField(data)
if err != nil {
return nil, err
}
return &_UnixFSHAMTShard{
ctx: ctx,
_substrate: substrate,
Expand Down
12 changes: 9 additions & 3 deletions hamt/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,16 @@ func maxPadLength(nd data.UnixFSData) int {
return len(fmt.Sprintf("%X", nd.FieldFanout().Must().Int()-1))
}

func bitField(nd data.UnixFSData) bitfield.Bitfield {
bf := bitfield.NewBitfield(int(nd.FieldFanout().Must().Int()))
const maximumHamtWidth = 1 << 10

func bitField(nd data.UnixFSData) (bitfield.Bitfield, error) {
fanout := int(nd.FieldFanout().Must().Int())
if fanout > maximumHamtWidth {
return nil, fmt.Errorf("hamt witdh (%d) exceed maximum allowed (%d)", fanout, maximumHamtWidth)
}
bf := bitfield.NewBitfield(fanout)
bf.SetBytes(nd.FieldData().Must().Bytes())
return bf
return bf, nil
}

func checkLogTwo(v int) error {
Expand Down

0 comments on commit 91b3d39

Please sign in to comment.