Skip to content
Merged
Prev Previous commit
Next Next commit
sqlite: avoid coercion when returning invalid value from conflict han…
…dler
  • Loading branch information
louwers committed Dec 28, 2024
commit 5d2fc4e38fe37732c535e687cc4534aa88a83ad9
2 changes: 2 additions & 0 deletions src/node_sqlite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,8 @@ void DatabaseSync::ApplyChangeset(const FunctionCallbackInfo<Value>& args) {
try_catch.ReThrow();
return SQLITE_CHANGESET_ABORT;
}
constexpr auto invalid_value = -1;
if (!result->IsInt32()) return invalid_value;
return result->Int32Value(env->context()).FromJust();
Comment thread
louwers marked this conversation as resolved.
};
Comment thread
louwers marked this conversation as resolved.
}
Expand Down
27 changes: 16 additions & 11 deletions test/parallel/test-sqlite-session.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,17 +324,22 @@ suite('conflict resolution', () => {
});

test('conflict resolution handler returns invalid value', (t) => {
const { database2, changeset } = prepareConflict();
t.assert.throws(() => {
database2.applyChangeset(changeset, {
onConflict: () => {
return -1;
}
});
}, {
name: 'Error',
message: 'bad parameter or other API misuse'
});
const invalidValues = [-1, {}, null];

for (const invalidValue of invalidValues) {
const { database2, changeset } = prepareConflict();
t.assert.throws(() => {
database2.applyChangeset(changeset, {
onConflict: () => {
return invalidValue;
}
});
}, {
name: 'Error',
message: 'bad parameter or other API misuse',
errcode: 21
}, `Did not throw expected exception when returning '${invalidValue}' from conflict handler`);
}
});

test('conflict resolution handler throws', (t) => {
Expand Down