Skip to content

Commit

Permalink
fix unmarshaling of empty nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
whyrusleeping committed Sep 10, 2019
1 parent 8aabc0c commit ee6e898
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 21 deletions.
26 changes: 7 additions & 19 deletions cbor_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ func (t *Node) UnmarshalCBOR(br io.Reader) error {
return err
}
t.Bitfield = big.NewInt(0).SetBytes(buf)
} else {
t.Bitfield = big.NewInt(0)
}
// t.t.Pointers ([]*hamt.Pointer)

Expand Down Expand Up @@ -125,8 +127,7 @@ func (t *KV) MarshalCBOR(w io.Writer) error {
return err
}

// t.t.Value (typegen.Deferred)

// t.t.Value (cbg.Deferred)
if err := t.Value.MarshalCBOR(w); err != nil {
return err
}
Expand All @@ -149,28 +150,15 @@ func (t *KV) UnmarshalCBOR(br io.Reader) error {

// t.t.Key (string)

maj, extra, err = cbg.CborReadHeader(br)
if err != nil {
return err
}

if maj != cbg.MajTextString {
return fmt.Errorf("expected cbor type 'text string' in input")
}

if extra > 256*1024 {
return fmt.Errorf("string in cbor input too long")
}

{
buf := make([]byte, extra)
if _, err := io.ReadFull(br, buf); err != nil {
sval, err := cbg.ReadString(br)
if err != nil {
return err
}

t.Key = string(buf)
t.Key = string(sval)
}
// t.t.Value (typegen.Deferred)
// t.t.Value (cbg.Deferred)

t.Value = new(cbg.Deferred)

Expand Down
1 change: 0 additions & 1 deletion gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ func main() {
t := []interface{}{
hamt.Node{},
hamt.KV{},
hamt.Pointer{},
}

for _, t := range t {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ require (
github.com/multiformats/go-multihash v0.0.1
github.com/polydawn/refmt v0.0.0-20190221155625-df39d6c2d992
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72
github.com/whyrusleeping/cbor-gen v0.0.0-20190822231004-8db835b09a5a
github.com/whyrusleeping/cbor-gen v0.0.0-20190910031516-c1cbffdb01bb
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ github.com/whyrusleeping/cbor-gen v0.0.0-20190822002707-4e02357de5c1 h1:wDIXmhgP
github.com/whyrusleeping/cbor-gen v0.0.0-20190822002707-4e02357de5c1/go.mod h1:xdlJQaiqipF0HW+Mzpg7XRM3fWbGvfgFlcppuvlkIvY=
github.com/whyrusleeping/cbor-gen v0.0.0-20190822231004-8db835b09a5a h1:9oEQR9eq2H2JDmglMcrCa+TxUEYy3HKSiNoLIkPNy/U=
github.com/whyrusleeping/cbor-gen v0.0.0-20190822231004-8db835b09a5a/go.mod h1:xdlJQaiqipF0HW+Mzpg7XRM3fWbGvfgFlcppuvlkIvY=
github.com/whyrusleeping/cbor-gen v0.0.0-20190910031516-c1cbffdb01bb h1:8yBVx6dgk1GfkiWOQ+RbeDDBLCOZxOtmZ949O2uj5H4=
github.com/whyrusleeping/cbor-gen v0.0.0-20190910031516-c1cbffdb01bb/go.mod h1:xdlJQaiqipF0HW+Mzpg7XRM3fWbGvfgFlcppuvlkIvY=
golang.org/x/crypto v0.0.0-20190211182817-74369b46fc67 h1:ng3VDlRp5/DHpSWl02R4rM9I+8M2rhmsuLwAMmkLQWE=
golang.org/x/crypto v0.0.0-20190211182817-74369b46fc67/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/sys v0.0.0-20190219092855-153ac476189d h1:Z0Ahzd7HltpJtjAHHxX8QFP3j1yYgiuvjbjRzDj/KH0=
Expand Down
20 changes: 20 additions & 0 deletions hamt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,26 @@ func nodesEqual(t *testing.T, store *CborIpldStore, n1, n2 *Node) bool {
return n1Cid.Equals(n2Cid)
}

func TestReloadEmpty(t *testing.T) {
ctx := context.Background()
cs := NewCborStore()

n := NewNode(cs)
c, err := cs.Put(ctx, n)
if err != nil {
t.Fatal(err)
}

on, err := LoadNode(ctx, cs, c)
if err != nil {
t.Fatal(err)
}

if err := on.Set(ctx, "foo", "bar"); err != nil {
t.Fatal(err)
}
}

func TestCopy(t *testing.T) {
ctx := context.Background()
cs := NewCborStore()
Expand Down

0 comments on commit ee6e898

Please sign in to comment.