diff --git a/Kernel/FileSystem/Custody.cpp b/Kernel/FileSystem/Custody.cpp index 0875f9c828..7e77e514cb 100644 --- a/Kernel/FileSystem/Custody.cpp +++ b/Kernel/FileSystem/Custody.cpp @@ -33,15 +33,10 @@ KResultOr> 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> 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_chain; size_t path_length = 0; @@ -89,9 +80,7 @@ KResultOr> 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> 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 diff --git a/Kernel/FileSystem/DevFS.cpp b/Kernel/FileSystem/DevFS.cpp index d3cba22692..841197c216 100644 --- a/Kernel/FileSystem/DevFS.cpp +++ b/Kernel/FileSystem/DevFS.cpp @@ -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> 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)) diff --git a/Kernel/FileSystem/SysFSComponent.cpp b/Kernel/FileSystem/SysFSComponent.cpp index 7bf0278619..1df44166b3 100644 --- a/Kernel/FileSystem/SysFSComponent.cpp +++ b/Kernel/FileSystem/SysFSComponent.cpp @@ -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()) { } diff --git a/Kernel/FileSystem/TmpFS.cpp b/Kernel/FileSystem/TmpFS.cpp index 6c6339a99b..d752d6c1e0 100644 --- a/Kernel/FileSystem/TmpFS.cpp +++ b/Kernel/FileSystem/TmpFS.cpp @@ -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(child) }; + auto name_kstring = TRY(KString::try_create(name)); + auto* child_entry = new (nothrow) Child { move(name_kstring), static_cast(child) }; if (!child_entry) return ENOMEM; diff --git a/Kernel/FileSystem/VirtualFileSystem.cpp b/Kernel/FileSystem/VirtualFileSystem.cpp index 3b456f8075..a0a9756633 100644 --- a/Kernel/FileSystem/VirtualFileSystem.cpp +++ b/Kernel/FileSystem/VirtualFileSystem.cpp @@ -323,9 +323,7 @@ KResultOr> 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)) { diff --git a/Kernel/KLexicalPath.cpp b/Kernel/KLexicalPath.cpp index 63defedf7e..1cdd9fb35f 100644 --- a/Kernel/KLexicalPath.cpp +++ b/Kernel/KLexicalPath.cpp @@ -62,7 +62,7 @@ Vector parts(StringView const& path) return path.split_view('/'); } -OwnPtr try_join(StringView const& first, StringView const& second) +KResultOr> try_join(StringView const& first, StringView const& second) { VERIFY(is_canonical(first)); VERIFY(is_canonical(second)); @@ -70,24 +70,19 @@ OwnPtr try_join(StringView const& first, StringView const& second) if (first == "/"sv) { char* buffer; - auto string = KString::try_create_uninitialized(1 + second.length(), buffer); - if (!string) - return {}; + auto string = TRY(KString::try_create_uninitialized(1 + second.length(), buffer)); buffer[0] = '/'; __builtin_memcpy(buffer + 1, second.characters_without_null_termination(), second.length()); buffer[string->length()] = 0; return string; - } else { - char* buffer; - auto string = KString::try_create_uninitialized(first.length() + 1 + second.length(), buffer); - if (!string) - return string; - __builtin_memcpy(buffer, first.characters_without_null_termination(), first.length()); - buffer[first.length()] = '/'; - __builtin_memcpy(buffer + first.length() + 1, second.characters_without_null_termination(), second.length()); - buffer[string->length()] = 0; - return string; } + char* buffer; + auto string = TRY(KString::try_create_uninitialized(first.length() + 1 + second.length(), buffer)); + __builtin_memcpy(buffer, first.characters_without_null_termination(), first.length()); + buffer[first.length()] = '/'; + __builtin_memcpy(buffer + first.length() + 1, second.characters_without_null_termination(), second.length()); + buffer[string->length()] = 0; + return string; } } diff --git a/Kernel/KLexicalPath.h b/Kernel/KLexicalPath.h index b398e7ca81..a8564fd81c 100644 --- a/Kernel/KLexicalPath.h +++ b/Kernel/KLexicalPath.h @@ -17,6 +17,6 @@ StringView basename(StringView const&); StringView dirname(StringView const&); Vector parts(StringView const&); -OwnPtr try_join(StringView const&, StringView const&); +KResultOr> try_join(StringView const&, StringView const&); } diff --git a/Kernel/KString.cpp b/Kernel/KString.cpp index da5c3bf76c..8a6582c7cd 100644 --- a/Kernel/KString.cpp +++ b/Kernel/KString.cpp @@ -10,49 +10,43 @@ extern bool g_in_early_boot; namespace Kernel { -OwnPtr KString::try_create(StringView const& string) +KResultOr> KString::try_create(StringView string) { char* characters = nullptr; size_t length = string.length(); - auto new_string = KString::try_create_uninitialized(length, characters); - if (!new_string) - return {}; + auto new_string = TRY(KString::try_create_uninitialized(length, characters)); if (!string.is_empty()) __builtin_memcpy(characters, string.characters_without_null_termination(), length); characters[length] = '\0'; return new_string; } -NonnullOwnPtr KString::must_create(StringView const& string) +NonnullOwnPtr KString::must_create(StringView string) { // We can only enforce success during early boot. VERIFY(g_in_early_boot); - auto kstring = KString::try_create(string); - VERIFY(kstring != nullptr); - return kstring.release_nonnull(); + return KString::try_create(string).release_value(); } -OwnPtr KString::try_create_uninitialized(size_t length, char*& characters) +KResultOr> KString::try_create_uninitialized(size_t length, char*& characters) { size_t allocation_size = sizeof(KString) + (sizeof(char) * length) + sizeof(char); auto* slot = kmalloc(allocation_size); if (!slot) - return {}; - auto* new_string = new (slot) KString(length); + return ENOMEM; + auto new_string = TRY(adopt_nonnull_own_or_enomem(new (slot) KString(length))); characters = new_string->m_characters; - return adopt_own_if_nonnull(new_string); + return new_string; } NonnullOwnPtr KString::must_create_uninitialized(size_t length, char*& characters) { // We can only enforce success during early boot. VERIFY(g_in_early_boot); - auto kstring = KString::try_create_uninitialized(length, characters); - VERIFY(kstring != nullptr); - return kstring.release_nonnull(); + return KString::try_create_uninitialized(length, characters).release_value(); } -OwnPtr KString::try_clone() const +KResultOr> KString::try_clone() const { return try_create(view()); } diff --git a/Kernel/KString.h b/Kernel/KString.h index 101bd974cb..2f157dfbec 100644 --- a/Kernel/KString.h +++ b/Kernel/KString.h @@ -16,14 +16,14 @@ class KString { AK_MAKE_NONMOVABLE(KString); public: - [[nodiscard]] static OwnPtr try_create_uninitialized(size_t, char*&); + [[nodiscard]] static KResultOr> try_create_uninitialized(size_t, char*&); [[nodiscard]] static NonnullOwnPtr must_create_uninitialized(size_t, char*&); - [[nodiscard]] static OwnPtr try_create(StringView const&); - [[nodiscard]] static NonnullOwnPtr must_create(StringView const&); + [[nodiscard]] static KResultOr> try_create(StringView); + [[nodiscard]] static NonnullOwnPtr must_create(StringView); void operator delete(void*); - [[nodiscard]] OwnPtr try_clone() const; + [[nodiscard]] KResultOr> try_clone() const; [[nodiscard]] bool is_empty() const { return m_length == 0; } [[nodiscard]] size_t length() const { return m_length; } diff --git a/Kernel/Memory/AddressSpace.cpp b/Kernel/Memory/AddressSpace.cpp index a193910d1a..22f7028cd0 100644 --- a/Kernel/Memory/AddressSpace.cpp +++ b/Kernel/Memory/AddressSpace.cpp @@ -140,8 +140,12 @@ KResultOr AddressSpace::try_allocate_range(VirtualAddress vaddr, s KResultOr AddressSpace::try_allocate_split_region(Region const& source_region, VirtualRange const& range, size_t offset_in_vmobject) { + OwnPtr region_name; + if (!source_region.name().is_null()) + region_name = TRY(KString::try_create(source_region.name())); + auto new_region = TRY(Region::try_create_user_accessible( - range, source_region.vmobject(), offset_in_vmobject, KString::try_create(source_region.name()), source_region.access(), source_region.is_cacheable() ? Region::Cacheable::Yes : Region::Cacheable::No, source_region.is_shared())); + range, source_region.vmobject(), offset_in_vmobject, move(region_name), source_region.access(), source_region.is_cacheable() ? Region::Cacheable::Yes : Region::Cacheable::No, source_region.is_shared())); auto* region = TRY(add_region(move(new_region))); region->set_syscall_region(source_region.is_syscall_region()); region->set_mmap(source_region.is_mmap()); @@ -157,8 +161,11 @@ KResultOr AddressSpace::try_allocate_split_region(Region const& source_ KResultOr AddressSpace::allocate_region(VirtualRange const& range, StringView name, int prot, AllocationStrategy strategy) { VERIFY(range.is_valid()); + OwnPtr region_name; + if (!name.is_null()) + region_name = TRY(KString::try_create(name)); auto vmobject = TRY(AnonymousVMObject::try_create_with_size(range.size(), strategy)); - auto region = TRY(Region::try_create_user_accessible(range, move(vmobject), 0, KString::try_create(name), prot_to_region_access_flags(prot), Region::Cacheable::Yes, false)); + auto region = TRY(Region::try_create_user_accessible(range, move(vmobject), 0, move(region_name), prot_to_region_access_flags(prot), Region::Cacheable::Yes, false)); TRY(region->map(page_directory())); return add_region(move(region)); } @@ -180,7 +187,10 @@ KResultOr AddressSpace::allocate_region_with_vmobject(VirtualRange cons return EINVAL; } offset_in_vmobject &= PAGE_MASK; - auto region = TRY(Region::try_create_user_accessible(range, move(vmobject), offset_in_vmobject, KString::try_create(name), prot_to_region_access_flags(prot), Region::Cacheable::Yes, shared)); + OwnPtr region_name; + if (!name.is_null()) + region_name = TRY(KString::try_create(name)); + auto region = TRY(Region::try_create_user_accessible(range, move(vmobject), offset_in_vmobject, move(region_name), prot_to_region_access_flags(prot), Region::Cacheable::Yes, shared)); auto* added_region = TRY(add_region(move(region))); TRY(added_region->map(page_directory())); return added_region; diff --git a/Kernel/Memory/MemoryManager.cpp b/Kernel/Memory/MemoryManager.cpp index e5d2337ff0..ab9c6b0f8c 100644 --- a/Kernel/Memory/MemoryManager.cpp +++ b/Kernel/Memory/MemoryManager.cpp @@ -729,7 +729,10 @@ KResultOr> MemoryManager::allocate_kernel_region(PhysicalA KResultOr> MemoryManager::allocate_kernel_region_with_vmobject(VirtualRange const& range, VMObject& vmobject, StringView name, Region::Access access, Region::Cacheable cacheable) { - auto region = TRY(Region::try_create_kernel_only(range, vmobject, 0, KString::try_create(name), access, cacheable)); + OwnPtr name_kstring; + if (!name.is_null()) + name_kstring = TRY(KString::try_create(name)); + auto region = TRY(Region::try_create_kernel_only(range, vmobject, 0, move(name_kstring), access, cacheable)); TRY(region->map(kernel_page_directory())); return region; } diff --git a/Kernel/Memory/Region.cpp b/Kernel/Memory/Region.cpp index 914e8ce0cc..559b540766 100644 --- a/Kernel/Memory/Region.cpp +++ b/Kernel/Memory/Region.cpp @@ -60,8 +60,13 @@ KResultOr> Region::try_clone() VERIFY(vmobject().is_shared_inode()); // Create a new region backed by the same VMObject. + + OwnPtr region_name; + if (m_name) + region_name = TRY(m_name->try_clone()); + auto region = TRY(Region::try_create_user_accessible( - m_range, m_vmobject, m_offset_in_vmobject, m_name ? m_name->try_clone() : OwnPtr {}, access(), m_cacheable ? Cacheable::Yes : Cacheable::No, m_shared)); + m_range, m_vmobject, m_offset_in_vmobject, move(region_name), access(), m_cacheable ? Cacheable::Yes : Cacheable::No, m_shared)); region->set_mmap(m_mmap); region->set_shared(m_shared); region->set_syscall_region(is_syscall_region()); @@ -75,8 +80,13 @@ KResultOr> Region::try_clone() // Set up a COW region. The parent (this) region becomes COW as well! remap(); + + OwnPtr clone_region_name; + if (m_name) + clone_region_name = TRY(m_name->try_clone()); + auto clone_region = TRY(Region::try_create_user_accessible( - m_range, vmobject_clone, m_offset_in_vmobject, m_name ? m_name->try_clone() : OwnPtr {}, access(), m_cacheable ? Cacheable::Yes : Cacheable::No, m_shared)); + m_range, vmobject_clone, m_offset_in_vmobject, move(clone_region_name), access(), m_cacheable ? Cacheable::Yes : Cacheable::No, m_shared)); if (m_stack) { VERIFY(is_readable()); diff --git a/Kernel/Net/LocalSocket.cpp b/Kernel/Net/LocalSocket.cpp index 8e5fadf3ce..8b03d388f6 100644 --- a/Kernel/Net/LocalSocket.cpp +++ b/Kernel/Net/LocalSocket.cpp @@ -127,9 +127,10 @@ KResult LocalSocket::bind(Userspace user_address, socklen_t add if (address.sun_family != AF_LOCAL) return set_so_error(EINVAL); - auto path = KString::try_create(StringView { address.sun_path, strnlen(address.sun_path, sizeof(address.sun_path)) }); - if (!path) - return set_so_error(ENOMEM); + auto path_kstring_or_error = KString::try_create(StringView { address.sun_path, strnlen(address.sun_path, sizeof(address.sun_path)) }); + if (path_kstring_or_error.is_error()) + return set_so_error(path_kstring_or_error.error()); + auto path = path_kstring_or_error.release_value(); dbgln_if(LOCAL_SOCKET_DEBUG, "LocalSocket({}) bind({})", this, path); @@ -176,9 +177,10 @@ KResult LocalSocket::connect(FileDescription& description, Userspace> LocalSocket::recvfd(const FileDescript KResult LocalSocket::try_set_path(StringView path) { - auto kstring = KString::try_create(path); - if (!kstring) - return ENOMEM; - m_path = move(kstring); + m_path = TRY(KString::try_create(path)); return KSuccess; } diff --git a/Kernel/Process.cpp b/Kernel/Process.cpp index 23e095d047..cbe5faebf1 100644 --- a/Kernel/Process.cpp +++ b/Kernel/Process.cpp @@ -864,11 +864,9 @@ KResult Process::set_coredump_property(NonnullOwnPtr key, NonnullOwnPtr KResult Process::try_set_coredump_property(StringView key, StringView value) { - auto key_kstring = KString::try_create(key); - auto value_kstring = KString::try_create(value); - if (key_kstring && value_kstring) - return set_coredump_property(key_kstring.release_nonnull(), value_kstring.release_nonnull()); - return ENOMEM; + auto key_kstring = TRY(KString::try_create(key)); + auto value_kstring = TRY(KString::try_create(value)); + return set_coredump_property(move(key_kstring), move(value_kstring)); }; static constexpr StringView to_string(Pledge promise) diff --git a/Kernel/ProcessExposed.cpp b/Kernel/ProcessExposed.cpp index 34b979cdba..87e7956539 100644 --- a/Kernel/ProcessExposed.cpp +++ b/Kernel/ProcessExposed.cpp @@ -86,7 +86,10 @@ ProcFSExposedComponent::ProcFSExposedComponent() ProcFSExposedComponent::ProcFSExposedComponent(StringView name) : m_component_index(s_allocate_global_inode_index()) { - m_name = KString::try_create(name); + auto name_or_error = KString::try_create(name); + if (name_or_error.is_error()) + TODO(); + m_name = name_or_error.release_value(); } ProcFSExposedDirectory::ProcFSExposedDirectory(StringView name) diff --git a/Kernel/StdLib.cpp b/Kernel/StdLib.cpp index 3cc73a0ea6..5bb48ea2b0 100644 --- a/Kernel/StdLib.cpp +++ b/Kernel/StdLib.cpp @@ -26,20 +26,18 @@ Kernel::KResultOr> try_copy_kstring_from_user(Use return EFAULT; } char* buffer; - auto new_string = Kernel::KString::try_create_uninitialized(length, buffer); - if (!new_string) - return ENOMEM; + auto new_string = TRY(Kernel::KString::try_create_uninitialized(length, buffer)); buffer[length] = '\0'; if (length == 0) - return new_string.release_nonnull(); + return new_string; if (!Kernel::safe_memcpy(buffer, user_str.unsafe_userspace_ptr(), (size_t)length, fault_at)) { dbgln("copy_kstring_from_user({:p}, {}) failed at {} (memcpy)", static_cast(user_str.unsafe_userspace_ptr()), user_str_size, VirtualAddress { fault_at }); return EFAULT; } - return new_string.release_nonnull(); + return new_string; } [[nodiscard]] Optional