Skip to content

Commit 91b3d39

Browse files
committed
Add size check on the bitfield before allocation
1 parent ad0a9a1 commit 91b3d39

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

hamt/shardeddir.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ func NewUnixFSHAMTShard(ctx context.Context, substrate dagpb.PBNode, data data.U
4242
return nil, err
4343
}
4444
shardCache := make(map[ipld.Link]*_UnixFSHAMTShard, substrate.FieldLinks().Length())
45-
bf := bitField(data)
45+
bf, err := bitField(data)
46+
if err != nil {
47+
return nil, err
48+
}
4649
return &_UnixFSHAMTShard{
4750
ctx: ctx,
4851
_substrate: substrate,

hamt/util.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,16 @@ func maxPadLength(nd data.UnixFSData) int {
8888
return len(fmt.Sprintf("%X", nd.FieldFanout().Must().Int()-1))
8989
}
9090

91-
func bitField(nd data.UnixFSData) bitfield.Bitfield {
92-
bf := bitfield.NewBitfield(int(nd.FieldFanout().Must().Int()))
91+
const maximumHamtWidth = 1 << 10
92+
93+
func bitField(nd data.UnixFSData) (bitfield.Bitfield, error) {
94+
fanout := int(nd.FieldFanout().Must().Int())
95+
if fanout > maximumHamtWidth {
96+
return nil, fmt.Errorf("hamt witdh (%d) exceed maximum allowed (%d)", fanout, maximumHamtWidth)
97+
}
98+
bf := bitfield.NewBitfield(fanout)
9399
bf.SetBytes(nd.FieldData().Must().Bytes())
94-
return bf
100+
return bf, nil
95101
}
96102

97103
func checkLogTwo(v int) error {

0 commit comments

Comments
 (0)