sqlite: avoid resetting a finalized statement

run() and get() reset a cached statement pointer on exit. Throwing an
error can run an Error.prototype setter that closes the database and
frees it. Reset through the Statement, which Finalize() clears.

Assisted-by: Claude Code
Signed-off-by: Guilherme Araújo <arauujogui@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/67557
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Christian Aurich Zanettini Martins <christian.aurichzm@gmail.com>
This commit is contained in:
Guilherme Araújo authored and GitHub committed 2026-11-19 19:24:27 +00:00
1 parent 7bfa50aa94
commit f0eb690cf1
2 files changed
+65 -2

No files matched your search

+4 -2
View File
@@ -3835,9 +3835,10 @@ MaybeLocal<Object> StatementExecutionHelper::Run(Environment* env,
// Declared before the reset below so that it outlives it: sqlite3_reset()
// can run JavaScript through an aggregate's xFinal.
SteppingStatementGuard stepping(db, stmt);
// Throwing can run JS that closes the database and frees `stmt`.
bool needs_reset = true;
auto reset = OnScopeLeave([&]() {
if (needs_reset) sqlite3_reset(stmt);
if (needs_reset) sqlite3_reset(statement->statement_.get());
});
int step_r = sqlite3_step(stmt);
@@ -3923,9 +3924,10 @@ MaybeLocal<Value> StatementExecutionHelper::Get(Environment* env,
// Declared before the reset below so that it outlives it: sqlite3_reset()
// can run JavaScript through an aggregate's xFinal.
SteppingStatementGuard stepping(db, stmt);
// Throwing can run JS that closes the database and frees `stmt`.
bool needs_reset = true;
auto reset = OnScopeLeave([&]() {
if (needs_reset) sqlite3_reset(stmt);
if (needs_reset) sqlite3_reset(statement->statement_.get());
});
int r = sqlite3_step(stmt);