Kernel: Use try_create not must_create in SysFSUSB::create

The function `KString::must_create()` can only be enforced
during early boot (that is, when `g_in_early_boot` is true), hence
the use of this function during runtime causes a `VERIFY` to assert,
leading to a Kernel Panic.
We should instead use `TRY()` along with `try_create()` to prevent
this from crashing whenever a USB device is inserted into the system,
and we don't have enough memory to allocate the device's KString.
This commit is contained in:
Jesse Buhagiar 2022-01-03 00:48:46 +11:00 committed by Idan Horowitz
parent 1d7d7d39b7
commit af31253a16
2 changed files with 11 additions and 5 deletions

View file

@ -133,7 +133,13 @@ void SysFSUSBBusDirectory::plug(USB::Device& new_device)
SpinlockLocker lock(m_lock);
auto device_node = device_node_for(new_device);
VERIFY(!device_node);
m_device_nodes.append(SysFSUSBDeviceInformation::create(new_device));
auto sysfs_usb_device_or_error = SysFSUSBDeviceInformation::create(new_device);
if (sysfs_usb_device_or_error.is_error()) {
dbgln("Failed to create SysFSUSBDevice for device id {}", new_device.address());
return;
}
m_device_nodes.append(sysfs_usb_device_or_error.release_value());
}
void SysFSUSBBusDirectory::unplug(USB::Device& deleted_device)
@ -162,10 +168,10 @@ UNMAP_AFTER_INIT void SysFSUSBBusDirectory::initialize()
s_procfs_usb_bus_directory = directory;
}
NonnullRefPtr<SysFSUSBDeviceInformation> SysFSUSBDeviceInformation::create(USB::Device& device)
ErrorOr<NonnullRefPtr<SysFSUSBDeviceInformation>> SysFSUSBDeviceInformation::create(USB::Device& device)
{
auto device_name = KString::must_create(String::number(device.address()));
return adopt_ref(*new SysFSUSBDeviceInformation(move(device_name), device));
auto device_name = TRY(KString::try_create(String::number(device.address())));
return adopt_nonnull_ref_or_enomem(new (nothrow) SysFSUSBDeviceInformation(move(device_name), device));
}
}

View file

@ -19,7 +19,7 @@ class SysFSUSBDeviceInformation : public SysFSComponent {
public:
virtual ~SysFSUSBDeviceInformation() override;
static NonnullRefPtr<SysFSUSBDeviceInformation> create(USB::Device&);
static ErrorOr<NonnullRefPtr<SysFSUSBDeviceInformation>> create(USB::Device&);
virtual StringView name() const override { return m_device_name->view(); }
RefPtr<USB::Device> device() const { return m_device; }