bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

getlogin_r on OSF/1 5.1


From: Bruno Haible
Subject: getlogin_r on OSF/1 5.1
Date: Wed, 22 Dec 2010 19:28:44 +0100
User-agent: KMail/1.9.9

On OSF/1 5.1 I'm seeing this test failure:

test-getlogin_r.c:76: assertion failed
sh: 363055 Abort - core dumped
FAIL: test-getlogin_r

The reason is that the OSF/1 getlogin_r() function produces a truncated
result when the buffer is not large enough. As if with strncpy. This
is not exactly a bug, just a portability problem, because the other
platforms return ERANGE in this case. I'm adding this workaround:


2010-12-22  Bruno Haible  <address@hidden>

        getlogin_r: Work around portability problem on OSF/1.
        * m4/getlogin_r.m4 (gl_FUNC_GETLOGIN_R): Detect the OSF/1 problem.
        * lib/unistd.in.h (getlogin_r): Replace if REPLACE_GETLOGIN_R is set.
        * lib/getlogin_r.c (getlogin_r): When getlogin_r exists, invoke it and
        test for a truncated result.
        * m4/unistd_h.m4 (gl_UNISTD_H_DEFAULTS): Initialize REPLACE_GETLOGIN_R.
        * modules/unistd (Makefile.am): Substitute REPLACE_GETLOGIN_R.
        * modules/getlogin_r (Depends-on): Add memchr.
        * doc/posix-functions/getlogin_r.texi: Mention the OSF/1 problem.

--- doc/posix-functions/getlogin_r.texi.orig    Wed Dec 22 19:20:38 2010
+++ doc/posix-functions/getlogin_r.texi Wed Dec 22 18:41:51 2010
@@ -18,6 +18,10 @@
 @item
 This function has an incompatible declaration on some platforms:
 Solaris 11 2010-11 (when @code{_POSIX_PTHREAD_SEMANTICS} is not defined).
address@hidden
+This function returns a truncated result, instead of failing with error code
address@hidden, when the buffer is not large enough, on some platforms:
+OSF/1 5.1.
 @end itemize
 
 Portability problems not fixed by Gnulib:
--- lib/getlogin_r.c.orig       Wed Dec 22 19:20:38 2010
+++ lib/getlogin_r.c    Wed Dec 22 18:54:12 2010
@@ -1,6 +1,6 @@
 /* Provide a working getlogin_r for systems which lack it.
 
-   Copyright (C) 2005-2007, 2009-2010 Free Software Foundation, Inc.
+   Copyright (C) 2005-2007, 2010 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -39,6 +39,7 @@
 int
 getlogin_r (char *name, size_t size)
 {
+#undef getlogin_r
 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
   /* Native Windows platform.  */
   DWORD sz;
@@ -59,6 +60,14 @@
         return ENOENT;
     }
   return 0;
+#elif HAVE_GETLOGIN_R
+  /* 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;
+  return ret;
 #else
   /* Platform with a getlogin() function.  */
   char *n;
--- lib/unistd.in.h.orig        Wed Dec 22 19:20:38 2010
+++ lib/unistd.in.h     Wed Dec 22 18:43:53 2010
@@ -712,13 +712,22 @@
      ${LOGNAME-$USER}        on Unix platforms,
      $USERNAME               on native Windows platforms.
  */
-# if address@hidden@
+# if @REPLACE_GETLOGIN_R@
+#  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
+#   define getlogin_r rpl_getlogin_r
+#  endif
+_GL_FUNCDECL_RPL (getlogin_r, int, (char *name, size_t size)
+                                   _GL_ARG_NONNULL ((1)));
+_GL_CXXALIAS_RPL (getlogin_r, int, (char *name, size_t size));
+# else
+#  if address@hidden@
 _GL_FUNCDECL_SYS (getlogin_r, int, (char *name, size_t size)
                                    _GL_ARG_NONNULL ((1)));
-# endif
+#  endif
 /* Need to cast, because on Solaris 10 systems, the second argument is
                                                      int size.  */
 _GL_CXXALIAS_SYS_CAST (getlogin_r, int, (char *name, size_t size));
+# endif
 _GL_CXXALIASWARN (getlogin_r);
 #elif defined GNULIB_POSIXCHECK
 # undef getlogin_r
--- m4/getlogin_r.m4.orig       Wed Dec 22 19:20:38 2010
+++ m4/getlogin_r.m4    Wed Dec 22 19:13:45 2010
@@ -1,4 +1,4 @@
-#serial 8
+#serial 9
 
 # Copyright (C) 2005-2007, 2009-2010 Free Software Foundation, Inc.
 #
@@ -27,6 +27,56 @@
 
   AC_CHECK_FUNCS_ONCE([getlogin_r])
   if test $ac_cv_func_getlogin_r = no; then
+    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.
+    AC_REQUIRE([AC_CANONICAL_HOST])
+    AC_CACHE_CHECK([whether getlogin_r works with small buffers],
+      [gl_cv_func_getlogin_r_works],
+      [
+        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" ;;
+        esac
+changequote([,])dnl
+        AC_RUN_IFELSE(
+          [AC_LANG_SOURCE([[
+#include <stddef.h>
+#include <unistd.h>
+#if !HAVE_DECL_GETLOGIN_R
+extern int getlogin_r (char *, size_t);
+#endif
+int
+main (void)
+{
+  int result = 0;
+  char buf[100];
+
+  if (getlogin_r (buf, 0) == 0)
+    result |= 16;
+  if (getlogin_r (buf, 1) == 0)
+    result |= 17;
+  return result;
+}]])],
+          [gl_cv_func_getlogin_r_works=yes],
+          [case $? in
+             16 | 17) gl_cv_func_getlogin_r_works=no ;;
+           esac
+          ],
+          [:])
+      ])
+    case "$gl_cv_func_getlogin_r_works" in
+      *yes) ;;
+      *) REPLACE_GETLOGIN_R=1 ;;
+    esac
+  fi
+  if test $HAVE_GETLOGIN_R = 0 || test $REPLACE_GETLOGIN_R = 1; then
     AC_LIBOBJ([getlogin_r])
     gl_PREREQ_GETLOGIN_R
   fi
--- m4/unistd_h.m4.orig Wed Dec 22 19:20:39 2010
+++ m4/unistd_h.m4      Wed Dec 22 18:45:44 2010
@@ -1,4 +1,4 @@
-# unistd_h.m4 serial 51
+# unistd_h.m4 serial 52
 dnl Copyright (C) 2006-2010 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -140,6 +140,7 @@
   REPLACE_FCHOWNAT=0;     AC_SUBST([REPLACE_FCHOWNAT])
   REPLACE_GETCWD=0;       AC_SUBST([REPLACE_GETCWD])
   REPLACE_GETDOMAINNAME=0; AC_SUBST([REPLACE_GETDOMAINNAME])
+  REPLACE_GETLOGIN_R=0;   AC_SUBST([REPLACE_GETLOGIN_R])
   REPLACE_GETGROUPS=0;    AC_SUBST([REPLACE_GETGROUPS])
   REPLACE_GETPAGESIZE=0;  AC_SUBST([REPLACE_GETPAGESIZE])
   REPLACE_LCHOWN=0;       AC_SUBST([REPLACE_LCHOWN])
--- modules/getlogin_r.orig     Wed Dec 22 19:20:39 2010
+++ modules/getlogin_r  Wed Dec 22 19:20:25 2010
@@ -8,6 +8,7 @@
 Depends-on:
 extensions
 unistd
+memchr
 
 configure.ac:
 gl_FUNC_GETLOGIN_R
--- modules/unistd.orig Wed Dec 22 19:20:39 2010
+++ modules/unistd      Wed Dec 22 18:45:22 2010
@@ -114,6 +114,7 @@
              -e 's|@''REPLACE_FCHOWNAT''@|$(REPLACE_FCHOWNAT)|g' \
              -e 's|@''REPLACE_GETCWD''@|$(REPLACE_GETCWD)|g' \
              -e 's|@''REPLACE_GETDOMAINNAME''@|$(REPLACE_GETDOMAINNAME)|g' \
+             -e 's|@''REPLACE_GETLOGIN_R''@|$(REPLACE_GETLOGIN_R)|g' \
              -e 's|@''REPLACE_GETGROUPS''@|$(REPLACE_GETGROUPS)|g' \
              -e 's|@''REPLACE_GETPAGESIZE''@|$(REPLACE_GETPAGESIZE)|g' \
              -e 's|@''REPLACE_LCHOWN''@|$(REPLACE_LCHOWN)|g' \



reply via email to

[Prev in Thread] Current Thread [Next in Thread]