From f10e4f88b6a0ee5558552e96a500cfa58756a1e3 Mon Sep 17 00:00:00 2001 From: Kurtis Rader Date: Wed, 10 May 2017 21:08:36 -0700 Subject: [PATCH] lint: replace `getpwuid()` with `getpwuid_r()` --- .cppcheck.suppressions | 3 --- src/env.cpp | 9 ++++++--- src/env_universal_common.cpp | 11 +++++------ src/fallback.h | 6 +----- 4 files changed, 12 insertions(+), 17 deletions(-) diff --git a/.cppcheck.suppressions b/.cppcheck.suppressions index a2991396b..154eebed2 100644 --- a/.cppcheck.suppressions +++ b/.cppcheck.suppressions @@ -8,6 +8,3 @@ varFuncNullUB // the warning being suppressed. In other words this unmatchedSuppression // warnings are false positives. unmatchedSuppression - -flockSemanticsWarning:src/env_universal_common.cpp -flockSemanticsWarning:src/history.cpp diff --git a/src/env.cpp b/src/env.cpp index 108fbab4c..d4736ca63 100644 --- a/src/env.cpp +++ b/src/env.cpp @@ -702,9 +702,12 @@ wcstring env_get_pwd_slash(void) { /// Set up the USER variable. static void setup_user(bool force) { if (env_get_string(L"USER").missing_or_empty() || force) { - const struct passwd *pw = getpwuid(getuid()); - if (pw && pw->pw_name) { - const wcstring uname = str2wcstring(pw->pw_name); + struct passwd userinfo; + struct passwd *result; + char buf[8192]; + int retval = getpwuid_r(getuid(), &userinfo, buf, sizeof(buf), &result); + if (!retval && result) { + const wcstring uname = str2wcstring(userinfo.pw_name); env_set(L"USER", uname.c_str(), ENV_GLOBAL | ENV_EXPORT); } } diff --git a/src/env_universal_common.cpp b/src/env_universal_common.cpp index ae1f58402..8466a4ab6 100644 --- a/src/env_universal_common.cpp +++ b/src/env_universal_common.cpp @@ -141,8 +141,11 @@ static wcstring get_runtime_path() { } else { const char *uname = getenv("USER"); if (uname == NULL) { - const struct passwd *pw = getpwuid(getuid()); - uname = pw->pw_name; + struct passwd userinfo; + struct passwd *result; + char buf[8192]; + int retval = getpwuid_r(getuid(), &userinfo, buf, sizeof(buf), &result); + if (!retval && result) uname = userinfo.pw_name; } // /tmp/fish.user @@ -1381,10 +1384,6 @@ std::unique_ptr universal_notifier_t::new_notifier_for_str case strategy_named_pipe: { return make_unique(test_path); } - default: { - debug(0, "Unsupported universal notifier strategy %d\n", strat); - return NULL; - } } } diff --git a/src/fallback.h b/src/fallback.h index f22363f3a..6d5ef8255 100644 --- a/src/fallback.h +++ b/src/fallback.h @@ -176,19 +176,15 @@ int killpg(int pgr, int sig); #endif #ifndef HAVE_FLOCK -/// Fallback implementation of flock in terms of fcntl +/// Fallback implementation of flock in terms of fcntl. /// Danger! The semantics of flock and fcntl locking are very different. /// Use with caution. -// Ignore the cppcheck warning as this is the implementation that it is -// warning about! -// cppcheck-suppress flockSemanticsWarning int flock(int fd, int op); #define LOCK_SH 1 // Shared lock. #define LOCK_EX 2 // Exclusive lock. #define LOCK_UN 8 // Unlock. #define LOCK_NB 4 // Don't block when locking. - #endif #endif