]> Savannah Git Hosting - gnulib.git/commitdiff
getlogin, getlogin_r tests: Really avoid test failure.
authorBruno Haible <bruno@clisp.org>
Sun, 19 Jun 2022 14:14:19 +0000 (16:14 +0200)
committerBruno Haible <bruno@clisp.org>
Wed, 31 Aug 2022 23:13:38 +0000 (01:13 +0200)
Reported by Letu Ren <fantasquex@gmail.com> in
<https://lists.gnu.org/archive/html/bug-gnulib/2022-06/msg00037.html>.

* tests/test-getlogin.h (test_getlogin_result): Parse the contents of
/proc/self/loginuid as an unsigned integer.

ChangeLog
tests/test-getlogin.h

index 3209c58e2100ed1edcce26f805fcac477c73530c..4c5e1afc3c6cd0cbd02d576d87b4d38d3024b7c1 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2022-06-19  Bruno Haible  <bruno@clisp.org>
+
+       getlogin, getlogin_r tests: Really avoid test failure.
+       Reported by Letu Ren <fantasquex@gmail.com> in
+       <https://lists.gnu.org/archive/html/bug-gnulib/2022-06/msg00037.html>.
+       * tests/test-getlogin.h (test_getlogin_result): Parse the contents of
+       /proc/self/loginuid as an unsigned integer.
+
 2022-06-12  Paul Eggert  <eggert@cs.ucla.edu>
 
        fchmodat: port better to MS-Windows etc.
index 96d95ee1c1e954dd6c4bb9f8e19ac816bb314d59..384cd4c576e591be78e6f77f7733f31f367c77f3 100644 (file)
@@ -51,16 +51,25 @@ test_getlogin_result (const char *buf, int err)
 #if defined __linux__
       /* On Linux, it is possible to set up a chroot environment in such a way
          that stdin is connected to a tty and nervertheless /proc/self/loginuid
-         contains "-1".  In this situation, getlogin() and getlogin_r() fail;
-         this is expected.  */
+         contains the value (uid_t)(-1).  In this situation, getlogin() and
+         getlogin_r() fail; this is expected.  */
       bool loginuid_undefined = false;
-      /* Does the special file /proc/self/loginuid contain "-1"?  */
+      /* Does the special file /proc/self/loginuid contain the value
+         (uid_t)(-1)?  */
       FILE *fp = fopen ("/proc/self/loginuid", "r");
       if (fp != NULL)
         {
-          char buf[3];
-          loginuid_undefined =
-            (fread (buf, 1, 3, fp) == 2 && buf[0] == '-' && buf[1] == '1');
+          char buf[21];
+          size_t n = fread (buf, 1, sizeof buf, fp);
+          if (n > 0 && n < sizeof buf)
+            {
+              buf[n] = '\0';
+              errno = 0;
+              char *endptr;
+              unsigned long value = strtoul (buf, &endptr, 10);
+              if (*endptr == '\0' && errno == 0)
+                loginuid_undefined = ((uid_t) value == (uid_t)(-1));
+            }
           fclose (fp);
         }
       if (loginuid_undefined)