Properly report errors when CreateProcess() fails

fixes #7599
This commit is contained in:
Gunnar Beutner 2014-11-07 09:53:23 +01:00
parent 60114d4b5d
commit 3cf08ad050
2 changed files with 17 additions and 26 deletions

View File

@ -24,6 +24,7 @@
#include "base/string.hpp" #include "base/string.hpp"
#include "base/stacktrace.hpp" #include "base/stacktrace.hpp"
#include "base/context.hpp" #include "base/context.hpp"
#include "base/utility.hpp"
#include <sstream> #include <sstream>
#include <boost/exception/errinfo_api_function.hpp> #include <boost/exception/errinfo_api_function.hpp>
#include <boost/exception/errinfo_errno.hpp> #include <boost/exception/errinfo_errno.hpp>
@ -97,26 +98,7 @@ typedef boost::error_info<struct errinfo_win32_error_, int> errinfo_win32_error;
inline std::string to_string(const errinfo_win32_error& e) inline std::string to_string(const errinfo_win32_error& e)
{ {
std::ostringstream tmp; return Utility::FormatErrorNumber(e.value());
int code = e.value();
char *message;
String result = "Unknown error.";
DWORD rc = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM, NULL, code, 0, (char *)&message,
0, NULL);
if (rc != 0) {
result = String(message);
LocalFree(message);
/* remove trailing new-line characters */
boost::algorithm::trim_right(result);
}
tmp << code << ", \"" << result << "\"";
return tmp.str();
} }
#endif /* _WIN32 */ #endif /* _WIN32 */

View File

@ -413,21 +413,30 @@ void Process::Run(const boost::function<void(const ProcessResult&)>& callback)
if (!CreateProcess(NULL, args, NULL, NULL, TRUE, if (!CreateProcess(NULL, args, NULL, NULL, TRUE,
EXTENDED_STARTUPINFO_PRESENT, envp, NULL, &si.StartupInfo, &pi)) { EXTENDED_STARTUPINFO_PRESENT, envp, NULL, &si.StartupInfo, &pi)) {
DWORD error = GetLastError();
CloseHandle(outWritePipe); CloseHandle(outWritePipe);
CloseHandle(outWritePipeDup); CloseHandle(outWritePipeDup);
delete args;
free(envp); free(envp);
DeleteProcThreadAttributeList(lpAttributeList); DeleteProcThreadAttributeList(lpAttributeList);
delete [] reinterpret_cast<char *>(lpAttributeList); delete [] reinterpret_cast<char *>(lpAttributeList);
BOOST_THROW_EXCEPTION(win32_error()
<< boost::errinfo_api_function("CreateProcess") m_Result.PID = 0;
<< errinfo_win32_error(GetLastError())); m_Result.ExecutionEnd = Utility::GetTime();
m_Result.ExitStatus = 127;
m_Result.Output = "Command " + String(args) + " failed to execute: " + Utility::FormatErrorNumber(error);
delete [] args;
if (callback)
Utility::QueueAsyncCallback(boost::bind(callback, m_Result));
return;
} }
delete args; delete [] args;
free(envp); free(envp);
DeleteProcThreadAttributeList(lpAttributeList); DeleteProcThreadAttributeList(lpAttributeList);
delete[] reinterpret_cast<char *>(lpAttributeList); delete [] reinterpret_cast<char *>(lpAttributeList);
CloseHandle(outWritePipe); CloseHandle(outWritePipe);
CloseHandle(outWritePipeDup); CloseHandle(outWritePipeDup);