From 3cba4ba44e7ba3cc8bb67ac71bc61245b5aca347 Mon Sep 17 00:00:00 2001 From: Vaxry Date: Fri, 13 Dec 2024 22:36:35 +0000 Subject: [PATCH] hyprctl: avoid crash on null pwuid fixes #8693 --- hyprctl/main.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/hyprctl/main.cpp b/hyprctl/main.cpp index 4ee2d598c..4092bca08 100644 --- a/hyprctl/main.cpp +++ b/hyprctl/main.cpp @@ -52,11 +52,17 @@ void log(const std::string& str) { std::println("{}", str); } +static int getUID() { + const auto UID = getuid(); + const auto PWUID = getpwuid(UID); + return PWUID ? PWUID->pw_uid : UID; +} + std::string getRuntimeDir() { const auto XDG = getenv("XDG_RUNTIME_DIR"); if (!XDG) { - const std::string USERID = std::to_string(getpwuid(getuid())->pw_uid); + const std::string USERID = std::to_string(getUID()); return "/run/user/" + USERID + "/hypr"; } @@ -168,7 +174,7 @@ int request(std::string arg, int minArgs = 0, bool needRoll = false) { return 2; } - const std::string USERID = std::to_string(getpwuid(getuid())->pw_uid); + const std::string USERID = std::to_string(getUID()); sockaddr_un serverAddress = {0}; serverAddress.sun_family = AF_UNIX; @@ -238,7 +244,7 @@ int requestHyprpaper(std::string arg) { sockaddr_un serverAddress = {0}; serverAddress.sun_family = AF_UNIX; - const std::string USERID = std::to_string(getpwuid(getuid())->pw_uid); + const std::string USERID = std::to_string(getUID()); std::string socketPath = getRuntimeDir() + "/" + instanceSignature + "/.hyprpaper.sock";