From 82b4d7225c147860a881e02ed0a77a90c08d8be1 Mon Sep 17 00:00:00 2001 From: Fabian Homborg Date: Tue, 22 Jan 2019 17:14:33 +0100 Subject: [PATCH] env_get_runtime_path: Check for getpwuid() failure Otherwise this is a NULL dereference and then crash. Fixes #5550. --- src/env.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/env.cpp b/src/env.cpp index 6582f928d..a046d9ab7 100644 --- a/src/env.cpp +++ b/src/env.cpp @@ -1723,12 +1723,16 @@ wcstring env_get_runtime_path() { } else { // Don't rely on $USER being set, as setup_user() has not yet been called. // See https://github.com/fish-shell/fish-shell/issues/5180 - const char *uname = getpwuid(geteuid())->pw_name; + // getpeuid() can't fail, but getpwuid sure can. + auto pwuid = getpwuid(geteuid()); + const char *uname = pwuid ? pwuid->pw_name : NULL; // /tmp/fish.user std::string tmpdir = "/tmp/fish."; - tmpdir.append(uname); + if (uname) { + tmpdir.append(uname); + } - if (check_runtime_path(tmpdir.c_str()) != 0) { + if (!uname || check_runtime_path(tmpdir.c_str()) != 0) { debug(0, L"Runtime path not available."); debug(0, L"Try deleting the directory %s and restarting fish.", tmpdir.c_str()); return result;