From 2abb43d70964cc09f822960c21ca07a597bb1a88 Mon Sep 17 00:00:00 2001 From: apio Date: Tue, 22 Aug 2023 11:54:00 +0200 Subject: [PATCH] kernel+libos: Call Vector::try_reserve where it is appropriate --- kernel/src/binfmt/Script.cpp | 1 + kernel/src/sys/poll.cpp | 4 +++- kernel/src/thread/ThreadImage.cpp | 1 + libos/src/Process.cpp | 8 ++++++++ 4 files changed, 13 insertions(+), 1 deletion(-) diff --git a/kernel/src/binfmt/Script.cpp b/kernel/src/binfmt/Script.cpp index 6af3f612..f9c5f3d8 100644 --- a/kernel/src/binfmt/Script.cpp +++ b/kernel/src/binfmt/Script.cpp @@ -49,6 +49,7 @@ Result ScriptLoader::load(AddressSpace* space) Result> ScriptLoader::cmdline(const String& path, Vector args) { Vector new_args; + TRY(new_args.try_reserve(m_interpreter_cmdline.size() + args.size() + 1)); for (auto& arg : m_interpreter_cmdline) { TRY(new_args.try_append(move(arg))); } auto arg = TRY(path.clone()); TRY(new_args.try_append(move(arg))); diff --git a/kernel/src/sys/poll.cpp b/kernel/src/sys/poll.cpp index 3283098f..cfdc4247 100644 --- a/kernel/src/sys/poll.cpp +++ b/kernel/src/sys/poll.cpp @@ -17,10 +17,12 @@ Result sys_poll(Registers*, SyscallArgs args) if (!MemoryManager::copy_from_user(fds, kfds, nfds * sizeof(pollfd))) return err(EFAULT); - Vector> inodes; auto* current = Scheduler::current(); TRY(check_pledge(current, Promise::p_stdio)); + Vector> inodes; + TRY(inodes.try_reserve(nfds)); + for (nfds_t i = 0; i < nfds; i++) { int fd = kfds[i].fd; diff --git a/kernel/src/thread/ThreadImage.cpp b/kernel/src/thread/ThreadImage.cpp index 9b79e0c1..3d03fd0c 100644 --- a/kernel/src/thread/ThreadImage.cpp +++ b/kernel/src/thread/ThreadImage.cpp @@ -88,6 +88,7 @@ Result ThreadImage::push_mem_on_stack(const u8* mem, usize size) Result ThreadImage::push_string_vector_on_stack(const Vector& vec) { Vector user_vec; + TRY(user_vec.try_reserve(vec.size() + 1)); for (const auto& item : vec) { // Copy each individual string and retrieve a userspace pointer to said copy diff --git a/libos/src/Process.cpp b/libos/src/Process.cpp index 81a40ee2..b533908d 100644 --- a/libos/src/Process.cpp +++ b/libos/src/Process.cpp @@ -26,6 +26,7 @@ namespace os Result Process::exec(StringView path, Slice args, bool search_in_path) { Vector argv; + TRY(argv.try_reserve(args.size() + 1)); for (const auto& arg : args) { TRY(argv.try_append(arg.chars())); } TRY(argv.try_append(nullptr)); @@ -39,6 +40,7 @@ namespace os Result Process::exec(StringView path, Slice args, bool search_in_path) { Vector argv; + TRY(argv.try_reserve(args.size() + 1)); for (const auto& arg : args) { TRY(argv.try_append(arg.chars())); } TRY(argv.try_append(nullptr)); @@ -52,10 +54,12 @@ namespace os Result Process::exec(StringView path, Slice args, Slice env, bool search_in_path) { Vector argv; + TRY(argv.try_reserve(args.size() + 1)); for (const auto& arg : args) { TRY(argv.try_append(arg.chars())); } TRY(argv.try_append(nullptr)); Vector envp; + TRY(envp.try_reserve(env.size() + 1)); for (const auto& arg : env) { TRY(envp.try_append(arg.chars())); } TRY(envp.try_append(nullptr)); @@ -83,6 +87,7 @@ namespace os Result Process::spawn(StringView path, Slice args, bool search_in_path) { Vector argv; + TRY(argv.try_reserve(args.size() + 1)); for (const auto& arg : args) { TRY(argv.try_append(arg.chars())); } TRY(argv.try_append(nullptr)); @@ -92,6 +97,7 @@ namespace os Result Process::spawn(StringView path, Slice args, bool search_in_path) { Vector argv; + TRY(argv.try_reserve(args.size() + 1)); for (const auto& arg : args) { TRY(argv.try_append(arg.chars())); } TRY(argv.try_append(nullptr)); @@ -101,10 +107,12 @@ namespace os Result Process::spawn(StringView path, Slice args, Slice env, bool search_in_path) { Vector argv; + TRY(argv.try_reserve(args.size() + 1)); for (const auto& arg : args) { TRY(argv.try_append(arg.chars())); } TRY(argv.try_append(nullptr)); Vector envp; + TRY(envp.try_reserve(env.size() + 1)); for (const auto& arg : env) { TRY(envp.try_append(arg.chars())); } TRY(envp.try_append(nullptr));