From 8362946fb1ffdd4c54d26ac938fa5234a0117b39 Mon Sep 17 00:00:00 2001 From: Harshil Goel <54325286+harshil-goel@users.noreply.github.com> Date: Tue, 18 Mar 2025 16:27:49 +0530 Subject: [PATCH] fix(core): fix unmarshal protobuf when len val is 0 (#9347) --- schema/schema.go | 6 ++++-- worker/sort_test.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/schema/schema.go b/schema/schema.go index d3ecd4d3c45..398acec7ecc 100644 --- a/schema/schema.go +++ b/schema/schema.go @@ -616,8 +616,9 @@ func loadFromDB(ctx context.Context, loadType int) error { err := item.Value(func(val []byte) error { if len(val) == 0 { s = pb.SchemaUpdate{Predicate: pk.Attr, ValueType: pb.Posting_DEFAULT} + } else { + x.Checkf(proto.Unmarshal(val, &s), "Error while loading schema from db") } - x.Checkf(proto.Unmarshal(val, &s), "Error while loading schema from db") State().Set(pk.Attr, &s) return nil }) @@ -627,8 +628,9 @@ func loadFromDB(ctx context.Context, loadType int) error { err := item.Value(func(val []byte) error { if len(val) == 0 { t = pb.TypeUpdate{TypeName: pk.Attr} + } else { + x.Checkf(proto.Unmarshal(val, &t), "Error while loading types from db") } - x.Checkf(proto.Unmarshal(val, &t), "Error while loading types from db") State().SetType(pk.Attr, &t) return nil }) diff --git a/worker/sort_test.go b/worker/sort_test.go index f69519281bf..b2d6eb80170 100644 --- a/worker/sort_test.go +++ b/worker/sort_test.go @@ -66,6 +66,41 @@ func writePostingListToDisk(kvs []*bpb.KV) error { return writer.Flush() } +func TestEmptyTypeSchema(t *testing.T) { + dir, err := os.MkdirTemp("", "storetest_") + x.Check(err) + defer os.RemoveAll(dir) + + opt := badger.DefaultOptions(dir) + ps, err := badger.OpenManaged(opt) + x.Check(err) + pstore = ps + posting.Init(ps, 0, false) + Init(ps) + + typeName := "1-temp" + ts := uint64(10) + txn := pstore.NewTransactionAt(ts, true) + defer txn.Discard() + e := &badger.Entry{ + Key: x.TypeKey(typeName), + Value: make([]byte, 0), + UserMeta: posting.BitSchemaPosting, + } + require.Nil(t, txn.SetEntry(e.WithDiscard())) + require.Nil(t, txn.CommitAt(ts, nil)) + + schema.Init(ps) + require.Nil(t, schema.LoadFromDb(context.Background())) + + req := &pb.SchemaRequest{} + types, err := GetTypes(context.Background(), req) + require.Nil(t, err) + + require.Equal(t, 1, len(types)) + x.ParseNamespaceAttr(types[0].TypeName) +} + func TestMultipleTxnListCount(t *testing.T) { dir, err := os.MkdirTemp("", "storetest_") x.Check(err)