[review] Use getpwuid_r instead of getpwuid when available

Message ID gerrit.1572720018000.I587359267f8963ef1da6ba0223a1525807a721de@gnutoolchain-gerrit.osci.io
State New, archived
Headers

Commit Message

Simon Marchi (Code Review) Nov. 2, 2019, 6:40 p.m. UTC
  Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/485
......................................................................

Use getpwuid_r instead of getpwuid when available

Change-Id: I587359267f8963ef1da6ba0223a1525807a721de
---
M gdb/config.in
M gdb/configure
M gdb/gdbserver/config.in
M gdb/gdbserver/configure
M gdb/gdbsupport/common.m4
M gdb/nat/linux-osdata.c
6 files changed, 17 insertions(+), 4 deletions(-)
  

Comments

Simon Marchi (Code Review) Nov. 11, 2019, 2:57 p.m. UTC | #1
Tom Tromey has posted comments on this change.

Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/485
......................................................................


Patch Set 2:

(1 comment)

Thank you for this and the other "_r" patches.

I think this one can be hugely simplified, see below.

| --- gdb/nat/linux-osdata.c
| +++ gdb/nat/linux-osdata.c
| @@ -204,11 +204,18 @@ /* Finds the user name for the user UID and copies it into USER.  At
|  
|  static void
|  user_from_uid (char *user, int maxlen, uid_t uid)
|  {
| -  struct passwd *pwentry = getpwuid (uid);
| +  struct passwd *pwentry;
| +#ifdef HAVE_GETPWUID_R
| +  char buf[1024];
| +  struct passwd pwd;
| +  getpwuid_r (uid, &pwd, buf, sizeof (buf), &pwentry);

PS2, Line 212:

I think that, because the only use is in a "nat" file,
and because we know this is always available on Linux,
then there's no need for the configury.  We can just call
this unconditionally.

| +#else
| +  pwentry = getpwuid (uid);
| +#endif
|  
|    if (pwentry)
|      {
|        strncpy (user, pwentry->pw_name, maxlen);
|        /* Ensure that the user name is null-terminated.  */
|        user[maxlen - 1] = '\0';
  
Simon Marchi (Code Review) Nov. 11, 2019, 7:36 p.m. UTC | #2
Christian Biesinger has posted comments on this change.

Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/485
......................................................................


Patch Set 3:

(1 comment)

| --- gdb/nat/linux-osdata.c
| +++ gdb/nat/linux-osdata.c
| @@ -204,11 +204,18 @@ /* Finds the user name for the user UID and copies it into USER.  At
|  
|  static void
|  user_from_uid (char *user, int maxlen, uid_t uid)
|  {
| -  struct passwd *pwentry = getpwuid (uid);
| +  struct passwd *pwentry;
| +#ifdef HAVE_GETPWUID_R
| +  char buf[1024];
| +  struct passwd pwd;
| +  getpwuid_r (uid, &pwd, buf, sizeof (buf), &pwentry);

PS2, Line 212:

Oh good point! Done.

| +#else
| +  pwentry = getpwuid (uid);
| +#endif
|  
|    if (pwentry)
|      {
|        strncpy (user, pwentry->pw_name, maxlen);
|        /* Ensure that the user name is null-terminated.  */
|        user[maxlen - 1] = '\0';
  
Simon Marchi (Code Review) Nov. 11, 2019, 11:25 p.m. UTC | #3
Tom Tromey has posted comments on this change.

Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/485
......................................................................


Patch Set 3: Code-Review+2

Looks good!  Thanks.
  

Patch

diff --git a/gdb/config.in b/gdb/config.in
index 5a21fca..ef90fbb 100644
--- a/gdb/config.in
+++ b/gdb/config.in
@@ -186,6 +186,9 @@ 
 /* Define to 1 if you have the `getpgid' function. */
 #undef HAVE_GETPGID
 
+/* Define to 1 if you have the `getpwuid_r' function. */
+#undef HAVE_GETPWUID_R
+
 /* Define to 1 if you have the `getrlimit' function. */
 #undef HAVE_GETRLIMIT
 
diff --git a/gdb/configure b/gdb/configure
index 512f016..fbc6c65 100755
--- a/gdb/configure
+++ b/gdb/configure
@@ -13480,7 +13480,7 @@ 
 
 
   for ac_func in fdwalk getrlimit pipe pipe2 socketpair sigaction \
-		  sigprocmask strerror_r
+		  sigprocmask strerror_r getpwuid_r
 do :
   as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
 ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
diff --git a/gdb/gdbserver/config.in b/gdb/gdbserver/config.in
index 2984281..e53afff 100644
--- a/gdb/gdbserver/config.in
+++ b/gdb/gdbserver/config.in
@@ -114,6 +114,9 @@ 
 /* Define to 1 if you have the `getauxval' function. */
 #undef HAVE_GETAUXVAL
 
+/* Define to 1 if you have the `getpwuid_r' function. */
+#undef HAVE_GETPWUID_R
+
 /* Define to 1 if you have the `getrlimit' function. */
 #undef HAVE_GETRLIMIT
 
diff --git a/gdb/gdbserver/configure b/gdb/gdbserver/configure
index 3f1f1c1..3fe2ec6 100755
--- a/gdb/gdbserver/configure
+++ b/gdb/gdbserver/configure
@@ -6822,7 +6822,7 @@ 
 
 
   for ac_func in fdwalk getrlimit pipe pipe2 socketpair sigaction \
-		  sigprocmask strerror_r
+		  sigprocmask strerror_r getpwuid_r
 do :
   as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
 ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
diff --git a/gdb/gdbsupport/common.m4 b/gdb/gdbsupport/common.m4
index 2e44cf4..19e67e2 100644
--- a/gdb/gdbsupport/common.m4
+++ b/gdb/gdbsupport/common.m4
@@ -33,7 +33,7 @@ 
 		   dlfcn.h)
 
   AC_CHECK_FUNCS([fdwalk getrlimit pipe pipe2 socketpair sigaction \
-		  sigprocmask strerror_r])
+		  sigprocmask strerror_r getpwuid_r])
 
   AC_CHECK_DECLS([strerror, strstr])
 
diff --git a/gdb/nat/linux-osdata.c b/gdb/nat/linux-osdata.c
index 67f9f3a..8b03a41 100644
--- a/gdb/nat/linux-osdata.c
+++ b/gdb/nat/linux-osdata.c
@@ -205,7 +205,14 @@ 
 static void
 user_from_uid (char *user, int maxlen, uid_t uid)
 {
-  struct passwd *pwentry = getpwuid (uid);
+  struct passwd *pwentry;
+#ifdef HAVE_GETPWUID_R
+  char buf[1024];
+  struct passwd pwd;
+  getpwuid_r (uid, &pwd, buf, sizeof (buf), &pwentry);
+#else
+  pwentry = getpwuid (uid);
+#endif
 
   if (pwentry)
     {