From aacea4070fc0c592d2eb7ae1e62b9c029671314f Mon Sep 17 00:00:00 2001 From: Martin Martinez Rivera Date: Fri, 12 Jul 2019 13:17:52 -0700 Subject: [PATCH 1/2] Fix crash when trying to use shortest path with a password predicate. The crash happens because querying this predicate does not populate the uid matrix, resulting in an out-of-index error. This fix just checks the size of the uid matrix before trying to access it. --- query/query3_test.go | 20 +++++++++++++++++++- query/shortest.go | 6 ++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/query/query3_test.go b/query/query3_test.go index 1bddfe835d6..1a3036d684a 100644 --- a/query/query3_test.go +++ b/query/query3_test.go @@ -482,7 +482,6 @@ func TestTwoShortestPathMinWeight(t *testing.T) { } func TestShortestPath(t *testing.T) { - query := ` { A as shortest(from:0x01, to:31) { @@ -517,6 +516,25 @@ func TestShortestPathRev(t *testing.T) { js) } +// Regression test for https://github.com/dgraph-io/dgraph/issues/3657. +func TestShortestPathPassword(t *testing.T) { + query := ` + { + A as shortest(from:0x01, to:31) { + password + friend + } + + me(func: uid( A)) { + name + } + }` + js := processQueryNoErr(t, query) + require.JSONEq(t, + `{"data": {"_path_":[{"uid":"0x1", "_weight_": 1, "friend":{"uid":"0x1f"}}], + "me":[{"name":"Michonne"},{"name":"Andrea"}]}}`, js) +} + func TestFacetVarRetrieval(t *testing.T) { query := ` diff --git a/query/shortest.go b/query/shortest.go index d184d72185d..3bd4891fd09 100644 --- a/query/shortest.go +++ b/query/shortest.go @@ -183,6 +183,12 @@ func (sg *SubGraph) expandOut(ctx context.Context, // Send the destuids in res chan. for mIdx, fromUID := range subgraph.SrcUIDs.Uids { + // This can happen when trying to go traverse a predicate of type password + // for example. + if len(subgraph.uidMatrix) < mIdx+1 { + continue + } + for lIdx, toUID := range subgraph.uidMatrix[mIdx].Uids { if adjacencyMap[fromUID] == nil { adjacencyMap[fromUID] = make(map[uint64]mapItem) From 61e9e64951f297aeb5fcb6e1e46bbf8e036d6770 Mon Sep 17 00:00:00 2001 From: Martin Martinez Rivera Date: Fri, 12 Jul 2019 13:37:22 -0700 Subject: [PATCH 2/2] Update query/shortest.go Co-Authored-By: Francesc Campoy --- query/shortest.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/query/shortest.go b/query/shortest.go index 3bd4891fd09..b57f87b0dd8 100644 --- a/query/shortest.go +++ b/query/shortest.go @@ -185,7 +185,7 @@ func (sg *SubGraph) expandOut(ctx context.Context, for mIdx, fromUID := range subgraph.SrcUIDs.Uids { // This can happen when trying to go traverse a predicate of type password // for example. - if len(subgraph.uidMatrix) < mIdx+1 { + if mIdx >= len(subgraph.uidMatrix) { continue }