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

Kernel: Make KString factories return KResultOr + use TRY() everywhere

There are a number of places that don't have an error propagation path
right now, so I've added FIXME's about that.
This commit is contained in:
Andreas Kling 2021-09-06 19:24:54 +02:00
parent 69b9b2888c
commit 56a2594de7
21 changed files with 100 additions and 122 deletions

View file

@ -33,15 +33,10 @@ KResultOr<NonnullRefPtr<Custody>> Custody::try_create(Custody* parent, StringVie
}
}
auto name_kstring = KString::try_create(name);
if (!name_kstring)
return ENOMEM;
auto custody = adopt_ref_if_nonnull(new (nothrow) Custody(parent, name_kstring.release_nonnull(), inode, mount_flags));
if (!custody)
return ENOMEM;
auto name_kstring = TRY(KString::try_create(name));
auto custody = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) Custody(parent, move(name_kstring), inode, mount_flags)));
all_custodies.prepend(*custody);
return custody.release_nonnull();
return custody;
});
}
@ -73,12 +68,8 @@ Custody::~Custody()
KResultOr<NonnullOwnPtr<KString>> Custody::try_serialize_absolute_path() const
{
if (!parent()) {
auto string = KString::try_create("/"sv);
if (!string)
return ENOMEM;
return string.release_nonnull();
}
if (!parent())
return KString::try_create("/"sv);
Vector<Custody const*, 32> custody_chain;
size_t path_length = 0;
@ -89,9 +80,7 @@ KResultOr<NonnullOwnPtr<KString>> Custody::try_serialize_absolute_path() const
VERIFY(path_length > 0);
char* buffer;
auto string = KString::try_create_uninitialized(path_length - 1, buffer);
if (!string)
return ENOMEM;
auto string = TRY(KString::try_create_uninitialized(path_length - 1, buffer));
size_t string_index = 0;
for (size_t custody_index = custody_chain.size() - 1; custody_index > 0; --custody_index) {
buffer[string_index] = '/';
@ -102,7 +91,7 @@ KResultOr<NonnullOwnPtr<KString>> Custody::try_serialize_absolute_path() const
}
VERIFY(string->length() == string_index);
buffer[string_index] = 0;
return string.release_nonnull();
return string;
}
String Custody::absolute_path() const

View file

@ -21,11 +21,10 @@ DevFS::DevFS()
void DevFS::notify_new_device(Device& device)
{
auto name = KString::try_create(device.device_name());
VERIFY(name);
// FIXME: Handle KString allocation failure.
auto name = KString::try_create(device.device_name()).release_value();
MutexLocker locker(m_lock);
auto new_device_inode = adopt_ref(*new DevFSDeviceInode(*this, device, name.release_nonnull()));
auto new_device_inode = adopt_ref(*new DevFSDeviceInode(*this, device, move(name)));
m_nodes.append(new_device_inode);
m_root_inode->m_devices.append(new_device_inode);
}
@ -276,10 +275,8 @@ KResultOr<NonnullRefPtr<Inode>> DevFSRootDirectoryInode::create_child(StringView
if (link.name() == name)
return EEXIST;
}
auto name_kstring = KString::try_create(name);
if (!name_kstring)
return ENOMEM;
auto new_link_inode = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) DevFSLinkInode(fs(), name_kstring.release_nonnull())));
auto name_kstring = TRY(KString::try_create(name));
auto new_link_inode = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) DevFSLinkInode(fs(), move(name_kstring))));
if (!m_links.try_ensure_capacity(m_links.size() + 1))
return ENOMEM;
if (!fs().m_nodes.try_ensure_capacity(fs().m_nodes.size() + 1))

View file

@ -21,7 +21,7 @@ static size_t allocate_inode_index()
}
SysFSComponent::SysFSComponent(StringView name)
: m_name(KString::try_create(name).release_nonnull())
: m_name(KString::try_create(name).release_value()) // FIXME: Handle KString allocation failure.
, m_component_index(allocate_inode_index())
{
}

View file

@ -282,11 +282,8 @@ KResult TmpFSInode::add_child(Inode& child, StringView const& name, mode_t)
if (name.length() > NAME_MAX)
return ENAMETOOLONG;
auto name_kstring = KString::try_create(name);
if (!name_kstring)
return ENOMEM;
auto* child_entry = new (nothrow) Child { name_kstring.release_nonnull(), static_cast<TmpFSInode&>(child) };
auto name_kstring = TRY(KString::try_create(name));
auto* child_entry = new (nothrow) Child { move(name_kstring), static_cast<TmpFSInode&>(child) };
if (!child_entry)
return ENOMEM;

View file

@ -323,9 +323,7 @@ KResultOr<NonnullRefPtr<FileDescription>> VirtualFileSystem::create(StringView p
{
auto basename = KLexicalPath::basename(path);
auto parent_path = TRY(parent_custody.try_serialize_absolute_path());
auto full_path = KLexicalPath::try_join(parent_path->view(), basename);
if (!full_path)
return ENOMEM;
auto full_path = TRY(KLexicalPath::try_join(parent_path->view(), basename));
TRY(validate_path_against_process_veil(full_path->view(), options));
if (!is_socket(mode) && !is_fifo(mode) && !is_block_device(mode) && !is_character_device(mode)) {