Skip to content
Closed
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions cpp/DBHostObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,12 @@ void DBHostObject::create_jsi_functions(jsi::Runtime &rt) {
std::string path = std::string(base_path);

if (count == 1) {
if (!args[1].isString()) {
if (!args[0].isString()) {
throw std::runtime_error(
"[op-sqlite][open] database location must be a string");
"[op-sqlite][delete] database location must be a string");
}

std::string location = args[1].asString(rt).utf8(rt);
std::string location = args[0].asString(rt).utf8(rt);

if (!location.empty()) {
if (location == ":memory:") {
Expand Down
32 changes: 32 additions & 0 deletions example/src/tests/dbsetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,38 @@ describe("DB setup tests", () => {
db.delete();
});

it("Should delete db when a location argument is passed to delete()", async () => {
const db = open({
name: "deleteWithLocationArg.sqlite",
encryptionKey: "test",
});

// Regression: delete() previously read args[1] when count == 1, which
// read past the JSI argument list and could crash native code. An
// empty-string location exercises the count == 1 / args[0] path; the
// empty-guard in the host function preserves the base path so the file
// is still found and removed.
db.delete("");
});

it("Should reject a non-string location argument to delete()", async () => {
const db = open({
name: "deleteBadArg.sqlite",
encryptionKey: "test",
});

let threw = false;
try {
// @ts-expect-error — intentionally passing a non-string to exercise the guard
db.delete(123);
} catch (e) {
threw = true;
}
expect(threw).toEqual(true);

db.delete();
});

it("Should create db in custom folder", async () => {
const db = open({
name: "customFolderTest.sqlite",
Expand Down
Loading