]> Savannah Git Hosting - gnulib.git/commitdiff
getlogin_r: Work around bug in Mac OS X 10.12.
authorBruno Haible <bruno@clisp.org>
Sun, 23 Apr 2017 00:45:19 +0000 (02:45 +0200)
committerBruno Haible <bruno@clisp.org>
Sun, 23 Apr 2017 00:46:27 +0000 (02:46 +0200)
* m4/getlogin_r.m4 (gl_FUNC_GETLOGIN_R): Test also against the Mac OS X
bug.
* lib/getlogin_r.c (getlogin_r): When getlogin_r returns a string of the
given size minus 1, call getlogin_r a second time, on a larger buffer.
* modules/getlogin_r (Depends-on): Add malloca.
* doc/posix-functions/getlogin_r.texi: Mention the Mac OS X bug.

ChangeLog
doc/posix-functions/getlogin_r.texi
lib/getlogin_r.c
m4/getlogin_r.m4
modules/getlogin_r

index bdebb9458995a4b476ea6c54a8a4760f5be842f1..25fe006255891252ceacbc23288326db5ec13669 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2017-04-22  Bruno Haible  <bruno@clisp.org>
+
+        getlogin_r: Work around bug in Mac OS X 10.12.
+        * m4/getlogin_r.m4 (gl_FUNC_GETLOGIN_R): Test also against the Mac OS X
+        bug.
+        * lib/getlogin_r.c (getlogin_r): When getlogin_r returns a string of the
+        given size minus 1, call getlogin_r a second time, on a larger buffer.
+        * modules/getlogin_r (Depends-on): Add malloca.
+        * doc/posix-functions/getlogin_r.texi: Mention the Mac OS X bug.
+
 2017-04-22  Paul Eggert  <eggert@cs.ucla.edu>
 
        parse-datetime: fix %z and prefer signed int
index 6efb9771eabddcf9572f1fe5ab2301cae1b93f9c..09aa5f166747d235edf16a2d65ce158c60acad7a 100644 (file)
@@ -18,7 +18,7 @@ HP-UX 11.
 @item
 This function returns a truncated result, instead of failing with error code
 @code{ERANGE}, when the buffer is not large enough, on some platforms:
-OSF/1 5.1.
+Mac OS X 10.12, OSF/1 5.1.
 @end itemize
 
 Portability problems not fixed by Gnulib:
index 352b0413db679cf9edc29dac4c6cbce6779b4f6f..230aba0ef53025a74a9748d9e46e47d9e7ffd243 100644 (file)
@@ -25,6 +25,8 @@
 #include <errno.h>
 #include <string.h>
 
+#include "malloca.h"
+
 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
 # define WIN32_LEAN_AND_MEAN
 # include <windows.h>
@@ -63,9 +65,27 @@ getlogin_r (char *name, size_t size)
   /* Platform with a getlogin_r() function.  */
   int ret = getlogin_r (name, size);
 
-  if (ret == 0 && memchr (name, '\0', size) == NULL)
-    /* name contains a truncated result.  */
-    return ERANGE;
+  if (ret == 0)
+    {
+      const char *nul = memchr (name, '\0', size);
+      if (nul == NULL)
+        /* name contains a truncated result.  */
+        return ERANGE;
+      if (size > 0 && nul == name + size - 1)
+        {
+          /* strlen(name) == size-1.  Determine whether the untruncated result
+             would have had length size-1 or size.  */
+          char *room = (char *) malloca (size + 1);
+          if (room == NULL)
+            return ENOMEM;
+          ret = getlogin_r (room, size + 1);
+          /* The untruncated result should be the same as in the first call.  */
+          if (ret == 0 && memcmp (name, room, size) != 0)
+            /* The untruncated result would have been different.  */
+            ret = ERANGE;
+          freea (room);
+        }
+    }
   return ret;
 #else
   /* Platform with a getlogin() function.  */
index 597bcd00baaa3ad85474f1a7512e03f2333282c4..c00a13e0f1fc623b20b48281777b82828c1a16fc 100644 (file)
@@ -1,4 +1,4 @@
-#serial 11
+#serial 12
 
 # Copyright (C) 2005-2007, 2009-2017 Free Software Foundation, Inc.
 #
@@ -30,8 +30,8 @@ AC_DEFUN([gl_FUNC_GETLOGIN_R],
     HAVE_GETLOGIN_R=0
   else
     HAVE_GETLOGIN_R=1
-    dnl On OSF/1 5.1, getlogin_r returns a truncated result if the buffer is
-    dnl not large enough.
+    dnl On Mac OS X 10.12 and OSF/1 5.1, getlogin_r returns a truncated result
+    dnl if the buffer is not large enough.
     AC_REQUIRE([AC_CANONICAL_HOST])
     AC_CACHE_CHECK([whether getlogin_r works with small buffers],
       [gl_cv_func_getlogin_r_works],
@@ -39,15 +39,16 @@ AC_DEFUN([gl_FUNC_GETLOGIN_R],
         dnl Initial guess, used when cross-compiling.
 changequote(,)dnl
         case "$host_os" in
-                # Guess no on OSF/1.
-          osf*) gl_cv_func_getlogin_r_works="guessing no" ;;
-                # Guess yes otherwise.
-          *)    gl_cv_func_getlogin_r_works="guessing yes" ;;
+                          # Guess no on Mac OS X, OSF/1.
+          darwin* | osf*) gl_cv_func_getlogin_r_works="guessing no" ;;
+                          # Guess yes otherwise.
+          *)              gl_cv_func_getlogin_r_works="guessing yes" ;;
         esac
 changequote([,])dnl
         AC_RUN_IFELSE(
           [AC_LANG_SOURCE([[
 #include <stddef.h>
+#include <string.h>
 #include <unistd.h>
 #if !HAVE_DECL_GETLOGIN_R
 extern
@@ -63,16 +64,19 @@ main (void)
   char buf[100];
 
   if (getlogin_r (buf, 0) == 0)
-    result |= 16;
+    result |= 1;
   if (getlogin_r (buf, 1) == 0)
-    result |= 17;
+    result |= 2;
+  if (getlogin_r (buf, 100) == 0)
+    {
+      size_t n = strlen (buf);
+      if (getlogin_r (buf, n) == 0)
+        result |= 4;
+    }
   return result;
 }]])],
           [gl_cv_func_getlogin_r_works=yes],
-          [case $? in
-             16 | 17) gl_cv_func_getlogin_r_works=no ;;
-           esac
-          ],
+          [gl_cv_func_getlogin_r_works=no],
           [:])
       ])
     case "$gl_cv_func_getlogin_r_works" in
index 169cb441a2f126f40d5e965fd81cd5df40922c13..befc365544207f1393fa45e705dbee10d53ae3ca 100644 (file)
@@ -9,6 +9,7 @@ m4/getlogin.m4
 Depends-on:
 unistd
 extensions
+malloca         [test $HAVE_GETLOGIN_R = 0 || test $REPLACE_GETLOGIN_R = 1]
 memchr          [test $HAVE_GETLOGIN_R = 0 || test $REPLACE_GETLOGIN_R = 1]
 
 configure.ac: