summaryrefslogtreecommitdiff
path: root/Kernel/Bus
diff options
context:
space:
mode:
authorJesse Buhagiar <jooster669@gmail.com>2022-01-03 00:48:46 +1100
committerIdan Horowitz <idan.horowitz@gmail.com>2022-01-03 10:39:56 +0200
commitaf31253a160bfdde58587ee92afe1476ecc27e16 (patch)
tree0b85dddc4a793413d60748aa37a169d20b2b3f48 /Kernel/Bus
parent1d7d7d39b75785a6cd1490d2389f0c74cc0c049d (diff)
downloadserenity-af31253a160bfdde58587ee92afe1476ecc27e16.zip
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.
Diffstat (limited to 'Kernel/Bus')
-rw-r--r--Kernel/Bus/USB/SysFSUSB.cpp14
-rw-r--r--Kernel/Bus/USB/SysFSUSB.h2
2 files changed, 11 insertions, 5 deletions
diff --git a/Kernel/Bus/USB/SysFSUSB.cpp b/Kernel/Bus/USB/SysFSUSB.cpp
index e4fc0a67ef..f6faabed7c 100644
--- a/Kernel/Bus/USB/SysFSUSB.cpp
+++ b/Kernel/Bus/USB/SysFSUSB.cpp
@@ -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));
}
}
diff --git a/Kernel/Bus/USB/SysFSUSB.h b/Kernel/Bus/USB/SysFSUSB.h
index 87a5b8857e..bf7163dd76 100644
--- a/Kernel/Bus/USB/SysFSUSB.h
+++ b/Kernel/Bus/USB/SysFSUSB.h
@@ -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; }