forked from OSchip/llvm-project
Avoid using alloca in Windows/Program.inc
One use needs to copy the alloca into a std::string, and the other use is before calling CreateProcess, which is very heavyweight anyway. llvm-svn: 187845
This commit is contained in:
parent
8552e22b07
commit
ac20e61b79
|
@ -12,6 +12,7 @@
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
#include "Windows.h"
|
#include "Windows.h"
|
||||||
|
#include "llvm/ADT/OwningPtr.h"
|
||||||
#include "llvm/Support/FileSystem.h"
|
#include "llvm/Support/FileSystem.h"
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
@ -46,10 +47,11 @@ std::string sys::FindProgramByName(const std::string &progName) {
|
||||||
|
|
||||||
// At this point, the file name is valid and does not contain slashes.
|
// At this point, the file name is valid and does not contain slashes.
|
||||||
// Let Windows search for it.
|
// Let Windows search for it.
|
||||||
char buffer[MAX_PATH];
|
std::string buffer;
|
||||||
|
buffer.resize(MAX_PATH);
|
||||||
char *dummy = NULL;
|
char *dummy = NULL;
|
||||||
DWORD len = SearchPath(NULL, progName.c_str(), ".exe", MAX_PATH,
|
DWORD len = SearchPath(NULL, progName.c_str(), ".exe", MAX_PATH,
|
||||||
buffer, &dummy);
|
&buffer[0], &dummy);
|
||||||
|
|
||||||
// See if it wasn't found.
|
// See if it wasn't found.
|
||||||
if (len == 0)
|
if (len == 0)
|
||||||
|
@ -57,19 +59,19 @@ std::string sys::FindProgramByName(const std::string &progName) {
|
||||||
|
|
||||||
// See if we got the entire path.
|
// See if we got the entire path.
|
||||||
if (len < MAX_PATH)
|
if (len < MAX_PATH)
|
||||||
return std::string(buffer);
|
return buffer;
|
||||||
|
|
||||||
// Buffer was too small; grow and retry.
|
// Buffer was too small; grow and retry.
|
||||||
while (true) {
|
while (true) {
|
||||||
char *b = reinterpret_cast<char *>(_alloca(len+1));
|
buffer.resize(len+1);
|
||||||
DWORD len2 = SearchPath(NULL, progName.c_str(), ".exe", len+1, b, &dummy);
|
DWORD len2 = SearchPath(NULL, progName.c_str(), ".exe", len+1, &buffer[0], &dummy);
|
||||||
|
|
||||||
// It is unlikely the search failed, but it's always possible some file
|
// It is unlikely the search failed, but it's always possible some file
|
||||||
// was added or removed since the last search, so be paranoid...
|
// was added or removed since the last search, so be paranoid...
|
||||||
if (len2 == 0)
|
if (len2 == 0)
|
||||||
return "";
|
return "";
|
||||||
else if (len2 <= len)
|
else if (len2 <= len)
|
||||||
return std::string(b);
|
return buffer;
|
||||||
|
|
||||||
len = len2;
|
len = len2;
|
||||||
}
|
}
|
||||||
|
@ -193,8 +195,8 @@ static bool Execute(void **Data,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now build the command line.
|
// Now build the command line.
|
||||||
char *command = reinterpret_cast<char *>(_alloca(len+1));
|
OwningArrayPtr<char> command(new char[len+1]);
|
||||||
char *p = command;
|
char *p = command.get();
|
||||||
|
|
||||||
for (unsigned i = 0; args[i]; i++) {
|
for (unsigned i = 0; args[i]; i++) {
|
||||||
const char *arg = args[i];
|
const char *arg = args[i];
|
||||||
|
@ -225,7 +227,7 @@ static bool Execute(void **Data,
|
||||||
*p = 0;
|
*p = 0;
|
||||||
|
|
||||||
// The pointer to the environment block for the new process.
|
// The pointer to the environment block for the new process.
|
||||||
char *envblock = 0;
|
OwningArrayPtr<char> envblock;
|
||||||
|
|
||||||
if (envp) {
|
if (envp) {
|
||||||
// An environment block consists of a null-terminated block of
|
// An environment block consists of a null-terminated block of
|
||||||
|
@ -238,8 +240,8 @@ static bool Execute(void **Data,
|
||||||
len += strlen(envp[i]) + 1;
|
len += strlen(envp[i]) + 1;
|
||||||
|
|
||||||
// Now build the environment block.
|
// Now build the environment block.
|
||||||
envblock = reinterpret_cast<char *>(_alloca(len+1));
|
envblock.reset(new char[len+1]);
|
||||||
p = envblock;
|
p = envblock.get();
|
||||||
|
|
||||||
for (unsigned i = 0; envp[i]; i++) {
|
for (unsigned i = 0; envp[i]; i++) {
|
||||||
const char *ev = envp[i];
|
const char *ev = envp[i];
|
||||||
|
@ -297,8 +299,8 @@ static bool Execute(void **Data,
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
fflush(stderr);
|
fflush(stderr);
|
||||||
std::string ProgramStr = Program;
|
std::string ProgramStr = Program;
|
||||||
BOOL rc = CreateProcess(ProgramStr.c_str(), command, NULL, NULL, TRUE, 0,
|
BOOL rc = CreateProcess(ProgramStr.c_str(), command.get(), NULL, NULL, TRUE,
|
||||||
envblock, NULL, &si, &pi);
|
0, envblock.get(), NULL, &si, &pi);
|
||||||
DWORD err = GetLastError();
|
DWORD err = GetLastError();
|
||||||
|
|
||||||
// Regardless of whether the process got created or not, we are done with
|
// Regardless of whether the process got created or not, we are done with
|
||||||
|
|
Loading…
Reference in New Issue