From 9d1bd60c9a2cb766808d36b4412fda8a3b4100b5 Mon Sep 17 00:00:00 2001 From: MineGame159 Date: Mon, 20 Feb 2023 22:34:14 +0100 Subject: [PATCH] Fixed UseShellExecute flag when spawning a process --- .gitignore | 4 +- BeefySysLib/platform/posix/PosixCommon.cpp | 143 +++++++++++++-------- 2 files changed, 91 insertions(+), 56 deletions(-) diff --git a/.gitignore b/.gitignore index e4334d83..bb479f2f 100644 --- a/.gitignore +++ b/.gitignore @@ -29,4 +29,6 @@ wasm/* BeefPerf.txt IDE/Tests/Rando install/ -JEMalloc/ \ No newline at end of file +JEMalloc/ +.cache +.vscode \ No newline at end of file diff --git a/BeefySysLib/platform/posix/PosixCommon.cpp b/BeefySysLib/platform/posix/PosixCommon.cpp index 89641873..4f5de917 100644 --- a/BeefySysLib/platform/posix/PosixCommon.cpp +++ b/BeefySysLib/platform/posix/PosixCommon.cpp @@ -972,80 +972,113 @@ BFP_EXPORT BfpSpawn* BFP_CALLTYPE BfpSpawn_Create(const char* inTargetPath, cons } } - int32 i = 0; - for ( ; true; i++) + // When executing in a shell the arguments are not split + if ((flags & BfpSpawnFlag_UseShellExecute) == 0) { - char c = args[i]; - if (c == '\0') - break; - if ((c == ' ') && (!inQuote)) + int32 i = 0; + for ( ; true; i++) { - if (firstCharIdx != -1) + char c = args[i]; + if (c == '\0') + break; + if ((c == ' ') && (!inQuote)) { - stringViews.Add(Beefy::StringView(args + firstCharIdx, i - firstCharIdx)); - firstCharIdx = -1; + if (firstCharIdx != -1) + { + stringViews.Add(Beefy::StringView(args + firstCharIdx, i - firstCharIdx)); + firstCharIdx = -1; + } } - } - else - { - if (firstCharIdx == -1) - firstCharIdx = i; - if (c == '"') - inQuote = !inQuote; - else if ((inQuote) && (c == '\\')) + else { - c = args[i + 1]; + if (firstCharIdx == -1) + firstCharIdx = i; if (c == '"') - i++; + inQuote = !inQuote; + else if ((inQuote) && (c == '\\')) + { + c = args[i + 1]; + if (c == '"') + i++; + } } } + if (firstCharIdx != -1) + stringViews.Add(Beefy::StringView(args + firstCharIdx, i - firstCharIdx)); } - if (firstCharIdx != -1) - stringViews.Add(Beefy::StringView(args + firstCharIdx, i - firstCharIdx)); Beefy::Array argvArr; - if ((flags & BfpSpawnFlag_ArgsIncludesTarget) == 0) + if ((flags & BfpSpawnFlag_UseShellExecute) == 0 && (flags & BfpSpawnFlag_ArgsIncludesTarget) == 0) argvArr.Add(strdup(targetPath.c_str())); - for (int32 i = 0; i < (int32)stringViews.size(); i++) + // When executing in a shell the arguments are not split nor are the quotes removed + if ((flags & BfpSpawnFlag_UseShellExecute) == 0) { - Beefy::StringView stringView = stringViews[i]; - char* str = NULL; - for (int32 pass = 0; pass < 2; pass++) + for (int32 i = 0; i < (int32)stringViews.size(); i++) { - char* strPtr = str; - - int32 strPos = 0; - for (int32 char8Idx = 0; char8Idx < stringView.mLength; char8Idx++) + Beefy::StringView stringView = stringViews[i]; + char* str = NULL; + for (int32 pass = 0; pass < 2; pass++) { - char c = stringView.mPtr[char8Idx]; - if (c == '"') - inQuote = !inQuote; - else - { - if ((inQuote) && (c == '\\') && (char8Idx < stringView.mLength - 1)) - { - char nextC = stringView.mPtr[char8Idx + 1]; - if (nextC == '"') - { - c = nextC; - char8Idx++; - } - } - if (strPtr != NULL) - *(strPtr++) = c; - strPos++; - } - } - if (pass == 0) - str = (char*)malloc(strPos + 1); - else - *(strPtr++) = 0; - } + char* strPtr = str; - argvArr.Add(str); + int32 strPos = 0; + for (int32 char8Idx = 0; char8Idx < stringView.mLength; char8Idx++) + { + char c = stringView.mPtr[char8Idx]; + if (c == '"') + inQuote = !inQuote; + else + { + if ((inQuote) && (c == '\\') && (char8Idx < stringView.mLength - 1)) + { + char nextC = stringView.mPtr[char8Idx + 1]; + if (nextC == '"') + { + c = nextC; + char8Idx++; + } + } + if (strPtr != NULL) + *(strPtr++) = c; + strPos++; + } + } + if (pass == 0) + str = (char*)malloc(strPos + 1); + else + *(strPtr++) = 0; + } + + argvArr.Add(str); + } } + + // Handle shell execution + if ((flags & BfpSpawnFlag_UseShellExecute) != 0) + { + // Create command string + int argsLength = strlen(args); + int length = targetPath.mLength + 1 + argsLength; + + char* commandStr = (char*)malloc(length + 1); + + memcpy(commandStr, targetPath.c_str(), targetPath.mLength); + commandStr[targetPath.mLength] = ' '; + memcpy(&commandStr[targetPath.mLength + 1], args, argsLength); + commandStr[length] = '\0'; + + // Replace target path + targetPath.Clear(); + targetPath.Append("/bin/sh"); + + // Add arguments + argvArr.Add(strdup("sh")); + argvArr.Add(strdup("-c")); + argvArr.Add(commandStr); + } + argvArr.Add(NULL); char** argv = NULL;