# HG changeset patch # User Markus Mützel # Date 1635444217 -7200 # Node ID e1b0ed07895b4314f747c8c4092c5ff08db98725 # Parent cd8d4a95815a5bcb108a21e621664a34926f07a5 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. diff -r cd8d4a95815a -r e1b0ed07895b liboctave/wrappers/unistd-wrappers.c --- 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; diff -r cd8d4a95815a -r e1b0ed07895b src/main.in.cc --- 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;