This is the mail archive of the fortran@gcc.gnu.org mailing list for the GNU Fortran project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [libfortran,committed] Fix PR31299, GETLOG intrinsic


# define GETLOG (getpwuid(geteuid())->pw_name)

This will segfault if getpwuid returns NULL.

Doh, stupid me. Here's what I committed as rev. 124143.


FX
Index: intrinsics/getlog.c
===================================================================
--- intrinsics/getlog.c	(revision 124142)
+++ intrinsics/getlog.c	(working copy)
@@ -37,8 +37,13 @@
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
+#ifdef HAVE_SYS_TYPES_H
+#include <sys/types.h>
+#endif
+#ifdef HAVE_PWD_H
+#include <pwd.h>
+#endif
 
-
 /* Windows32 version */
 #if defined __MINGW32__ && !defined  HAVE_GETLOGIN
 #define WIN32_LEAN_AND_MEAN
@@ -66,7 +71,6 @@
    process.
    CHARACTER(len=*), INTENT(OUT) :: LOGIN  */
 
-#ifdef HAVE_GETLOGIN
 void PREFIX(getlog) (char *, gfc_charlen_type);
 export_proto_np(PREFIX(getlog));
 
@@ -78,7 +82,22 @@
 
   memset (login, ' ', login_len); /* Blank the string.  */
 
-  p = getlogin ();
+#if defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
+  {
+    struct passwd *pw = getpwuid (geteuid ());
+    if (pw)
+      p = pw->pw_name;
+    else
+      return;
+  }
+#else
+# ifdef HAVE_GETLOGIN
+  p = getlogin();
+# else
+  return;
+# endif
+#endif
+
   if (p == NULL)
     return;
 
@@ -88,4 +107,3 @@
   else
     memcpy (login, p, p_len);
 }
-#endif
Index: configure.ac
===================================================================
--- configure.ac	(revision 124142)
+++ configure.ac	(working copy)
@@ -178,7 +178,7 @@
 AC_HAVE_HEADERS(stdlib.h string.h unistd.h signal.h)
 AC_CHECK_HEADERS(time.h sys/time.h sys/times.h sys/resource.h)
 AC_CHECK_HEADERS(sys/types.h sys/stat.h sys/wait.h floatingpoint.h ieeefp.h)
-AC_CHECK_HEADERS(fenv.h fptrap.h float.h execinfo.h)
+AC_CHECK_HEADERS(fenv.h fptrap.h float.h execinfo.h pwd.h)
 AC_CHECK_HEADER([complex.h],[AC_DEFINE([HAVE_COMPLEX_H], [1], [complex.h exists])])
 GCC_HEADER_STDINT(gstdint.h)
 
@@ -191,7 +191,7 @@
 AC_CHECK_FUNCS(chdir strerror getlogin gethostname kill link symlink perror)
 AC_CHECK_FUNCS(sleep time ttyname signal alarm ctime clock access fork execl)
 AC_CHECK_FUNCS(wait setmode execvp pipe dup2 close fdopen strcasestr getrlimit)
-AC_CHECK_FUNCS(gettimeofday stat fstat lstat)
+AC_CHECK_FUNCS(gettimeofday stat fstat lstat getpwuid)
 
 # Check for glibc backtrace functions
 AC_CHECK_FUNCS(backtrace backtrace_symbols)
@@ -204,9 +204,9 @@
 AC_CHECK_LIB([c],[getpid],[AC_DEFINE([HAVE_GETPID],[1],[libc includes getpid])])
 AC_CHECK_LIB([c],[getppid],[AC_DEFINE([HAVE_GETPPID],[1],[libc includes getppid])])
 AC_CHECK_LIB([c],[getuid],[AC_DEFINE([HAVE_GETUID],[1],[libc includes getuid])])
+AC_CHECK_LIB([c],[geteuid],[AC_DEFINE([HAVE_GETEUID],[1],[libc includes geteuid])])
 
 # Check for C99 (and other IEEE) math functions
-# ??? This list seems awful long. Is there a better way to test for these?
 AC_CHECK_LIB([m],[acosf],[AC_DEFINE([HAVE_ACOSF],[1],[libm includes acosf])])
 AC_CHECK_LIB([m],[acos],[AC_DEFINE([HAVE_ACOS],[1],[libm includes acos])])
 AC_CHECK_LIB([m],[acosl],[AC_DEFINE([HAVE_ACOSL],[1],[libm includes acosl])])

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]