diff --git a/src/addon.cc b/src/addon.cc index 912af62..f8774a2 100644 --- a/src/addon.cc +++ b/src/addon.cc @@ -398,6 +398,9 @@ Napi::Value Statement::Close(const Napi::CallbackInfo& info) { auto env = info.Env(); auto stmt = FromExternal(info[0]); + if (stmt == nullptr) { + return Napi::Value(); + } int r = sqlite3_finalize(stmt->handle_); if (r != SQLITE_OK) { @@ -413,6 +416,10 @@ Napi::Value Statement::Run(const Napi::CallbackInfo& info) { auto env = info.Env(); auto stmt = FromExternal(info[0]); + if (stmt == nullptr) { + return Napi::Value(); + } + auto params = info[1]; auto result = info[2].As(); @@ -451,6 +458,10 @@ Napi::Value Statement::Step(const Napi::CallbackInfo& info) { auto env = info.Env(); auto stmt = FromExternal(info[0]); + if (stmt == nullptr) { + return Napi::Value(); + } + auto params = info[1]; auto cache = info[2]; auto is_get = info[3].As(); diff --git a/test/memory.test.ts b/test/memory.test.ts index 3bb5a02..a70424d 100644 --- a/test/memory.test.ts +++ b/test/memory.test.ts @@ -49,6 +49,16 @@ test('db.close', () => { db = new Database(); }); +test('db.close with existing statement', () => { + const stmt = db.prepare('SELECT 1'); + db.close(); + + expect(() => stmt.run()).toThrowError('Statement closed'); + + // Just to fix afterEach + db = new Database(); +}); + test('statement.close', () => { const stmt = db.prepare('SELECT 1'); stmt.close();