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

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


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG22339e52da4e: [Support] Use thread safe version of getpwuid and getpwnam. (authored by yabinc, committed by pirama).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D137864/new/

https://reviews.llvm.org/D137864

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


Index: llvm/lib/Support/Unix/Path.inc
===================================================================
--- llvm/lib/Support/Unix/Path.inc
+++ llvm/lib/Support/Unix/Path.inc
@@ -676,11 +676,17 @@
 
   // 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 @@
 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;
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D137864.475183.patch
Type: text/x-patch
Size: 1408 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221114/d1ce1406/attachment.bin>


More information about the llvm-commits mailing list