Skip to content

Commit

Permalink
Don't inherit the same handle more than once. (#1234)
Browse files Browse the repository at this point in the history
  • Loading branch information
BillyONeal authored Oct 18, 2023
1 parent f40f40c commit bad6195
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
26 changes: 26 additions & 0 deletions azure-pipelines/end-to-end-tests-dir/commands.extract.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
. $PSScriptRoot/../end-to-end-tests-prelude.ps1

Refresh-TestRoot
$out = Join-Path $TestingRoot "a-tar-with-execute"
Run-Vcpkg z-extract "$PSScriptRoot/../e2e-assets/extract/a-tar-with-plus-x.tar.gz" $out
Throw-IfFailed
Expand All @@ -15,3 +16,28 @@ if (-Not $IsWindows) {
throw "File does not have +x permission. UnixMode: $unixMode"
}
}

# Regression test for https://github.com/microsoft/vcpkg/issues/33904 / https://github.com/microsoft/vcpkg-tool/pull/1234
if ($IsWindows) {
Refresh-TestRoot
$gitCommand = Get-Command git
$gitDirectory = (Get-Item $gitCommand.Source).Directory
$bash = "$gitDirectory/bash.exe"
if (-Not (Test-Path $bash)) {
$gitInstallation = $gitDirectory.Parent
$bash = "$gitInstallation/bin/bash.exe"
if (-Not (Test-Path $bash)) {
throw 'git bash not found'
}
}

$out = Join-Path $TestingRoot "a-tar-with-execute"
[string]$vcpkgExeForwardSlashes = $vcpkgExe.Replace("\", "/")
[string]$tarballForwardSlashes = "$PSScriptRoot/../e2e-assets/extract/a-tar-with-plus-x.tar.gz".Replace("\", "/")
[string]$outForwardSlashes = $out.Replace("\", "/")
& $bash -c "`"$vcpkgExeForwardSlashes`" z-extract `"$tarballForwardSlashes`" `"$outForwardSlashes`""
$extractedFilePath = Join-Path $out "myExe"
if (-Not (Test-Path $extractedFilePath)) {
throw "Extraction Failed"
}
}
9 changes: 7 additions & 2 deletions src/vcpkg/base/system.process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1380,10 +1380,15 @@ namespace vcpkg
return std::move(proc_attribute_list_create).error();
}

HANDLE handles_to_inherit[] = {
constexpr size_t number_of_candidate_handles = 3;
HANDLE handles_to_inherit[number_of_candidate_handles] = {
GetStdHandle(STD_INPUT_HANDLE), GetStdHandle(STD_OUTPUT_HANDLE), GetStdHandle(STD_ERROR_HANDLE)};
Util::sort(handles_to_inherit);
size_t number_of_handles =
std::unique(handles_to_inherit, handles_to_inherit + number_of_candidate_handles) - handles_to_inherit;

auto maybe_error = proc_attribute_list.update_attribute(
PROC_THREAD_ATTRIBUTE_HANDLE_LIST, handles_to_inherit, 3 * sizeof(HANDLE));
PROC_THREAD_ATTRIBUTE_HANDLE_LIST, handles_to_inherit, number_of_handles * sizeof(HANDLE));
if (!maybe_error.has_value())
{
return maybe_error.error();
Expand Down
16 changes: 16 additions & 0 deletions src/vcpkg/tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,22 @@ namespace vcpkg

ExpectedL<Path> find_system_tar(const ReadOnlyFilesystem& fs)
{
#if defined(_WIN32)
const auto& maybe_system32 = get_system32();
if (auto system32 = maybe_system32.get())
{
auto shipped_with_windows = *system32 / "tar.exe";
if (fs.is_regular_file(shipped_with_windows))
{
return shipped_with_windows;
}
}
else
{
return maybe_system32.error();
}
#endif // ^^^ _WIN32

const auto tools = fs.find_from_PATH(Tools::TAR);
if (tools.empty())
{
Expand Down

0 comments on commit bad6195

Please sign in to comment.