From 867dcbfbdcd171255ab3df6b44dc292765799e0a Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Wed, 8 Apr 2026 13:42:26 +0900 Subject: [PATCH] move reset/clear into putCachedStmt and always finalize on failure This avoids an unnecessary reset when the cache is full, guarantees a statement cannot enter the cache without being reset/cleared, and fixes a leak where sqlite3_finalize was not called when reset failed. --- sqlite3.go | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/sqlite3.go b/sqlite3.go index 41dc811..db865a7 100644 --- a/sqlite3.go +++ b/sqlite3.go @@ -1942,6 +1942,10 @@ func (c *SQLiteConn) putCachedStmt(s *SQLiteStmt) bool { if c.db == nil || c.stmtCacheCount >= c.stmtCacheSize { return false } + rv := C._sqlite3_reset_clear(s.s) + if rv != C.SQLITE_ROW && rv != C.SQLITE_OK && rv != C.SQLITE_DONE { + return false + } c.stmtCache[s.cacheKey] = append(c.stmtCache[s.cacheKey], s) c.stmtCacheCount++ return true @@ -2116,16 +2120,8 @@ func (s *SQLiteStmt) Close() error { if !conn.dbConnOpen() { return errors.New("sqlite statement with already closed database connection") } - if s.cacheKey != "" { - rv := C._sqlite3_reset_clear(stmt) - if rv != C.SQLITE_ROW && rv != C.SQLITE_OK && rv != C.SQLITE_DONE { - s.s = nil - s.c = nil - return conn.lastError() - } - if conn.putCachedStmt(s) { - return nil - } + if s.cacheKey != "" && conn.putCachedStmt(s) { + return nil } s.s = nil s.c = nil