fix: crash after closing the database

This commit is contained in:
Fedor Indutny 2025-04-28 14:06:06 -07:00 committed by GitHub
parent d2364faa35
commit b4d9e35023
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 0 deletions

View File

@ -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<Napi::Array>();
@ -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<Napi::Boolean>();

View File

@ -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();