Skip to content

Commit ee6e898

Browse files
committed
fix unmarshaling of empty nodes
1 parent 8aabc0c commit ee6e898

File tree

5 files changed

+30
-21
lines changed

5 files changed

+30
-21
lines changed

cbor_gen.go

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ func (t *Node) UnmarshalCBOR(br io.Reader) error {
8383
return err
8484
}
8585
t.Bitfield = big.NewInt(0).SetBytes(buf)
86+
} else {
87+
t.Bitfield = big.NewInt(0)
8688
}
8789
// t.t.Pointers ([]*hamt.Pointer)
8890

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

128-
// t.t.Value (typegen.Deferred)
129-
130+
// t.t.Value (cbg.Deferred)
130131
if err := t.Value.MarshalCBOR(w); err != nil {
131132
return err
132133
}
@@ -149,28 +150,15 @@ func (t *KV) UnmarshalCBOR(br io.Reader) error {
149150

150151
// t.t.Key (string)
151152

152-
maj, extra, err = cbg.CborReadHeader(br)
153-
if err != nil {
154-
return err
155-
}
156-
157-
if maj != cbg.MajTextString {
158-
return fmt.Errorf("expected cbor type 'text string' in input")
159-
}
160-
161-
if extra > 256*1024 {
162-
return fmt.Errorf("string in cbor input too long")
163-
}
164-
165153
{
166-
buf := make([]byte, extra)
167-
if _, err := io.ReadFull(br, buf); err != nil {
154+
sval, err := cbg.ReadString(br)
155+
if err != nil {
168156
return err
169157
}
170158

171-
t.Key = string(buf)
159+
t.Key = string(sval)
172160
}
173-
// t.t.Value (typegen.Deferred)
161+
// t.t.Value (cbg.Deferred)
174162

175163
t.Value = new(cbg.Deferred)
176164

gen/main.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ func main() {
2222
t := []interface{}{
2323
hamt.Node{},
2424
hamt.KV{},
25-
hamt.Pointer{},
2625
}
2726

2827
for _, t := range t {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ require (
77
github.com/multiformats/go-multihash v0.0.1
88
github.com/polydawn/refmt v0.0.0-20190221155625-df39d6c2d992
99
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72
10-
github.com/whyrusleeping/cbor-gen v0.0.0-20190822231004-8db835b09a5a
10+
github.com/whyrusleeping/cbor-gen v0.0.0-20190910031516-c1cbffdb01bb
1111
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7
1212
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ github.com/whyrusleeping/cbor-gen v0.0.0-20190822002707-4e02357de5c1 h1:wDIXmhgP
3939
github.com/whyrusleeping/cbor-gen v0.0.0-20190822002707-4e02357de5c1/go.mod h1:xdlJQaiqipF0HW+Mzpg7XRM3fWbGvfgFlcppuvlkIvY=
4040
github.com/whyrusleeping/cbor-gen v0.0.0-20190822231004-8db835b09a5a h1:9oEQR9eq2H2JDmglMcrCa+TxUEYy3HKSiNoLIkPNy/U=
4141
github.com/whyrusleeping/cbor-gen v0.0.0-20190822231004-8db835b09a5a/go.mod h1:xdlJQaiqipF0HW+Mzpg7XRM3fWbGvfgFlcppuvlkIvY=
42+
github.com/whyrusleeping/cbor-gen v0.0.0-20190910031516-c1cbffdb01bb h1:8yBVx6dgk1GfkiWOQ+RbeDDBLCOZxOtmZ949O2uj5H4=
43+
github.com/whyrusleeping/cbor-gen v0.0.0-20190910031516-c1cbffdb01bb/go.mod h1:xdlJQaiqipF0HW+Mzpg7XRM3fWbGvfgFlcppuvlkIvY=
4244
golang.org/x/crypto v0.0.0-20190211182817-74369b46fc67 h1:ng3VDlRp5/DHpSWl02R4rM9I+8M2rhmsuLwAMmkLQWE=
4345
golang.org/x/crypto v0.0.0-20190211182817-74369b46fc67/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
4446
golang.org/x/sys v0.0.0-20190219092855-153ac476189d h1:Z0Ahzd7HltpJtjAHHxX8QFP3j1yYgiuvjbjRzDj/KH0=

hamt_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,26 @@ func nodesEqual(t *testing.T, store *CborIpldStore, n1, n2 *Node) bool {
382382
return n1Cid.Equals(n2Cid)
383383
}
384384

385+
func TestReloadEmpty(t *testing.T) {
386+
ctx := context.Background()
387+
cs := NewCborStore()
388+
389+
n := NewNode(cs)
390+
c, err := cs.Put(ctx, n)
391+
if err != nil {
392+
t.Fatal(err)
393+
}
394+
395+
on, err := LoadNode(ctx, cs, c)
396+
if err != nil {
397+
t.Fatal(err)
398+
}
399+
400+
if err := on.Set(ctx, "foo", "bar"); err != nil {
401+
t.Fatal(err)
402+
}
403+
}
404+
385405
func TestCopy(t *testing.T) {
386406
ctx := context.Background()
387407
cs := NewCborStore()

0 commit comments

Comments
 (0)