[llvm] 22339e5 - [Support] Use thread safe version of getpwuid and getpwnam.

Pirama Arumuga Nainar via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 14 09:49:27 PST 2022


Author: Yabin Cui
Date: 2022-11-14T17:48:44Z
New Revision: 22339e52da4e88af499e232e452f4d8bb358fa05

URL: https://github.com/llvm/llvm-project/commit/22339e52da4e88af499e232e452f4d8bb358fa05
DIFF: https://github.com/llvm/llvm-project/commit/22339e52da4e88af499e232e452f4d8bb358fa05.diff

LOG: [Support] Use thread safe version of getpwuid and getpwnam.

OpenGroup specification doesn't require getpwuid and getpwnam
to be thread-safe. And musl libc has a not thread-safe implementation.
When building clang with musl, this can make clang-scan-deps crash.

Reviewed By: pirama

Differential Revision: https://reviews.llvm.org/D137864

Added: 
    

Modified: 
    llvm/lib/Support/Unix/Path.inc

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Support/Unix/Path.inc b/llvm/lib/Support/Unix/Path.inc
index 3ecc5c971b79f..2eb17474c46f0 100644
--- a/llvm/lib/Support/Unix/Path.inc
+++ b/llvm/lib/Support/Unix/Path.inc
@@ -676,11 +676,17 @@ static void expandTildeExpr(SmallVectorImpl<char> &Path) {
 
   // This is a string of the form ~username/, look up this user's entry in the
   // password database.
-  struct passwd *Entry = nullptr;
+  std::unique_ptr<char[]> Buf;
+  long BufSize = sysconf(_SC_GETPW_R_SIZE_MAX);
+  if (BufSize <= 0)
+    BufSize = 16384;
+  Buf = std::make_unique<char[]>(BufSize);
+  struct passwd Pwd;
   std::string User = Expr.str();
-  Entry = ::getpwnam(User.c_str());
+  struct passwd *Entry = nullptr;
+  getpwnam_r(User.c_str(), &Pwd, Buf.get(), BufSize, &Entry);
 
-  if (!Entry) {
+  if (!Entry || !Entry->pw_dir) {
     // Unable to look up the entry, just return back the original path.
     return;
   }
@@ -1339,9 +1345,16 @@ std::error_code changeFileOwnership(int FD, uint32_t Owner, uint32_t Group) {
 namespace path {
 
 bool home_directory(SmallVectorImpl<char> &result) {
+  std::unique_ptr<char[]> Buf;
   char *RequestedDir = getenv("HOME");
   if (!RequestedDir) {
-    struct passwd *pw = getpwuid(getuid());
+    long BufSize = sysconf(_SC_GETPW_R_SIZE_MAX);
+    if (BufSize <= 0)
+      BufSize = 16384;
+    Buf = std::make_unique<char[]>(BufSize);
+    struct passwd Pwd;
+    struct passwd *pw = nullptr;
+    getpwuid_r(getuid(), &Pwd, Buf.get(), BufSize, &pw);
     if (pw && pw->pw_dir)
       RequestedDir = pw->pw_dir;
   }


        


More information about the llvm-commits mailing list