Skip to content
Open
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
14 changes: 14 additions & 0 deletions src/node_sqlite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1584,6 +1584,13 @@ void DatabaseSync::Exec(const FunctionCallbackInfo<Value>& args) {
return;
}

// Keep the database alive during sqlite3_exec(), which may call
// user-defined SQLite functions that trigger JavaScript callbacks.
// If the JavaScript callback drops all references to the database,
// the DatabaseSync could otherwise be garbage-collected while the
// SQLite callback is still executing, causing a use-after-free.
BaseObjectPtr<DatabaseSync> guard(db);

Utf8Value sql(env->isolate(), args[0].As<String>());
int r = sqlite3_exec(db->connection_, *sql, nullptr, nullptr, nullptr);
CHECK_ERROR_OR_THROW(env->isolate(), db, r, SQLITE_OK, void());
Expand Down Expand Up @@ -2358,6 +2365,13 @@ void DatabaseSync::ApplyChangeset(const FunctionCallbackInfo<Value>& args) {
}
}

// Keep the database alive during sqlite3changeset_apply(), which may
// call conflict or filter callbacks that trigger JavaScript execution.
// If the JavaScript callback drops all references to the database,
// the DatabaseSync could otherwise be garbage-collected while the
// callback is still executing, causing a use-after-free.
BaseObjectPtr<DatabaseSync> guard(db);

ArrayBufferViewContents<uint8_t> buf(args[0]);
int r = sqlite3changeset_apply(
db->connection_,
Expand Down
Loading