Skip to content

Commit 77d74ba

Browse files
committed
Fix potential leak.
1 parent 4142680 commit 77d74ba

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

Diff for: func.go

+18-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package sqlite3
22

33
import (
44
"context"
5+
"io"
56
"sync"
67

78
"github.com/tetratelabs/wazero/api"
@@ -85,13 +86,19 @@ func (c *Conn) CreateWindowFunction(name string, nArg int, flag FunctionFlag, fn
8586
var funcPtr ptr_t
8687
defer c.arena.mark()()
8788
namePtr := c.arena.string(name)
89+
call := "sqlite3_create_aggregate_function_go"
8890
if fn != nil {
91+
agg := fn()
92+
if c, ok := agg.(io.Closer); ok {
93+
if err := c.Close(); err != nil {
94+
return err
95+
}
96+
}
97+
if _, ok := agg.(WindowFunction); ok {
98+
call = "sqlite3_create_window_function_go"
99+
}
89100
funcPtr = util.AddHandle(c.ctx, fn)
90101
}
91-
call := "sqlite3_create_aggregate_function_go"
92-
if _, ok := fn().(WindowFunction); ok {
93-
call = "sqlite3_create_window_function_go"
94-
}
95102
rc := res_t(c.call(call,
96103
stk_t(c.handle), stk_t(namePtr), stk_t(nArg),
97104
stk_t(flag), stk_t(funcPtr)))
@@ -172,7 +179,13 @@ func finalCallback(ctx context.Context, mod api.Module, pCtx, pAgg, pApp ptr_t)
172179
db := ctx.Value(connKey{}).(*Conn)
173180
fn, handle := callbackAggregate(db, pAgg, pApp)
174181
fn.Value(Context{db, pCtx})
175-
if err := util.DelHandle(ctx, handle); err != nil {
182+
var err error
183+
if handle != 0 {
184+
err = util.DelHandle(ctx, handle)
185+
} else if c, ok := fn.(io.Closer); ok {
186+
err = c.Close()
187+
}
188+
if err != nil {
176189
Context{db, pCtx}.ResultError(err)
177190
return // notest
178191
}

0 commit comments

Comments
 (0)