changeset 30257:e1b0ed07895b

Don't use P_OVERLAY to spawn main process from wrapper executable on Windows (bug #61396). * liboctave/wrappers/unistd-wrappers.c (octave_execv_wrapper): Spawning a process with P_OVERLAY returns the control to the terminal on Windows. Instead wait for the child process to finish in the wrapper executable. * src/main.in.cc: Only print error message for status = -1 on Windows.
author Markus Mützel <markus.muetzel@gmx.de>
date Thu, 28 Oct 2021 20:03:37 +0200
parents cd8d4a95815a
children b3beb8273630
files liboctave/wrappers/unistd-wrappers.c src/main.in.cc
diffstat 2 files changed, 22 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/wrappers/unistd-wrappers.c	Thu Oct 28 19:38:19 2021 +0200
+++ b/liboctave/wrappers/unistd-wrappers.c	Thu Oct 28 20:03:37 2021 +0200
@@ -124,7 +124,8 @@
 #if defined (__WIN32__) && ! defined (__CYGWIN__)
 
   char *argv_mem_to_free;
-  char **sanitized_argv = prepare_spawn (argv, &argv_mem_to_free);
+  const char **sanitized_argv = prepare_spawn ((const char * const *) argv,
+                                               &argv_mem_to_free);
 
 #  if defined (OCTAVE_USE_WINDOWS_API) && defined (_UNICODE)
 
@@ -145,9 +146,23 @@
   free (sanitized_argv);
   free (argv_mem_to_free);
 
-  int status = _wspawnv (P_OVERLAY, wfile, wargv);
+  int status = _wspawnv (P_WAIT, wfile, wargv+1);
+
+#    if 0
+  // Code snippet from gnulib execute.c
 
-  // This only happens if _wspawnv fails.
+  // Executing arbitrary files as shell scripts is unsecure.
+  if (status == -1 && errno == ENOEXEC)
+    {
+      // prog is not a native executable.  Try to execute it as a
+      // shell script.  Note that prepare_spawn() has already prepended
+      // a hidden element "sh.exe" to argv.
+      argv[1] = prog_path;
+      status = _wspawnv (P_WAIT, wargv[0], wargv);
+    }
+#    endif
+
+  // This happens when the spawned child process terminates.
 
   free (wfile);
   const wchar_t **wp = wargv;
--- a/src/main.in.cc	Thu Oct 28 19:38:19 2021 +0200
+++ b/src/main.in.cc	Thu Oct 28 20:03:37 2021 +0200
@@ -201,6 +201,10 @@
 {
   int status = octave_execv_wrapper (file.c_str (), argv);
 
+#if defined (OCTAVE_USE_WINDOWS_API)
+  // The above wrapper uses spawn(P_WAIT,...) instead of exec on Windows.
+  if (status == -1)
+#endif
   std::cerr << argv[0] << ": failed to exec '" << file << "'" << std::endl;
 
   return status;