diff options
| author | Tom Englund <tomenglund26@gmail.com> | 2024-08-27 20:42:30 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-27 20:42:30 +0200 |
| commit | 17ed4fc04cedbaad365bdebf6bfe0160c527f3fe (patch) | |
| tree | 3a69215a796816f9547099fc455a4372695bd16e /src/debug/HyprCtl.cpp | |
| parent | 6a8824253c38584d233380ece1e090dd9e7f2e3e (diff) | |
hyprctl: avoid parsing string::npos on invalid cmd (#7544)
* hyprctl: avoid parsing string::npos on invalid cmd
invalid lines passed to hyprctl keyword made the string parsing try to
parse std::string::npos, avoid that and return an error text instead.
* style
---------
Co-authored-by: Vaxry <vaxry@vaxry.net>
Diffstat (limited to 'src/debug/HyprCtl.cpp')
| -rw-r--r-- | src/debug/HyprCtl.cpp | 26 |
1 files changed, 20 insertions, 6 deletions
diff --git a/src/debug/HyprCtl.cpp b/src/debug/HyprCtl.cpp index 40708cf8..ddd1c1ab 100644 --- a/src/debug/HyprCtl.cpp +++ b/src/debug/HyprCtl.cpp @@ -970,12 +970,26 @@ std::string dispatchRequest(eHyprCtlOutputFormat format, std::string in) { } std::string dispatchKeyword(eHyprCtlOutputFormat format, std::string in) { - // get rid of the keyword keyword - in = in.substr(in.find_first_of(' ') + 1); - - const auto COMMAND = in.substr(0, in.find_first_of(' ')); - - const auto VALUE = in.substr(in.find_first_of(' ') + 1); + // Find the first space to strip the keyword keyword + auto const firstSpacePos = in.find_first_of(' '); + if (firstSpacePos == std::string::npos) // Handle the case where there's no space found (invalid input) + return "Invalid input: no space found"; + + // Strip the keyword + in = in.substr(firstSpacePos + 1); + + // Find the next space for the COMMAND and VALUE + auto const secondSpacePos = in.find_first_of(' '); + if (secondSpacePos == std::string::npos) // Handle the case where there's no second space (invalid input) + return "Invalid input: command and value not properly formatted"; + + // Extract COMMAND and VALUE + const auto COMMAND = in.substr(0, secondSpacePos); + const auto VALUE = in.substr(secondSpacePos + 1); + + // If either COMMAND or VALUE is empty, handle accordingly + if (COMMAND.empty() || VALUE.empty()) + return "Invalid input: command or value is empty"; std::string retval = g_pConfigManager->parseKeyword(COMMAND, VALUE); |
