Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

Fix: panic when childer is nil #127

Merged
merged 2 commits into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions hamt/hamt.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,12 @@ type Shard struct {

// NewShard creates a new, empty HAMT shard with the given size.
func NewShard(dserv ipld.DAGService, size int) (*Shard, error) {
ds, err := makeShard(dserv, size)
return NewShardValue(dserv, size, "", nil)
}

// NewShardValue creates a new, empty HAMT shard with the given key, value and size.
func NewShardValue(dserv ipld.DAGService, size int, key string, value *ipld.Link) (*Shard, error) {
ds, err := makeShard(dserv, size, key, value)
if err != nil {
return nil, err
}
Expand All @@ -96,7 +101,7 @@ func NewShard(dserv ipld.DAGService, size int) (*Shard, error) {
return ds, nil
}

func makeShard(ds ipld.DAGService, size int) (*Shard, error) {
func makeShard(ds ipld.DAGService, size int, key string, val *ipld.Link) (*Shard, error) {
lg2s, err := Logtwo(size)
if err != nil {
return nil, err
Expand All @@ -109,6 +114,9 @@ func makeShard(ds ipld.DAGService, size int) (*Shard, error) {
childer: newChilder(ds, size),
tableSize: size,
dserv: ds,

key: key,
val: val,
}

s.childer.sd = s
Expand Down Expand Up @@ -138,7 +146,7 @@ func NewHamtFromDag(dserv ipld.DAGService, nd ipld.Node) (*Shard, error) {

size := int(fsn.Fanout())

ds, err := makeShard(dserv, size)
ds, err := makeShard(dserv, size, "", nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -214,7 +222,7 @@ func (ds *Shard) Node() (ipld.Node, error) {

func (ds *Shard) makeShardValue(lnk *ipld.Link) (*Shard, error) {
lnk2 := *lnk
s, err := makeShard(ds.dserv, ds.tableSize)
s, err := makeShard(ds.dserv, ds.tableSize, "", nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -795,7 +803,11 @@ func (s *childer) insert(key string, lnk *ipld.Link, idx int) error {

lnk.Name = s.sd.linkNamePrefix(idx) + key
i := s.sliceIndex(idx)
sd := &Shard{key: key, val: lnk}

sd, err := NewShardValue(s.dserv, 256, key, lnk)
if err != nil {
return err
}

s.children = append(s.children[:i], append([]*Shard{sd}, s.children[i:]...)...)
s.links = append(s.links[:i], append([]*ipld.Link{nil}, s.links[i:]...)...)
Expand Down
32 changes: 32 additions & 0 deletions hamt/hamt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,38 @@ func TestDuplicateAddShard(t *testing.T) {
}
}

// fix https://github.com/ipfs/kubo/issues/9063
func TestSetLink(t *testing.T) {
ds := mdtest.Mock()
dir, _ := NewShard(ds, 256)
_, s, err := makeDir(ds, 300)
if err != nil {
t.Fatal(err)
}

lnk, err := s.Link()
if err != nil {
t.Fatal(err)
}

ctx := context.Background()

err = dir.SetLink(ctx, "test", lnk)
if err != nil {
t.Fatal(err)
}

if len(dir.childer.children) != 1 {
t.Fatal("no child")
}

for _, sh := range dir.childer.children {
if sh.childer == nil {
t.Fatal("no childer on shard")
}
}
}

func TestLoadFailsFromNonShard(t *testing.T) {
ds := mdtest.Mock()
nd := ft.EmptyDirNode()
Expand Down