Skip to content

Commit f7aa133

Browse files
authored
Merge pull request #3277 from dolthub/elian/9984
dolthub/dolt#9984: Fix `nil` dereference by `Dispose()` in `GROUP BY` iterator when `Next()` is never called
2 parents b9d804f + e1e23e4 commit f7aa133

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

enginetest/queries/script_queries.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,30 @@ type ScriptTestAssertion struct {
122122
// Unlike other engine tests, ScriptTests must be self-contained. No other tables are created outside the definition of
123123
// the tests.
124124
var ScriptTests = []ScriptTest{
125+
{
126+
// https://github.com/dolthub/dolt/issues/9987
127+
Name: "GROUP BY nil pointer dereference in Dispose when Next() never called",
128+
SetUpScript: []string{
129+
"CREATE TABLE test_table (id INT PRIMARY KEY, value INT, category VARCHAR(50))",
130+
"INSERT INTO test_table VALUES (1, 100, 'A'), (2, 200, 'B'), (3, 300, 'A')",
131+
},
132+
Assertions: []ScriptTestAssertion{
133+
{
134+
// LIMIT 0 causes the iterator to close without ever calling Next() on groupByIter
135+
// This leaves all buffer elements as nil causing panic in Dispose(), or empty depending data struct
136+
Query: "SELECT category, SUM(value) FROM test_table GROUP BY category LIMIT 0",
137+
Expected: []sql.Row{},
138+
},
139+
{
140+
Query: "SELECT category, COUNT(*) FROM test_table GROUP BY category LIMIT 0",
141+
Expected: []sql.Row{},
142+
},
143+
{
144+
Query: "SELECT SUM(value) FROM test_table LIMIT 0",
145+
Expected: []sql.Row{},
146+
},
147+
},
148+
},
125149
{
126150
// https://github.com/dolthub/dolt/issues/9935
127151
Dialect: "mysql",

sql/rowexec/agg.go

Lines changed: 3 additions & 1 deletion
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

0 commit comments

Comments
 (0)