1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-09 03:52:19 +02:00

Fixed UseShellExecute flag when spawning a process

This commit is contained in:
MineGame159 2023-02-20 22:34:14 +01:00
parent 0aedc37d42
commit 9d1bd60c9a
2 changed files with 91 additions and 56 deletions

2
.gitignore vendored
View file

@ -30,3 +30,5 @@ BeefPerf.txt
IDE/Tests/Rando IDE/Tests/Rando
install/ install/
JEMalloc/ JEMalloc/
.cache
.vscode

View file

@ -972,80 +972,113 @@ BFP_EXPORT BfpSpawn* BFP_CALLTYPE BfpSpawn_Create(const char* inTargetPath, cons
} }
} }
int32 i = 0; // When executing in a shell the arguments are not split
for ( ; true; i++) if ((flags & BfpSpawnFlag_UseShellExecute) == 0)
{ {
char c = args[i]; int32 i = 0;
if (c == '\0') for ( ; true; i++)
break;
if ((c == ' ') && (!inQuote))
{ {
if (firstCharIdx != -1) char c = args[i];
if (c == '\0')
break;
if ((c == ' ') && (!inQuote))
{ {
stringViews.Add(Beefy::StringView(args + firstCharIdx, i - firstCharIdx)); if (firstCharIdx != -1)
firstCharIdx = -1; {
stringViews.Add(Beefy::StringView(args + firstCharIdx, i - firstCharIdx));
firstCharIdx = -1;
}
} }
} else
else
{
if (firstCharIdx == -1)
firstCharIdx = i;
if (c == '"')
inQuote = !inQuote;
else if ((inQuote) && (c == '\\'))
{ {
c = args[i + 1]; if (firstCharIdx == -1)
firstCharIdx = i;
if (c == '"') 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<char*> argvArr; Beefy::Array<char*> argvArr;
if ((flags & BfpSpawnFlag_ArgsIncludesTarget) == 0) if ((flags & BfpSpawnFlag_UseShellExecute) == 0 && (flags & BfpSpawnFlag_ArgsIncludesTarget) == 0)
argvArr.Add(strdup(targetPath.c_str())); 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]; for (int32 i = 0; i < (int32)stringViews.size(); i++)
char* str = NULL;
for (int32 pass = 0; pass < 2; pass++)
{ {
char* strPtr = str; Beefy::StringView stringView = stringViews[i];
char* str = NULL;
int32 strPos = 0; for (int32 pass = 0; pass < 2; pass++)
for (int32 char8Idx = 0; char8Idx < stringView.mLength; char8Idx++)
{ {
char c = stringView.mPtr[char8Idx]; char* strPtr = str;
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); 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); argvArr.Add(NULL);
char** argv = NULL; char** argv = NULL;