From 5563ebab5a4e7d41dccda23e536819eca7b4bb0b Mon Sep 17 00:00:00 2001 From: PrestonLTaylor <95388976+PrestonLTaylor@users.noreply.github.com> Date: Tue, 2 May 2023 22:26:16 +0100 Subject: [PATCH] profile: Prevent crash when -p is supplied a non-integer value Instead of crashing we give a helpful warning message to the user. This also removes a fixme! :^) --- Userland/Utilities/profile.cpp | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/Userland/Utilities/profile.cpp b/Userland/Utilities/profile.cpp index f4a2ec369c3..ddab9d6d3d3 100644 --- a/Userland/Utilities/profile.cpp +++ b/Userland/Utilities/profile.cpp @@ -11,6 +11,8 @@ #include #include +static Optional determine_pid_to_profile(StringView pid_argument, bool all_processes); + ErrorOr serenity_main(Main::Arguments arguments) { Core::ArgsParser args_parser; @@ -86,9 +88,13 @@ ErrorOr serenity_main(Main::Arguments arguments) return 1; } - // FIXME: Handle error case. - pid_t pid = all_processes ? -1 : pid_argument.to_int().release_value(); + auto pid_opt = determine_pid_to_profile(pid_argument, all_processes); + if (!pid_opt.has_value()) { + warnln("-p requires an integer value."); + return 1; + } + pid_t pid = pid_opt.value(); if (wait || enable) { TRY(Core::System::profiling_enable(pid, event_mask)); @@ -116,3 +122,13 @@ ErrorOr serenity_main(Main::Arguments arguments) return 0; } + +static Optional determine_pid_to_profile(StringView pid_argument, bool all_processes) +{ + if (all_processes) { + return { -1 }; + } + + // pid_argument is guaranteed to have a value + return pid_argument.to_int(); +}