LibWeb: Check weak pointer liveness in Database::for_key_and_name()

The database map stores GC::Weak<Database> entries. When the GC
collects a Database, the weak pointer goes null but the map entry
remains. The old code dereferenced the weak pointer without checking
liveness, causing a null reference binding (UBSan).

Fix this by checking ptr() before dereferencing, and cleaning up the
stale map entry if the database was collected.
This commit is contained in:
Andreas Kling
2026-03-08 10:26:15 +01:00
committed by Shannon Booth
parent 0c0250451a
commit f6e755506d
Notes: github-actions[bot] 2026-03-08 10:41:52 +00:00

View File

@@ -86,8 +86,12 @@ RequestList& ConnectionQueueHandler::for_key_and_name(StorageAPI::StorageKey con
Optional<Database&> Database::for_key_and_name(StorageAPI::StorageKey const& key, String const& name)
{
auto database_mapping = m_databases.ensure(key, [] { return HashMap<String, GC::Weak<Database>>(); });
if (auto maybe_database = database_mapping.get(name); maybe_database.has_value())
return *maybe_database.value();
if (auto maybe_database = database_mapping.get(name); maybe_database.has_value()) {
if (auto database = maybe_database.value().ptr())
return *database;
database_mapping.remove(name);
m_databases.set(key, database_mapping);
}
return {};
}