re PR libfortran/81984 (NULL string pointer dereferencing forces undefined behaviour...
[gcc.git] / libgfortran / intrinsics / execute_command_line.c
index fd9d4835cb28a0fafc2f2121400f803b2754975b..72d431ff25d62ce926e66d54d8935219c2e452d5 100644 (file)
@@ -1,6 +1,6 @@
 /* Implementation of the EXECUTE_COMMAND_LINE intrinsic.
-   Copyright (C) 2009-2013 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,7 +25,6 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 
 #include "libgfortran.h"
 #include <string.h>
-#include <stdlib.h>
 
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
@@ -36,11 +35,12 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 
 
 enum { EXEC_SYNCHRONOUS = -2, EXEC_NOERROR = 0, EXEC_SYSTEMFAILED,
-       EXEC_CHILDFAILED };
+       EXEC_CHILDFAILED, EXEC_INVALIDCOMMAND };
 static const char *cmdmsg_values[] =
   { "",
     "Termination status of the command-language interpreter cannot be obtained",
-    "Execution of child process impossible" };
+    "Execution of child process impossible",
+    "Invalid command line" };
 
 
 
@@ -50,7 +50,12 @@ set_cmdstat (int *cmdstat, int value)
   if (cmdstat)
     *cmdstat = value;
   else if (value > EXEC_NOERROR)
-    runtime_error ("Could not execute command line");
+    {
+#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();
@@ -97,6 +100,15 @@ execute_command_line (const char *command, bool wait, int *exitstat,
       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);
 
@@ -110,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 > EXEC_NOERROR)
-    {
-      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]);
-    }
 }