Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 36.7k
sqlite: check sqlite3_step() and sqlite3_reset() results#63319
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nodejs-github-bot
merged 4 commits into
nodejs:main
from
semimikoh:sqlite/check-step-reset-returnsAug 11, 2026
+139
−14
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6c683d0
sqlite: check sqlite3_step() and sqlite3_reset() results
semimikoh f369e87
sqlite: address review feedback on reset error handling
semimikoh 1dd0a31
sqlite: set done_ before the checked reset in Next()
semimikoh 90fe5e2
sqlite: fix build after rebase onto upstream refactor
semimikoh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -88,6 +88,17 @@ inline MaybeLocal<String> Utf8StringMaybeOneByte(Isolate* isolate, | ||
| } \ | ||
| } while (0) | ||
| #define RESET_OR_THROW(isolate, db, stmt, ret) \ | ||
| CHECK_ERROR_OR_THROW((isolate), (db), sqlite3_reset((stmt)), SQLITE_OK, (ret)) | ||
| // Surface deferred SQLite errors that sqlite3_reset() returns from the prior | ||
| // sqlite3_step(). Disables the safety-net reset guard via |needs_reset|. | ||
| #define RESET_AND_CHECK(isolate, db, stmt, needs_reset, ret) \ | ||
| do { \ | ||
| (needs_reset) = false; \ | ||
| RESET_OR_THROW((isolate), (db), (stmt), (ret)); \ | ||
| } while (0) | ||
| #define THROW_AND_RETURN_ON_BAD_STATE(env, condition, msg) \ | ||
| do { \ | ||
| if ((condition)) { \ | ||
| @@ -3005,9 +3016,20 @@ MaybeLocal<Object> StatementExecutionHelper::Run(Environment* env, | ||
| bool use_big_ints) { | ||
| Isolate* isolate = env->isolate(); | ||
| EscapableHandleScope scope(isolate); | ||
| sqlite3_step(stmt); | ||
| int r = sqlite3_reset(stmt); | ||
| CHECK_ERROR_OR_THROW(isolate, db, r, SQLITE_OK, MaybeLocal<Object>()); | ||
| bool needs_reset = true; | ||
| auto reset = OnScopeLeave([&]() { | ||
| if (needs_reset) sqlite3_reset(stmt); | ||
| }); | ||
| int step_r = sqlite3_step(stmt); | ||
| // SQLITE_ROW is accepted here (and discarded) so that run() can still be | ||
| // used on RETURNING/SELECT statements, matching prior behavior of | ||
| // ignoring the step result entirely. | ||
| if (step_r != SQLITE_DONE && step_r != SQLITE_ROW) { | ||
| THROW_ERR_SQLITE_ERROR(isolate, db); | ||
| return MaybeLocal<Object>(); | ||
| } | ||
| RESET_AND_CHECK(isolate, db, stmt, needs_reset, MaybeLocal<Object>()); | ||
| sqlite3_int64 last_insert_rowid = sqlite3_last_insert_rowid(db->Connection()); | ||
| sqlite3_int64 changes = sqlite3_changes64(db->Connection()); | ||
| @@ -3081,18 +3103,25 @@ MaybeLocal<Value> StatementExecutionHelper::Get(Environment* env, | ||
| bool use_big_ints) { | ||
| Isolate* isolate = env->isolate(); | ||
| EscapableHandleScope scope(isolate); | ||
| auto reset = OnScopeLeave([&]() { sqlite3_reset(stmt); }); | ||
| bool needs_reset = true; | ||
| auto reset = OnScopeLeave([&]() { | ||
| if (needs_reset) sqlite3_reset(stmt); | ||
| }); | ||
| int r = sqlite3_step(stmt); | ||
| if (r == SQLITE_DONE) return scope.Escape(Undefined(isolate)); | ||
| if (r == SQLITE_DONE) { | ||
| RESET_AND_CHECK(isolate, db, stmt, needs_reset, MaybeLocal<Value>()); | ||
| return scope.Escape(Undefined(isolate)); | ||
| } | ||
| if (r != SQLITE_ROW) { | ||
| THROW_ERR_SQLITE_ERROR(isolate, db); | ||
| return MaybeLocal<Value>(); | ||
| } | ||
| int num_cols = sqlite3_column_count(stmt); | ||
| if (num_cols == 0) { | ||
| return Undefined(isolate); | ||
| RESET_AND_CHECK(isolate, db, stmt, needs_reset, MaybeLocal<Value>()); | ||
| return scope.Escape(Undefined(isolate)); | ||
| } | ||
| LocalVector<Value> row_values(isolate); | ||
| @@ -3101,9 +3130,9 @@ MaybeLocal<Value> StatementExecutionHelper::Get(Environment* env, | ||
| return MaybeLocal<Value>(); | ||
| } | ||
| Local<Value> result; | ||
| if (return_arrays) { | ||
| return scope.Escape( | ||
| Array::New(isolate, row_values.data(), row_values.size())); | ||
| result = Array::New(isolate, row_values.data(), row_values.size()); | ||
| } else { | ||
| LocalVector<Name> keys(isolate); | ||
| keys.reserve(num_cols); | ||
| @@ -3116,9 +3145,12 @@ MaybeLocal<Value> StatementExecutionHelper::Get(Environment* env, | ||
| } | ||
| DCHECK_EQ(keys.size(), row_values.size()); | ||
| return scope.Escape(Object::New( | ||
| isolate, Null(isolate), keys.data(), row_values.data(), num_cols)); | ||
| result = Object::New( | ||
| isolate, Null(isolate), keys.data(), row_values.data(), num_cols); | ||
| } | ||
| RESET_AND_CHECK(isolate, db, stmt, needs_reset, MaybeLocal<Value>()); | ||
trivikr marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| return scope.Escape(result); | ||
| } | ||
| void StatementSync::All(const FunctionCallbackInfo<Value>& args) { | ||
| @@ -3135,15 +3167,19 @@ void StatementSync::All(const FunctionCallbackInfo<Value>& args) { | ||
| return; | ||
| } | ||
| auto reset = OnScopeLeave([&]() { sqlite3_reset(stmt->statement_); }); | ||
| bool needs_reset = true; | ||
| auto reset = OnScopeLeave([&]() { | ||
| if (needs_reset) sqlite3_reset(stmt->statement_); | ||
| }); | ||
| Local<Value> result; | ||
| if (StatementExecutionHelper::All(env, | ||
| stmt->db_.get(), | ||
| stmt->statement_, | ||
| stmt->return_arrays_, | ||
| stmt->use_big_ints_) | ||
| .ToLocal(&result)) { | ||
| RESET_AND_CHECK( | ||
| isolate, stmt->db_.get(), stmt->statement_, needs_reset, void()); | ||
| args.GetReturnValue().Set(result); | ||
| } | ||
| } | ||
| @@ -3577,14 +3613,20 @@ void SQLTagStore::All(const FunctionCallbackInfo<Value>& args) { | ||
| return; | ||
| } | ||
| auto reset = OnScopeLeave([&]() { sqlite3_reset(stmt->statement_); }); | ||
| Isolate* isolate = env->isolate(); | ||
| bool needs_reset = true; | ||
| auto reset = OnScopeLeave([&]() { | ||
| if (needs_reset) sqlite3_reset(stmt->statement_); | ||
| }); | ||
| Local<Value> result; | ||
| if (StatementExecutionHelper::All(env, | ||
| stmt->db_.get(), | ||
| stmt->statement_, | ||
| stmt->return_arrays_, | ||
| stmt->use_big_ints_) | ||
| .ToLocal(&result)) { | ||
| RESET_AND_CHECK( | ||
| isolate, stmt->db_.get(), stmt->statement_, needs_reset, void()); | ||
| args.GetReturnValue().Set(result); | ||
| } | ||
| } | ||
| @@ -3811,8 +3853,11 @@ void StatementSyncIterator::Next(const FunctionCallbackInfo<Value>& args) { | ||
| if (r != SQLITE_ROW) { | ||
| CHECK_ERROR_OR_THROW( | ||
| env->isolate(), iter->stmt_->db_.get(), r, SQLITE_DONE, void()); | ||
| sqlite3_reset(iter->stmt_->statement_); | ||
| iter->done_ = true; | ||
| RESET_OR_THROW(env->isolate(), | ||
trivikr marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| iter->stmt_->db_.get(), | ||
| iter->stmt_->statement_, | ||
| void()); | ||
| MaybeLocal<Value> values[] = {Boolean::New(isolate, true), Null(isolate)}; | ||
| Local<Object> result; | ||
| if (NewDictionaryInstanceNullProto(env->context(), iter_template, values) | ||
| @@ -3864,6 +3909,10 @@ void StatementSyncIterator::Return(const FunctionCallbackInfo<Value>& args) { | ||
| env, iter->stmt_->IsFinalized(), "statement has been finalized"); | ||
| Isolate* isolate = env->isolate(); | ||
| // Unlike Next(), the reset result is intentionally ignored here: Return() | ||
| // is invoked by the language during abrupt completion (e.g. a `throw` | ||
| // inside a `for...of` body), and throwing on a deferred SQLite error | ||
| // would discard the caller's already-pending exception. | ||
| sqlite3_reset(iter->stmt_->statement_); | ||
| iter->done_ = true; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.