PR 78534, 83704 Handle large formatted I/O
[gcc.git] / libgfortran / runtime / error.c
index e61904496c8f802fcd7489100f98fa04171a04ca..1a53e2f72f138c0c06d5b84aeb5ea3b24108c08e 100644 (file)
@@ -1,5 +1,4 @@
-/* Copyright (C) 2002, 2003, 2005, 2006, 2007, 2009, 2010, 2011
-   Free Software Foundation, Inc.
+/* Copyright (C) 2002-2018 Free Software Foundation, Inc.
    Contributed by Andy Vaught
 
 This file is part of the GNU Fortran runtime library (libgfortran).
@@ -28,19 +27,12 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 #include <assert.h>
 #include <string.h>
 #include <errno.h>
-
-#ifdef HAVE_SIGNAL_H
 #include <signal.h>
-#endif
 
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
 
-#ifdef HAVE_STDLIB_H
-#include <stdlib.h>
-#endif
-
 #ifdef HAVE_SYS_TIME_H
 #include <sys/time.h>
 #endif
@@ -52,50 +44,47 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 #endif
 
 
+#include <locale.h>
+
+#ifdef HAVE_XLOCALE_H
+#include <xlocale.h>
+#endif
+
+
 #ifdef __MINGW32__
 #define HAVE_GETPID 1
 #include <process.h>
 #endif
 
 
-/* sys_exit()-- Terminate the program with an exit code.  */
+/* Termination of a program: F2008 2.3.5 talks about "normal
+   termination" and "error termination". Normal termination occurs as
+   a result of e.g. executing the end program statement, and executing
+   the STOP statement. It includes the effect of the C exit()
+   function. 
 
-void
-sys_exit (int code)
-{
-  /* Show error backtrace if possible.  */
-  if (code != 0 && code != 4
-      && (options.backtrace == 1
-         || (options.backtrace == -1 && compile_options.backtrace == 1)))
-    show_backtrace ();
-
-  /* Dump core if requested.  */
-  if (code != 0
-      && (options.dump_core == 1
-        || (options.dump_core == -1 && compile_options.dump_core == 1)))
-    {
-#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_CORE)
-      /* Warn if a core file cannot be produced because
-        of core size limit.  */
+   Error termination is initiated when the ERROR STOP statement is
+   executed, when ALLOCATE/DEALLOCATE fails without STAT= being
+   specified, when some of the co-array synchronization statements
+   fail without STAT= being specified, and some I/O errors if
+   ERR/IOSTAT/END/EOR is not present, and finally EXECUTE_COMMAND_LINE
+   failure without CMDSTAT=.
 
-      struct rlimit core_limit;
+   2.3.5 also explains how co-images synchronize during termination.
 
-      if (getrlimit (RLIMIT_CORE, &core_limit) == 0 && core_limit.rlim_cur == 0)
-       estr_write ("** Warning: a core dump was requested, but the core size"
-                  "limit\n**          is currently zero.\n\n");
-#endif
-      
-      
-#if defined(HAVE_KILL) && defined(HAVE_GETPID) && defined(SIGQUIT)
-      kill (getpid (), SIGQUIT);
-#else
-      estr_write ("Core dump not possible, sorry.");
-#endif
-    }
-
-  exit (code);
-}
+   In libgfortran we have three ways of ending a program. exit(code)
+   is a normal exit; calling exit() also causes open units to be
+   closed. No backtrace or core dump is needed here.  For error
+   termination, we have exit_error(status), which prints a backtrace
+   if backtracing is enabled, then exits.  Finally, when something
+   goes terribly wrong, we have sys_abort() which tries to print the
+   backtrace if -fbacktrace is enabled, and then dumps core; whether a
+   core file is generated is system dependent. When aborting, we don't
+   flush and close open units, as program memory might be corrupted
+   and we'd rather risk losing dirty data in the buffers rather than
+   corrupting files on disk.
 
+*/
 
 /* Error conditions.  The tricky part here is printing a message when
  * it is the I/O subsystem that is severely wounded.  Our goal is to
@@ -107,7 +96,6 @@ sys_exit (int code)
  * 1    Terminated because of operating system error.
  * 2    Error in the runtime library
  * 3    Internal error in runtime library
- * 4    Error during error processing (very bad)
  *
  * Other error returns are reserved for the STOP statement with a numeric code.
  */
@@ -150,7 +138,7 @@ st_vprintf (const char *format, va_list ap)
 #define ERROR_MESSAGE "Internal error: buffer overrun in st_vprintf()\n"
       write (STDERR_FILENO, buffer, ST_VPRINTF_SIZE - 1);
       write (STDERR_FILENO, ERROR_MESSAGE, strlen(ERROR_MESSAGE));
-      sys_exit(2);
+      sys_abort ();
 #undef ERROR_MESSAGE
 
     }
@@ -173,6 +161,43 @@ st_printf (const char * format, ...)
 }
 
 
+/* sys_abort()-- Terminate the program showing backtrace and dumping
+   core.  */
+
+void
+sys_abort (void)
+{
+  /* If backtracing is enabled, print backtrace and disable signal
+     handler for ABRT.  */
+  if (options.backtrace == 1
+      || (options.backtrace == -1 && compile_options.backtrace == 1))
+    {
+      estr_write ("\nProgram aborted. Backtrace:\n");
+      show_backtrace (false);
+      signal (SIGABRT, SIG_DFL);
+    }
+
+  abort();
+}
+
+
+/* Exit in case of error termination. If backtracing is enabled, print
+   backtrace, then exit.  */
+
+void
+exit_error (int status)
+{
+  if (options.backtrace == 1
+      || (options.backtrace == -1 && compile_options.backtrace == 1))
+    {
+      estr_write ("\nError termination. Backtrace:\n");
+      show_backtrace (false);
+    }
+  exit (status);
+}
+
+
+
 /* gfc_xtoa()-- Integer to hexadecimal conversion.  */
 
 const char *
@@ -203,27 +228,45 @@ gfc_xtoa (GFC_UINTEGER_LARGEST n, char *buffer, size_t len)
 }
 
 
-/* Hopefully thread-safe wrapper for a strerror_r() style function.  */
+/* Hopefully thread-safe wrapper for a strerror() style function.  */
 
 char *
 gf_strerror (int errnum, 
              char * buf __attribute__((unused)), 
             size_t buflen __attribute__((unused)))
 {
-#ifdef HAVE_STRERROR_R
-  /* TODO: How to prevent the compiler warning due to strerror_r of
-     the untaken branch having the wrong return type?  */
-  if (__builtin_classify_type (strerror_r (0, buf, 0)) == 5)
+#ifdef HAVE_STRERROR_L
+  locale_t myloc = newlocale (LC_CTYPE_MASK | LC_MESSAGES_MASK, "",
+                             (locale_t) 0);
+  char *p;
+  if (myloc)
     {
-      /* GNU strerror_r()  */
-      return strerror_r (errnum, buf, buflen);
+      p = strerror_l (errnum, myloc);
+      freelocale (myloc);
     }
   else
-    {
-      /* POSIX strerror_r ()  */
-      strerror_r (errnum, buf, buflen);
-      return buf;
-    }
+    /* newlocale might fail e.g. due to running out of memory, fall
+       back to the simpler strerror.  */
+    p = strerror (errnum);
+  return p;
+#elif defined(HAVE_STRERROR_R)
+#ifdef HAVE_USELOCALE
+  /* Some targets (Darwin at least) have the POSIX 2008 extended
+     locale functions, but not strerror_l.  So reset the per-thread
+     locale here.  */
+  uselocale (LC_GLOBAL_LOCALE);
+#endif
+  /* POSIX returns an "int", GNU a "char*".  */
+  return
+    __builtin_choose_expr (__builtin_classify_type (strerror_r (0, buf, 0))
+                          == 5,
+                          /* GNU strerror_r()  */
+                          strerror_r (errnum, buf, buflen),
+                          /* POSIX strerror_r ()  */
+                          (strerror_r (errnum, buf, buflen), buf));
+#elif defined(HAVE_STRERROR_R_2ARGS)
+  strerror_r (errnum, buf);
+  return buf;
 #else
   /* strerror () is not necessarily thread-safe, but should at least
      be available everywhere.  */
@@ -278,7 +321,7 @@ recursion_check (void)
 
   /* Don't even try to print something at this point */
   if (magic == MAGIC)
-    sys_exit (4);
+    sys_abort ();
 
   magic = MAGIC;
 }
@@ -300,7 +343,7 @@ os_error (const char *message)
   estr_write ("\n");
   estr_write (message);
   estr_write ("\n");
-  sys_exit (1);
+  exit_error (1);
 }
 iexport(os_error);
 
@@ -319,7 +362,7 @@ runtime_error (const char *message, ...)
   st_vprintf (message, ap);
   va_end (ap);
   estr_write ("\n");
-  sys_exit (2);
+  exit_error (2);
 }
 iexport(runtime_error);
 
@@ -338,7 +381,7 @@ runtime_error_at (const char *where, const char *message, ...)
   st_vprintf (message, ap);
   va_end (ap);
   estr_write ("\n");
-  sys_exit (2);
+  exit_error (2);
 }
 iexport(runtime_error_at);
 
@@ -376,7 +419,7 @@ internal_error (st_parameter_common *cmp, const char *message)
      because hopefully it doesn't happen too often).  */
   stupid_function_name_for_static_linking();
 
 sys_exit (3);
exit_error (3);
 }
 
 
@@ -470,6 +513,10 @@ translate_error (int code)
       p = "Unformatted file structure has been corrupted";
       break;
 
+    case LIBERROR_INQUIRE_INTERNAL_UNIT:
+      p = "Inquire statement identifies an internal file";
+      break;
+
     default:
       p = "Unknown error code";
       break;
@@ -544,7 +591,7 @@ generate_error (st_parameter_common *cmp, int family, const char *message)
   estr_write ("Fortran runtime error: ");
   estr_write (message);
   estr_write ("\n");
-  sys_exit (2);
+  exit_error (2);
 }
 iexport(generate_error);
 
@@ -587,17 +634,17 @@ notification_std (int std)
    feature.  An error/warning will be issued if the currently selected
    standard does not contain the requested bits.  */
 
-try
+bool
 notify_std (st_parameter_common *cmp, int std, const char * message)
 {
   int warning;
 
   if (!compile_options.pedantic)
-    return SUCCESS;
+    return true;
 
   warning = compile_options.warn_std & std;
   if ((compile_options.allow_std & std) != 0 && !warning)
-    return SUCCESS;
+    return true;
 
   if (!warning)
     {
@@ -606,7 +653,7 @@ notify_std (st_parameter_common *cmp, int std, const char * message)
       estr_write ("Fortran runtime error: ");
       estr_write (message);
       estr_write ("\n");
-      sys_exit (2);
+      exit_error (2);
     }
   else
     {
@@ -615,5 +662,5 @@ notify_std (st_parameter_common *cmp, int std, const char * message)
       estr_write (message);
       estr_write ("\n");
     }
-  return FAILURE;
+  return false;
 }