Skip to content

Commit 7d11922

Browse files
author
Harshil Goel
authored
fix(core): fix unmarshal protobuf when len val is 0 (#9347)
1 parent e4b7425 commit 7d11922

2 files changed

Lines changed: 39 additions & 2 deletions

File tree

schema/schema.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,8 +605,9 @@ func loadFromDB(ctx context.Context, loadType int) error {
605605
err := item.Value(func(val []byte) error {
606606
if len(val) == 0 {
607607
s = pb.SchemaUpdate{Predicate: pk.Attr, ValueType: pb.Posting_DEFAULT}
608+
} else {
609+
x.Checkf(proto.Unmarshal(val, &s), "Error while loading schema from db")
608610
}
609-
x.Checkf(proto.Unmarshal(val, &s), "Error while loading schema from db")
610611
State().Set(pk.Attr, &s)
611612
return nil
612613
})
@@ -616,8 +617,9 @@ func loadFromDB(ctx context.Context, loadType int) error {
616617
err := item.Value(func(val []byte) error {
617618
if len(val) == 0 {
618619
t = pb.TypeUpdate{TypeName: pk.Attr}
620+
} else {
621+
x.Checkf(proto.Unmarshal(val, &t), "Error while loading types from db")
619622
}
620-
x.Checkf(proto.Unmarshal(val, &t), "Error while loading types from db")
621623
State().SetType(pk.Attr, &t)
622624
return nil
623625
})

worker/sort_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,41 @@ func writePostingListToDisk(kvs []*bpb.KV) error {
5555
return writer.Flush()
5656
}
5757

58+
func TestEmptyTypeSchema(t *testing.T) {
59+
dir, err := os.MkdirTemp("", "storetest_")
60+
x.Check(err)
61+
defer os.RemoveAll(dir)
62+
63+
opt := badger.DefaultOptions(dir)
64+
ps, err := badger.OpenManaged(opt)
65+
x.Check(err)
66+
pstore = ps
67+
posting.Init(ps, 0, false)
68+
Init(ps)
69+
70+
typeName := "1-temp"
71+
ts := uint64(10)
72+
txn := pstore.NewTransactionAt(ts, true)
73+
defer txn.Discard()
74+
e := &badger.Entry{
75+
Key: x.TypeKey(typeName),
76+
Value: make([]byte, 0),
77+
UserMeta: posting.BitSchemaPosting,
78+
}
79+
require.Nil(t, txn.SetEntry(e.WithDiscard()))
80+
require.Nil(t, txn.CommitAt(ts, nil))
81+
82+
schema.Init(ps)
83+
require.Nil(t, schema.LoadFromDb(context.Background()))
84+
85+
req := &pb.SchemaRequest{}
86+
types, err := GetTypes(context.Background(), req)
87+
require.Nil(t, err)
88+
89+
require.Equal(t, 1, len(types))
90+
x.ParseNamespaceAttr(types[0].TypeName)
91+
}
92+
5893
func TestMultipleTxnListCount(t *testing.T) {
5994
dir, err := os.MkdirTemp("", "storetest_")
6095
x.Check(err)

0 commit comments

Comments
 (0)