re PR libfortran/81984 (NULL string pointer dereferencing forces undefined behaviour...
[gcc.git] / libgfortran / intrinsics / execute_command_line.c
index 4e3c4451d62916e12fadb16b009626c9022f27d9..72d431ff25d62ce926e66d54d8935219c2e452d5 100644 (file)
@@ -1,6 +1,6 @@
 /* Implementation of the EXECUTE_COMMAND_LINE intrinsic.
-   Copyright (C) 2009 Free Software Foundation, Inc.
-   Contributed by FranÃ\83§ois-Xavier Coudert.
+   Copyright (C) 2009-2018 Free Software Foundation, Inc.
+   Contributed by François-Xavier Coudert.
 
 This file is part of the GNU Fortran runtime library (libgfortran).
 
@@ -25,11 +25,7 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 
 #include "libgfortran.h"
 #include <string.h>
-#include <stdbool.h>
 
-#ifdef HAVE_STDLIB_H
-#include <stdlib.h>
-#endif
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
@@ -38,9 +34,13 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 #endif
 
 
-enum { EXEC_NOERROR = 0, EXEC_SYSTEMFAILED };
+enum { EXEC_SYNCHRONOUS = -2, EXEC_NOERROR = 0, EXEC_SYSTEMFAILED,
+       EXEC_CHILDFAILED, EXEC_INVALIDCOMMAND };
 static const char *cmdmsg_values[] =
-  { "", "Execution of child process impossible" };
+  { "",
+    "Termination status of the command-language interpreter cannot be obtained",
+    "Execution of child process impossible",
+    "Invalid command line" };
 
 
 
@@ -49,8 +49,13 @@ set_cmdstat (int *cmdstat, int value)
 {
   if (cmdstat)
     *cmdstat = value;
-  else if (value != 0)
-    runtime_error ("Could not execute command line");
+  else if (value > EXEC_NOERROR)
+    {
+#define MSGLEN 200
+      char msg[MSGLEN] = "EXECUTE_COMMAND_LINE: ";
+      strncat (msg, cmdmsg_values[value], MSGLEN - strlen(msg) - 1);
+      runtime_error ("%s", msg);
+    }
 }
 
 
@@ -61,9 +66,7 @@ execute_command_line (const char *command, bool wait, int *exitstat,
                      gfc_charlen_type cmdmsg_len)
 {
   /* Transform the Fortran string to a C string.  */
-  char cmd[command_len + 1];
-  memcpy (cmd, command, command_len);
-  cmd[command_len] = '\0';
+  char *cmd = fc_strdup (command, command_len);
 
   /* Flush all I/O units before executing the command.  */
   flush_all_units();
@@ -74,10 +77,10 @@ execute_command_line (const char *command, bool wait, int *exitstat,
       /* Asynchronous execution.  */
       pid_t pid;
 
-      set_cmdstat (cmdstat, 0);
+      set_cmdstat (cmdstat, EXEC_NOERROR);
 
       if ((pid = fork()) < 0)
-       set_cmdstat (cmdstat, EXEC_SYSTEMFAILED);
+       set_cmdstat (cmdstat, EXEC_CHILDFAILED);
       else if (pid == 0)
        {
          /* Child process.  */
@@ -91,13 +94,26 @@ execute_command_line (const char *command, bool wait, int *exitstat,
       /* Synchronous execution.  */
       int res = system (cmd);
 
-      if (!wait)
-       set_cmdstat (cmdstat, -2);
-      else if (res == -1)
+      if (res == -1)
        set_cmdstat (cmdstat, EXEC_SYSTEMFAILED);
+#ifndef HAVE_FORK
+      else if (!wait)
+       set_cmdstat (cmdstat, EXEC_SYNCHRONOUS);
+#endif
+      else if (res == 127 || res == 126
+#if defined(WEXITSTATUS) && defined(WIFEXITED)
+              || (WIFEXITED(res) && WEXITSTATUS(res) == 127)
+              || (WIFEXITED(res) && WEXITSTATUS(res) == 126)
+#endif
+              )
+       /* Shell return codes 126 and 127 mean that the command line could
+          not be executed for various reasons.  */
+       set_cmdstat (cmdstat, EXEC_INVALIDCOMMAND);
       else
+       set_cmdstat (cmdstat, EXEC_NOERROR);
+
+      if (res != -1)
        {
-         set_cmdstat (cmdstat, 0);
 #if defined(WEXITSTATUS) && defined(WIFEXITED)
          *exitstat = WIFEXITED(res) ? WEXITSTATUS(res) : res;
 #else
@@ -106,16 +122,12 @@ execute_command_line (const char *command, bool wait, int *exitstat,
        }
     }
 
+  free (cmd);
+
   /* Now copy back to the Fortran string if needed.  */
-  if (cmdstat && *cmdstat > 0)
-    {
-      if (cmdmsg)
-       fstrcpy (cmdmsg, cmdmsg_len, cmdmsg_values[*cmdstat],
+  if (cmdstat && *cmdstat > EXEC_NOERROR && cmdmsg)
+    fstrcpy (cmdmsg, cmdmsg_len, cmdmsg_values[*cmdstat],
                strlen (cmdmsg_values[*cmdstat]));
-      else
-       runtime_error ("Failure in EXECUTE_COMMAND_LINE: %s",
-                      cmdmsg_values[*cmdstat]);
-    }
 }