1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 04:27:44 +00:00

LibSQL+SQLServer: Do not re-open databases

Both `Database` and `Heap` were allowed to be opened twice. Prevent
this, and change SQLServer to only open databases that are not already
opened.

This fixes a Ladybird crash where opening the application twice would
erroneously duplicate free heap block indices.
This commit is contained in:
Jelle Raaijmakers 2023-06-13 21:52:58 +02:00 committed by Tim Flynn
parent c05e08decb
commit 70ca295404
3 changed files with 8 additions and 4 deletions

View file

@ -23,6 +23,7 @@ Database::Database(DeprecatedString name)
ResultOr<void> Database::open() ResultOr<void> Database::open()
{ {
VERIFY(!m_open);
TRY(m_heap->open()); TRY(m_heap->open());
m_schemas = BTree::construct(m_serializer, SchemaDef::index_def()->to_tuple_descriptor(), m_heap->schemas_root()); m_schemas = BTree::construct(m_serializer, SchemaDef::index_def()->to_tuple_descriptor(), m_heap->schemas_root());

View file

@ -29,6 +29,8 @@ Heap::~Heap()
ErrorOr<void> Heap::open() ErrorOr<void> Heap::open()
{ {
VERIFY(!m_file);
size_t file_size = 0; size_t file_size = 0;
struct stat stat_buffer; struct stat stat_buffer;
if (stat(name().characters(), &stat_buffer) != 0) { if (stat(name().characters(), &stat_buffer) != 0) {

View file

@ -38,10 +38,11 @@ ErrorOr<NonnullRefPtr<DatabaseConnection>> DatabaseConnection::create(StringView
return Error::from_string_view("Invalid database name"sv); return Error::from_string_view("Invalid database name"sv);
auto database = TRY(find_or_create_database(database_path, database_name)); auto database = TRY(find_or_create_database(database_path, database_name));
if (!database->is_open()) {
if (auto result = database->open(); result.is_error()) { if (auto result = database->open(); result.is_error()) {
warnln("Could not open database: {}", result.error().error_string()); warnln("Could not open database: {}", result.error().error_string());
return Error::from_string_view("Could not open database"sv); return Error::from_string_view("Could not open database"sv);
}
} }
return adopt_nonnull_ref_or_enomem(new (nothrow) DatabaseConnection(move(database), move(database_name), client_id)); return adopt_nonnull_ref_or_enomem(new (nothrow) DatabaseConnection(move(database), move(database_name), client_id));