Skip to content

Commit bce2393

Browse files
authored
Panic due to nil maps (#3042)
* query/query.go: var assignment was missing map init varValue assignments were missing map initialization for Vars field causing panic when accessed later in fillVars(). * query/query0_test.go: added tests that found the bug
1 parent 06df966 commit bce2393

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

query/query.go

+2
Original file line numberDiff line numberDiff line change
@@ -1495,6 +1495,7 @@ func (sg *SubGraph) populateUidValVar(doneVars map[string]varValue, sgPath []*Su
14951495
doneVars[sg.Params.Var] = varValue{
14961496
strList: sg.valueMatrix,
14971497
path: sgPath,
1498+
Vals: make(map[uint64]types.Val),
14981499
}
14991500
} else if len(sg.counts) > 0 {
15001501
// This implies it is a value variable.
@@ -1532,6 +1533,7 @@ func (sg *SubGraph) populateUidValVar(doneVars map[string]varValue, sgPath []*Su
15321533
doneVars[sg.Params.Var] = varValue{
15331534
Uids: uids,
15341535
path: sgPath,
1536+
Vals: make(map[uint64]types.Val),
15351537
}
15361538
return nil
15371539
}

query/query0_test.go

+31
Original file line numberDiff line numberDiff line change
@@ -1728,6 +1728,37 @@ func TestCountUidToVarCombinedWithNormalVar(t *testing.T) {
17281728
require.JSONEq(t, `{"data": {"me":[{"score": 5}]}}`, js)
17291729
}
17301730

1731+
func TestDefaultValueVar1(t *testing.T) {
1732+
query := `
1733+
{
1734+
var(func: has(pred)) {
1735+
n as uid
1736+
cnt as count(_predicate_)
1737+
}
1738+
1739+
data(func: uid(n)) @filter(gt(val(cnt), 4)) {
1740+
expand(_all_)
1741+
}
1742+
}`
1743+
js := processQueryNoErr(t, query)
1744+
require.JSONEq(t, `{"data": {"data":[]}}`, js)
1745+
}
1746+
1747+
func TestDefaultValueVar2(t *testing.T) {
1748+
query := `
1749+
{
1750+
var(func: uid(0x1)) {
1751+
cnt as _predicate_
1752+
}
1753+
1754+
data(func: uid(0x1)) {
1755+
val(cnt)
1756+
}
1757+
}`
1758+
js := processQueryNoErr(t, query)
1759+
require.JSONEq(t, `{"data": {"data":[]}}`, js)
1760+
}
1761+
17311762
func TestMain(m *testing.M) {
17321763
populateCluster()
17331764
os.Exit(m.Run())

0 commit comments

Comments
 (0)