From c5270ab0c1bfe1f79bc57650da1091cd7358c689 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Sat, 12 Feb 2022 12:59:04 +0000 Subject: ash: workaround for UCRT bug There seems to be a bug in UCRT such that if a process has been started by passing a non-NULL environment block to CreateProcess() any subsequent call to spawnve() with a non-NULL environment pointer results in a crash. Commit 5b48ca53b (win32: pass NULL to spawnve, not environ) fixed the problem in busybox-w32 for those cases where a NULL environment pointer was sufficient. It didn't handle the case where the shell passes a modified environment to its child. All calls to spawnve() in the shell occur in a process which will terminate whether or not the call succeeds. It therefore doesn't matter if we mess with the environment of this process to allow spawnve() to be passed a NULL environment pointer. Do this for UCRT builds only. (GitHub issue #234) --- shell/ash.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/shell/ash.c b/shell/ash.c index 46c4f1675..c9122b291 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -8899,9 +8899,13 @@ tryexec(IF_FEATURE_SH_STANDALONE(int applet_no,) const char *cmd, char **argv, c # else if (APPLET_IS_NOEXEC(applet_no)) { # endif +#if ENABLE_PLATFORM_MINGW32 && !defined(_UCRT) + /* If building for UCRT move this up into shellexec() to + * work around a bug. */ clearenv(); while (*envp) putenv(*envp++); +#endif popredir(/*drop:*/ 1); run_noexec_applet_and_exit(applet_no, cmd, argv); } @@ -8971,6 +8975,14 @@ static void shellexec(char *prog, char **argv, const char *path, int idx) int applet_no = -1; /* used only by FEATURE_SH_STANDALONE */ envp = listvars(VEXPORT, VUNSET, /*strlist:*/ NULL, /*end:*/ NULL); +#if ENABLE_PLATFORM_MINGW32 && defined(_UCRT) + /* Avoid UCRT bug by updating parent's environment and passing a + * NULL environment pointer to execve(). */ + clearenv(); + while (*envp) + putenv(*envp++); + envp = NULL; +#endif #if !ENABLE_PLATFORM_MINGW32 if (strchr(prog, '/') != NULL #else -- cgit v1.2.3-55-g6feb