Skip to content
Merged
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: 6 additions & 0 deletions ext/sqlite3/database.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
if(!_ctxt->db) \
rb_raise(rb_path2class("SQLite3::Exception"), "cannot use a closed database");

#define REQUIRE_CLOSED_DB(_ctxt) \
if(_ctxt->db) \
rb_raise(rb_path2class("SQLite3::Exception"), "cannot open a database that is already open");

VALUE cSqlite3Database;

/* See adr/2024-09-fork-safety.md */
Expand Down Expand Up @@ -188,6 +192,7 @@ rb_sqlite3_open_v2(VALUE self, VALUE file, VALUE mode, VALUE zvfs)
int flags;

TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
REQUIRE_CLOSED_DB(ctx);

#if defined TAINTING_SUPPORT
# if defined StringValueCStr
Expand Down Expand Up @@ -988,6 +993,7 @@ rb_sqlite3_open16(VALUE self, VALUE file)
sqlite3RubyPtr ctx;

TypedData_Get_Struct(self, sqlite3Ruby, &database_type, ctx);
REQUIRE_CLOSED_DB(ctx);

#if defined TAINTING_SUPPORT
#if defined StringValueCStr
Expand Down
12 changes: 12 additions & 0 deletions test/test_database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ def test_segv
assert_raises { SQLite3::Database.new 1 } # rubocop:disable Minitest/UnspecifiedException
end

def test_open_v2_raises_when_database_is_already_open
assert_raise(SQLite3::Exception) do
db.send(:open_v2, ":memory:", Constants::Open::READWRITE | Constants::Open::CREATE, nil)
end
end

def test_open16_raises_when_database_is_already_open
assert_raise(SQLite3::Exception) do
db.send(:open16, ":memory:".encode(Encoding::UTF_16LE))
end
end

def test_db_filename
tf = nil
assert_equal "", @db.filename("main")
Expand Down
Loading