Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,11 @@ type SQLiteConn struct {
txlock string
funcs []*functionInfo
aggregators []*aggInfo
// authorizerMu guards authorizerHandle. It is deliberately not c.mu:
// sqlite3_set_authorizer takes db->mutex, while the cancellation path
// takes c.mu, so holding c.mu here would invert the lock order.
authorizerMu sync.Mutex
authorizerHandle unsafe.Pointer
// Prepared-statement cache. The slice is allocated at Open with a
// fixed capacity equal to the configured cache size; cap bounds the
// cache, len is the live count, and entries are ordered LRU-first
Expand Down Expand Up @@ -751,11 +756,22 @@ func (c *SQLiteConn) RegisterUpdateHook(callback func(int, string, string, int64
// depending on operation. More details see:
// https://www.sqlite.org/c3ref/c_alter_table.html
func (c *SQLiteConn) RegisterAuthorizer(callback func(int, string, string, string) int) {
if callback == nil {
C.sqlite3_set_authorizer(c.db, nil, nil)
} else {
C.sqlite3_set_authorizer(c.db, (*[0]byte)(C.authorizerTrampoline), newHandle(c, callback))
c.authorizerMu.Lock()
defer c.authorizerMu.Unlock()

var handle unsafe.Pointer
var trampoline *[0]byte
if callback != nil {
handle = newHandle(c, callback)
trampoline = (*[0]byte)(C.authorizerTrampoline)
}
if C.sqlite3_set_authorizer(c.db, trampoline, handle) != C.SQLITE_OK {
deleteHandle(handle)
return
}
// SQLite no longer uses the previous callback after replacing it.
deleteHandle(c.authorizerHandle)
c.authorizerHandle = handle
}

// RegisterFunc makes a Go function available as a SQLite function.
Expand Down Expand Up @@ -2024,6 +2040,10 @@ func (c *SQLiteConn) Close() error {
return lastError(c.db)
}
deleteHandles(c)
// Lock order is c.mu -> authorizerMu; RegisterAuthorizer never takes c.mu.
c.authorizerMu.Lock()
c.authorizerHandle = nil
c.authorizerMu.Unlock()
c.db = nil
return nil
}
Expand Down
118 changes: 118 additions & 0 deletions sqlite3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1971,6 +1971,124 @@ func TestAuthorizer(t *testing.T) {
}
}

func TestAuthorizerHandleRelease(t *testing.T) {
openConn := func() *SQLiteConn {
t.Helper()
conn, err := (&SQLiteDriver{}).Open(":memory:")
if err != nil {
t.Fatal(err)
}
c := conn.(*SQLiteConn)
t.Cleanup(func() {
if err := c.Close(); err != nil {
t.Error(err)
}
})
return c
}
countHandles := func(c *SQLiteConn) int {
got := 0
handleVals.Range(func(_, value any) bool {
if value.(handleVal).db == c {
got++
}
return true
})
return got
}
checkHandles := func(c *SQLiteConn, want int) {
t.Helper()
got := countHandles(c)
if got != want {
t.Fatalf("connection has %d callback handles, want %d", got, want)
}
}

c, other := openConn(), openConn()
updates := 0
c.RegisterUpdateHook(func(int, string, string, int64) { updates++ })
if err := c.RegisterFunc("retained", func() int { return 42 }, true); err != nil {
t.Fatal(err)
}
if _, err := c.Exec("CREATE TABLE foo (value INTEGER)", nil); err != nil {
t.Fatal(err)
}
baseline := countHandles(c)
otherBaseline := countHandles(other)
otherCalls := 0
other.RegisterAuthorizer(func(int, string, string, string) int {
otherCalls++
return SQLITE_OK
})
checkHandles(other, otherBaseline+1)
c.RegisterAuthorizer(nil)
checkHandles(c, baseline)

calls := make([]int, 3)
for i := range calls {
i := i
previous := append([]int(nil), calls...)
c.RegisterAuthorizer(func(int, string, string, string) int {
calls[i]++
return SQLITE_OK
})
checkHandles(c, baseline+1)
if _, err := c.Exec("INSERT INTO foo VALUES (retained())", nil); err != nil {
t.Fatal(err)
}
if calls[i] == 0 {
t.Fatalf("authorizer %d was not called", i)
}
if !reflect.DeepEqual(calls[:i], previous[:i]) {
t.Fatal("a replaced authorizer was called")
}
}

previous := append([]int(nil), calls...)
c.RegisterAuthorizer(nil)
c.RegisterAuthorizer(nil)
checkHandles(c, baseline)
if _, err := c.Exec("INSERT INTO foo VALUES (retained())", nil); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(calls, previous) {
t.Fatal("a removed authorizer was called")
}
if updates != len(calls)+1 {
t.Fatalf("update hook called %d times, want %d", updates, len(calls)+1)
}
rows, err := c.Query("SELECT sum(value) FROM foo", nil)
if err != nil {
t.Fatal(err)
}
values := make([]driver.Value, 1)
if err := rows.Next(values); err != nil {
rows.Close()
t.Fatal(err)
}
if err := rows.Close(); err != nil {
t.Fatal(err)
}
if values[0] != int64(42*(len(calls)+1)) {
t.Fatalf("sum(value) = %v, want %d", values[0], 42*(len(calls)+1))
}
if err := c.Close(); err != nil {
t.Fatal(err)
}
checkHandles(c, 0)
checkHandles(other, otherBaseline+1)
if _, err := other.Exec("SELECT 1", nil); err != nil {
t.Fatal(err)
}
if otherCalls == 0 {
t.Fatal("the other connection's authorizer was not called")
}
if err := other.Close(); err != nil {
t.Fatal(err)
}
checkHandles(other, 0)
}

func TestSetFileControlInt(t *testing.T) {
t.Run("PERSIST_WAL", func(t *testing.T) {
tempFilename := TempFilename(t)
Expand Down
Loading