Repository: kythe/kythe
Pull request #4835 (2021-02-22 16:13:04)

fix(tooling): guard adding nil Entry to EntrySet


Patch:
diff --git a/kythe/go/storage/entryset/BUILD b/kythe/go/storage/entryset/BUILD
index f5bf304c1..3cc9f5a65 100644
--- a/kythe/go/storage/entryset/BUILD
+++ b/kythe/go/storage/entryset/BUILD
@@ -11,7 +11,8 @@ go_library(
         "//kythe/proto:entryset_go_proto",
         "//kythe/proto:internal_go_proto",
         "//kythe/proto:storage_go_proto",
-        "@com_github_golang_protobuf//proto:go_default_library",
+        "@com_github_pkg_errors//:go_default_library",
+        "@org_golang_google_protobuf//proto:go_default_library",
     ],
 )
 
@@ -24,5 +25,6 @@ go_test(
         "//kythe/go/test/testutil",
         "//kythe/go/util/compare",
         "//kythe/go/util/kytheuri",
+        "@org_golang_google_protobuf//encoding/prototext:go_default_library",
     ],
 )
diff --git a/kythe/go/storage/entryset/entryset.go b/kythe/go/storage/entryset/entryset.go
index 4752237c3..fd3efb49a 100644
--- a/kythe/go/storage/entryset/entryset.go
+++ b/kythe/go/storage/entryset/entryset.go
@@ -63,10 +63,12 @@ import (
 	"io"
 	"sort"
 
-	"github.com/golang/protobuf/proto"
 	"kythe.io/kythe/go/util/kytheuri"
 	"kythe.io/kythe/go/util/schema/edges"
 
+	"github.com/pkg/errors"
+	"google.golang.org/protobuf/proto"
+
 	espb "kythe.io/kythe/proto/entryset_go_proto"
 	intpb "kythe.io/kythe/proto/internal_go_proto"
 	spb "kythe.io/kythe/proto/storage_go_proto"
@@ -150,7 +152,10 @@ func (s *Set) Stats() *Stats {
 // set, such entries are simply discarded. It is therefore safe to ignore an
 // error from this method if you want to drop invalid data.
 func (s *Set) Add(e *spb.Entry) error {
-	if (e.Target == nil) != (e.EdgeKind == "") {
+	if e == nil {
+		s.addErrors++
+		return errors.New("entryset: nil entry")
+	} else if (e.Target == nil) != (e.EdgeKind == "") {
 		s.addErrors++
 		return fmt.Errorf("entryset: invalid entry: target=%v/kind=%v", e.Target == nil, e.EdgeKind == "")
 	}


Test patch:
diff --git a/kythe/go/storage/entryset/entryset_test.go b/kythe/go/storage/entryset/entryset_test.go
index f00d0b8c8..ffe1f8c1b 100644
--- a/kythe/go/storage/entryset/entryset_test.go
+++ b/kythe/go/storage/entryset/entryset_test.go
@@ -22,11 +22,12 @@ import (
 	"strings"
 	"testing"
 
-	"github.com/golang/protobuf/proto"
-
 	"kythe.io/kythe/go/util/compare"
 	"kythe.io/kythe/go/util/kytheuri"
 
+	"google.golang.org/protobuf/encoding/prototext"
+	"google.golang.org/protobuf/proto"
+
 	espb "kythe.io/kythe/proto/entryset_go_proto"
 	intpb "kythe.io/kythe/proto/internal_go_proto"
 	spb "kythe.io/kythe/proto/storage_go_proto"
@@ -178,7 +179,7 @@ func TestSplit(t *testing.T) {
 		t.Errorf("Add(%+v) failed: %v", big, err)
 	}
 	enc := s.Encode()
-	t.Logf("After encoding:\n%s", proto.MarshalTextString(enc))
+	t.Logf("After encoding:\n%s", prototext.Format(enc))
 
 	dec, err := Decode(s.Encode())
 	if err != nil {
@@ -189,7 +190,7 @@ func TestSplit(t *testing.T) {
 		numEntries++
 		if !proto.Equal(ent, big) {
 			t.Errorf("Visited entry:\ngot  %s\nwant %s",
-				proto.MarshalTextString(ent), proto.MarshalTextString(big))
+				prototext.Format(ent), prototext.Format(big))
 		}
 		return true
 	})