1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 09:47:35 +00:00

Kernel: Get RefPtr<Device> from the DeviceManagement::get_device method

Instead of returning a raw pointer, which could be technically invalid
when using it in the caller function, we return a valid RefPtr of such
device.

This ensures that the code in DevPtsFS is now safe from a rare race
condition in which the SlavePTY device is gone but we still have a
pointer to it.
This commit is contained in:
Liav A 2024-03-04 21:19:31 +02:00 committed by Andrew Kaster
parent 5dcf03ad9a
commit 11ead5c84f
6 changed files with 24 additions and 16 deletions

View file

@ -42,13 +42,13 @@ DeviceManagement& DeviceManagement::the()
return *s_the;
}
Device* DeviceManagement::get_device(MajorNumber major, MinorNumber minor)
RefPtr<Device> DeviceManagement::get_device(MajorNumber major, MinorNumber minor)
{
return m_devices.with([&](auto& map) -> Device* {
return m_devices.with([&](auto& map) -> RefPtr<Device> {
auto it = map.find(encoded_device(major.value(), minor.value()));
if (it == map.end())
return nullptr;
return it->value;
return *it->value;
});
}

View file

@ -41,7 +41,7 @@ public:
void for_each(Function<void(Device&)>);
ErrorOr<void> try_for_each(Function<ErrorOr<void>(Device&)>);
Device* get_device(MajorNumber major, MinorNumber minor);
RefPtr<Device> get_device(MajorNumber major, MinorNumber minor);
NullDevice const& null_device() const;
NullDevice& null_device();

View file

@ -353,9 +353,9 @@ UNMAP_AFTER_INIT void StorageManagement::determine_block_boot_device()
// Note: We simply fetch the corresponding BlockDevice with the major and minor parameters.
// We don't try to accept and resolve a partition number as it will make this code much more
// complicated. This rule is also explained in the boot_device_addressing(7) manual page.
LockRefPtr<Device> device = DeviceManagement::the().get_device(parameters_view[0], parameters_view[1]);
auto device = DeviceManagement::the().get_device(parameters_view[0], parameters_view[1]);
if (device && device->is_block_device())
m_boot_block_device = static_ptr_cast<BlockDevice>(device);
m_boot_block_device = *static_ptr_cast<BlockDevice>(device);
}
UNMAP_AFTER_INIT void StorageManagement::determine_boot_device_with_logical_unit_number()