-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
sqlite: handle exceptions in filter callback database.applyChangeset() #56903
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -743,26 +743,28 @@ void DatabaseSync::CreateSession(const FunctionCallbackInfo<Value>& args) { | |||||||
args.GetReturnValue().Set(session->object()); | ||||||||
} | ||||||||
|
||||||||
struct ConflictCallbackContext { | ||||||||
std::function<bool(std::string)> filterCallback; | ||||||||
std::function<int(int)> conflictCallback; | ||||||||
}; | ||||||||
|
||||||||
// the reason for using static functions here is that SQLite needs a | ||||||||
// function pointer | ||||||||
static std::function<int(int)> conflictCallback; | ||||||||
|
||||||||
static int xConflict(void* pCtx, int eConflict, sqlite3_changeset_iter* pIter) { | ||||||||
if (!conflictCallback) return SQLITE_CHANGESET_ABORT; | ||||||||
return conflictCallback(eConflict); | ||||||||
auto ctx = static_cast<ConflictCallbackContext*>(pCtx); | ||||||||
if (!ctx->conflictCallback) return SQLITE_CHANGESET_ABORT; | ||||||||
return ctx->conflictCallback(eConflict); | ||||||||
} | ||||||||
|
||||||||
static std::function<bool(std::string)> filterCallback; | ||||||||
|
||||||||
static int xFilter(void* pCtx, const char* zTab) { | ||||||||
if (!filterCallback) return 1; | ||||||||
|
||||||||
return filterCallback(zTab) ? 1 : 0; | ||||||||
auto ctx = static_cast<ConflictCallbackContext*>(pCtx); | ||||||||
if (!ctx->filterCallback) return 1; | ||||||||
return ctx->filterCallback(zTab) ? 1 : 0; | ||||||||
} | ||||||||
|
||||||||
void DatabaseSync::ApplyChangeset(const FunctionCallbackInfo<Value>& args) { | ||||||||
conflictCallback = nullptr; | ||||||||
filterCallback = nullptr; | ||||||||
ConflictCallbackContext context; | ||||||||
|
||||||||
DatabaseSync* db; | ||||||||
ASSIGN_OR_RETURN_UNWRAP(&db, args.This()); | ||||||||
|
@@ -794,7 +796,7 @@ void DatabaseSync::ApplyChangeset(const FunctionCallbackInfo<Value>& args) { | |||||||
return; | ||||||||
} | ||||||||
Local<Function> conflictFunc = conflictValue.As<Function>(); | ||||||||
conflictCallback = [env, conflictFunc](int conflictType) -> int { | ||||||||
context.conflictCallback = [env, conflictFunc](int conflictType) -> int { | ||||||||
Local<Value> argv[] = {Integer::New(env->isolate(), conflictType)}; | ||||||||
TryCatch try_catch(env->isolate()); | ||||||||
Local<Value> result = | ||||||||
|
@@ -824,14 +826,21 @@ void DatabaseSync::ApplyChangeset(const FunctionCallbackInfo<Value>& args) { | |||||||
|
||||||||
Local<Function> filterFunc = filterValue.As<Function>(); | ||||||||
|
||||||||
filterCallback = [env, filterFunc](std::string item) -> bool { | ||||||||
context.filterCallback = [env, filterFunc](std::string item) -> bool { | ||||||||
Local<Value> argv[] = {String::NewFromUtf8(env->isolate(), | ||||||||
item.c_str(), | ||||||||
NewStringType::kNormal) | ||||||||
.ToLocalChecked()}; | ||||||||
Local<Value> result = | ||||||||
filterFunc->Call(env->context(), Null(env->isolate()), 1, argv) | ||||||||
.ToLocalChecked(); | ||||||||
item.c_str(), | ||||||||
NewStringType::kNormal) | ||||||||
.ToLocalChecked()}; | ||||||||
MaybeLocal<Value> maybe_result = | ||||||||
filterFunc->Call(env->context(), Null(env->isolate()), 1, argv); | ||||||||
|
||||||||
if (maybe_result.IsEmpty()) { | ||||||||
return false; | ||||||||
} | ||||||||
Comment on lines
+837
to
+839
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not technically wrong, but you don't need this because you already handle the
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will still add some logic here, I think this branch is taken when an exception is thrown, correct? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it should be taken when |
||||||||
Local<Value> result; | ||||||||
if (!maybe_result.ToLocal(&result)) { | ||||||||
return false; | ||||||||
} | ||||||||
return result->BooleanValue(env->isolate()); | ||||||||
}; | ||||||||
} | ||||||||
|
@@ -844,7 +853,7 @@ void DatabaseSync::ApplyChangeset(const FunctionCallbackInfo<Value>& args) { | |||||||
const_cast<void*>(static_cast<const void*>(buf.data())), | ||||||||
xFilter, | ||||||||
xConflict, | ||||||||
nullptr); | ||||||||
static_cast<void*>(&context)); | ||||||||
if (r == SQLITE_OK) { | ||||||||
args.GetReturnValue().Set(true); | ||||||||
return; | ||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const tmpdir = require('../common/tmpdir'); | ||
const { join } = require('node:path'); | ||
|
||
let cnt = 0; | ||
|
||
tmpdir.refresh(); | ||
|
||
function nextDb() { | ||
return join(tmpdir.path, `database-${cnt++}.db`); | ||
} | ||
|
||
module.exports = { nextDb }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// this worker is used for one of the tests in test-sqlite-session.js | ||
|
||
const { parentPort, workerData } = require('worker_threads'); | ||
const { DatabaseSync, constants } = require('node:sqlite'); | ||
const { changeset, mode, dbPath } = workerData; | ||
|
||
const db = new DatabaseSync(dbPath); | ||
|
||
const options = {} | ||
if (mode !== constants.SQLITE_CHANGESET_ABORT && mode !== constants.SQLITE_CHANGESET_OMIT) { | ||
throw new Error("Unexpected value for mode"); | ||
} | ||
options.onConflict = () => mode; | ||
|
||
try { | ||
const result = db.applyChangeset(changeset, options); | ||
parentPort.postMessage({ mode, result, error: null }); | ||
} catch (error) { | ||
parentPort.postMessage({ mode, result: null, errorMessage: error.message, errcode: error.errcode }); | ||
} finally { | ||
db.close(); // just to make sure it is closed ASAP | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you need the
TryCatch
here. The problem is the use ofToLocalChecked()
. Take a look at this code. You can tell if V8 has an exception pending if theToLocal()
call does not succeed.