Skip to content

Commit ed44bbd

Browse files
committed
fix nil deref on Dispose() in
1 parent fff320b commit ed44bbd

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

enginetest/memory_engine_test.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -200,23 +200,29 @@ func TestSingleQueryPrepared(t *testing.T) {
200200

201201
// Convenience test for debugging a single query. Unskip and set to the desired query.
202202
func TestSingleScript(t *testing.T) {
203-
t.Skip()
203+
//t.Skip()
204204
var scripts = []queries.ScriptTest{
205205
{
206-
Name: "AS OF propagates to nested CALLs",
207-
SetUpScript: []string{},
206+
// https://github.com/dolthub/dolt/issues/9987
207+
Name: "GROUP BY nil pointer dereference in Dispose when Next() never called",
208+
SetUpScript: []string{
209+
"CREATE TABLE test_table (id INT PRIMARY KEY, value INT, category VARCHAR(50))",
210+
"INSERT INTO test_table VALUES (1, 100, 'A'), (2, 200, 'B'), (3, 300, 'A')",
211+
},
208212
Assertions: []queries.ScriptTestAssertion{
209213
{
210-
Query: "create procedure create_proc() create table t (i int primary key, j int);",
211-
Expected: []sql.Row{
212-
{types.NewOkResult(0)},
213-
},
214+
// LIMIT 0 causes the iterator to close without ever calling Next() on groupByIter
215+
// This leaves all buffer elements as nil, causing panic in Dispose()
216+
Query: "SELECT category, SUM(value) FROM test_table GROUP BY category LIMIT 0",
217+
Expected: []sql.Row{},
214218
},
215219
{
216-
Query: "call create_proc()",
217-
Expected: []sql.Row{
218-
{types.NewOkResult(0)},
219-
},
220+
Query: "SELECT category, COUNT(*) FROM test_table GROUP BY category LIMIT 0",
221+
Expected: []sql.Row{},
222+
},
223+
{
224+
Query: "SELECT SUM(value) FROM test_table LIMIT 0",
225+
Expected: []sql.Row{},
220226
},
221227
},
222228
},

sql/rowexec/agg.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ func (i *groupByIter) Close(ctx *sql.Context) error {
103103

104104
func (i *groupByIter) Dispose() {
105105
for _, b := range i.buf {
106-
b.Dispose()
106+
if b != nil {
107+
b.Dispose()
108+
}
107109
}
108110
}
109111

@@ -234,7 +236,9 @@ func (i *groupByGroupingIter) Dispose() {
234236
bs, _ := i.get(k)
235237
if bs != nil {
236238
for _, b := range bs {
237-
b.Dispose()
239+
if b != nil {
240+
b.Dispose()
241+
}
238242
}
239243
}
240244
}

0 commit comments

Comments
 (0)