diff options
author | Laszlo Csomor <laszlocsomor@google.com> | 2017-03-06 13:40:41 +0000 |
---|---|---|
committer | Vladimir Moskva <vladmos@google.com> | 2017-03-06 14:45:23 +0000 |
commit | 373879d81e45a36c24322d725604e051f172d7b4 (patch) | |
tree | 201728882b4c9f28377d37e3186658a57de2322b /src/main/native | |
parent | 79dd9351b11aef0433ffb5b3afad6262379cef21 (diff) |
Windows, JNI: shorten the cwd for CreateProcess
The hard limit for SetCurrentDirectory{A,W} is
MAX_PATH-1, even with UNC prefix, therefore a
process' cwd may also not be longer than that.
See https://github.com/bazelbuild/bazel/issues/2107
See https://github.com/bazelbuild/bazel/issues/2406
See https://github.com/bazelbuild/bazel/issues/2181
--
PiperOrigin-RevId: 149290147
MOS_MIGRATED_REVID=149290147
Diffstat (limited to 'src/main/native')
-rw-r--r-- | src/main/native/windows_processes.cc | 10 | ||||
-rw-r--r-- | src/main/native/windows_util.cc | 25 | ||||
-rw-r--r-- | src/main/native/windows_util.h | 4 |
3 files changed, 30 insertions, 9 deletions
diff --git a/src/main/native/windows_processes.cc b/src/main/native/windows_processes.cc index a014a8cdaa..3165b382cb 100644 --- a/src/main/native/windows_processes.cc +++ b/src/main/native/windows_processes.cc @@ -184,7 +184,15 @@ Java_com_google_devtools_build_lib_windows_WindowsProcesses_nativeCreateProcess( AddUncPrefixMaybe(GetJavaWstring(env, java_stdout_redirect)); std::wstring stderr_redirect = AddUncPrefixMaybe(GetJavaWstring(env, java_stderr_redirect)); - std::string cwd = GetJavaUTFString(env, java_cwd); + std::string cwd; + error_msg = windows_util::AsShortPath( + GetJavaUTFString(env, java_cwd), + [env, java_cwd]() { return GetJavaWstring(env, java_cwd); }, + &cwd); + if (!error_msg.empty()) { + result->error_ = error_msg; + return PtrAsJlong(result); + } std::unique_ptr<char[]> mutable_commandline(new char[commandline.size() + 1]); strncpy(mutable_commandline.get(), commandline.c_str(), diff --git a/src/main/native/windows_util.cc b/src/main/native/windows_util.cc index 4223d338ac..cc0d02954f 100644 --- a/src/main/native/windows_util.cc +++ b/src/main/native/windows_util.cc @@ -64,9 +64,8 @@ static void QuotePath(const string& path, string* result) { *result = string("\"") + path + "\""; } -string AsExecutablePathForCreateProcess(const string& path, - function<wstring()> path_as_wstring, - string* result) { +string AsShortPath(const string& path, function<wstring()> path_as_wstring, + string* result) { if (path.empty()) { return string("argv[0] should not be empty"); } @@ -93,10 +92,7 @@ string AsExecutablePathForCreateProcess(const string& path, // Fast-track: the path is already short. if (path.size() < MAX_PATH) { - // Quote the path in case it's something like "c:\foo\app name.exe". - // Do this unconditionally, there's no harm in quoting. Quotes are not - // allowed inside paths so we don't need to escape quotes. - QuotePath(path, result); + *result = path; return ""; } // At this point we know that the path is at least MAX_PATH long and that it's @@ -139,8 +135,21 @@ string AsExecutablePathForCreateProcess(const string& path, } mbs_short[mbs_size] = 0; - QuotePath(mbs_short, result); + *result = mbs_short; return ""; } +string AsExecutablePathForCreateProcess(const string& path, + function<wstring()> path_as_wstring, + string* result) { + string error = AsShortPath(path, path_as_wstring, result); + if (error.empty()) { + // Quote the path in case it's something like "c:\foo\app name.exe". + // Do this unconditionally, there's no harm in quoting. Quotes are not + // allowed inside paths so we don't need to escape quotes. + QuotePath(*result, result); + } + return error; +} + } // namespace windows_util diff --git a/src/main/native/windows_util.h b/src/main/native/windows_util.h index a79d9dde64..ea632e7a7b 100644 --- a/src/main/native/windows_util.h +++ b/src/main/native/windows_util.h @@ -48,6 +48,10 @@ struct AutoHandle { string GetLastErrorString(const string& cause); +// Same as `AsExecutablePathForCreateProcess` except it won't quote the result. +string AsShortPath(const string& path, function<wstring()> path_as_wstring, + string* result); + // Computes a path suitable as the executable part in CreateProcessA's cmdline. // // The null-terminated executable path for CreateProcessA has to fit into |