diff options
author | Reid Kleckner <reid@kleckner.net> | 2013-08-07 01:21:33 +0000 |
---|---|---|
committer | Reid Kleckner <reid@kleckner.net> | 2013-08-07 01:21:33 +0000 |
commit | e3c7bdf9a955dac6536588f8ee9c25716f479a04 (patch) | |
tree | c4518df853fecf2ccf898d4aec17e1b81b7ad45b /lib/Support/Windows/Program.inc | |
parent | b7669139e6309679acbe16ca26f1b1a2fdd91268 (diff) | |
download | external_llvm-e3c7bdf9a955dac6536588f8ee9c25716f479a04.zip external_llvm-e3c7bdf9a955dac6536588f8ee9c25716f479a04.tar.gz external_llvm-e3c7bdf9a955dac6536588f8ee9c25716f479a04.tar.bz2 |
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.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@187845 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Support/Windows/Program.inc')
-rw-r--r-- | lib/Support/Windows/Program.inc | 28 |
1 files changed, 15 insertions, 13 deletions
diff --git a/lib/Support/Windows/Program.inc b/lib/Support/Windows/Program.inc index a368407..8165ef4 100644 --- a/lib/Support/Windows/Program.inc +++ b/lib/Support/Windows/Program.inc @@ -12,6 +12,7 @@ //===----------------------------------------------------------------------===// #include "Windows.h" +#include "llvm/ADT/OwningPtr.h" #include "llvm/Support/FileSystem.h" #include <cstdio> #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. // Let Windows search for it. - char buffer[MAX_PATH]; + std::string buffer; + buffer.resize(MAX_PATH); char *dummy = NULL; DWORD len = SearchPath(NULL, progName.c_str(), ".exe", MAX_PATH, - buffer, &dummy); + &buffer[0], &dummy); // See if it wasn't found. if (len == 0) @@ -57,19 +59,19 @@ std::string sys::FindProgramByName(const std::string &progName) { // See if we got the entire path. if (len < MAX_PATH) - return std::string(buffer); + return buffer; // Buffer was too small; grow and retry. while (true) { - char *b = reinterpret_cast<char *>(_alloca(len+1)); - DWORD len2 = SearchPath(NULL, progName.c_str(), ".exe", len+1, b, &dummy); + buffer.resize(len+1); + 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 // was added or removed since the last search, so be paranoid... if (len2 == 0) return ""; else if (len2 <= len) - return std::string(b); + return buffer; len = len2; } @@ -193,8 +195,8 @@ static bool Execute(void **Data, } // Now build the command line. - char *command = reinterpret_cast<char *>(_alloca(len+1)); - char *p = command; + OwningArrayPtr<char> command(new char[len+1]); + char *p = command.get(); for (unsigned i = 0; args[i]; i++) { const char *arg = args[i]; @@ -225,7 +227,7 @@ static bool Execute(void **Data, *p = 0; // The pointer to the environment block for the new process. - char *envblock = 0; + OwningArrayPtr<char> envblock; if (envp) { // An environment block consists of a null-terminated block of @@ -238,8 +240,8 @@ static bool Execute(void **Data, len += strlen(envp[i]) + 1; // Now build the environment block. - envblock = reinterpret_cast<char *>(_alloca(len+1)); - p = envblock; + envblock.reset(new char[len+1]); + p = envblock.get(); for (unsigned i = 0; envp[i]; i++) { const char *ev = envp[i]; @@ -297,8 +299,8 @@ static bool Execute(void **Data, fflush(stdout); fflush(stderr); std::string ProgramStr = Program; - BOOL rc = CreateProcess(ProgramStr.c_str(), command, NULL, NULL, TRUE, 0, - envblock, NULL, &si, &pi); + BOOL rc = CreateProcess(ProgramStr.c_str(), command.get(), NULL, NULL, TRUE, + 0, envblock.get(), NULL, &si, &pi); DWORD err = GetLastError(); // Regardless of whether the process got created or not, we are done with |