Skip to content

Commit 9e5e8a2

Browse files
daxian-dbwadityapatwardhan
authored andcommitted
Don't throw exception when trying to resolve a possible link path (PowerShell#16310)
1 parent 66ea628 commit 9e5e8a2

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

src/System.Management.Automation/engine/NativeCommandProcessor.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -990,10 +990,19 @@ private static bool IsWindowsApplication(string fileName)
990990
// 1. When starting a process on Windows, if the 'FileName' is a symbolic link, the immediate link target will automatically be used,
991991
// but the OS does not do recursive resolution when the immediate link target is also a symbolic link.
992992
// 2. Keep the same behavior as before adopting the 'LinkTarget' and 'ResolveLinkTarget' APIs in .NET 6.
993-
string linkTarget = File.ResolveLinkTarget(fileName, returnFinalTarget: false)?.FullName;
994-
if (linkTarget is not null)
993+
try
994+
{
995+
string linkTarget = File.ResolveLinkTarget(fileName, returnFinalTarget: false)?.FullName;
996+
if (linkTarget is not null)
997+
{
998+
fileName = linkTarget;
999+
}
1000+
}
1001+
catch
9951002
{
996-
fileName = linkTarget;
1003+
// An exception may be thrown from 'File.ResolveLinkTarget' when it fails to resolve a link path,
1004+
// for example, when the underlying file system doesn't support reparse points.
1005+
// Just use the original file name in this case.
9971006
}
9981007

9991008
SHFILEINFO shinfo = new SHFILEINFO();

test/powershell/Language/Scripting/NativeExecution/NativeCommandProcessor.Tests.ps1

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,3 +252,48 @@ Categories=Application;
252252
{ $dllFile = "$PSHOME\System.Management.Automation.dll"; & $dllFile } | Should -Throw -ErrorId "NativeCommandFailed"
253253
}
254254
}
255+
256+
Describe "Run native command from a mounted FAT-format VHD" -tags @("Feature", "RequireAdminOnWindows") {
257+
BeforeAll {
258+
if (-not $IsWindows) {
259+
return;
260+
}
261+
262+
$vhdx = Join-Path -Path $TestDrive -ChildPath ncp.vhdx
263+
264+
if (Test-Path -Path $vhdx) {
265+
Remove-item -Path $vhdx -Force
266+
}
267+
268+
$create_vhdx = Join-Path -Path $TestDrive -ChildPath 'create_vhdx.txt'
269+
270+
Set-Content -Path $create_vhdx -Force -Value @"
271+
create vdisk file="$vhdx" maximum=20 type=fixed
272+
select vdisk file="$vhdx"
273+
attach vdisk
274+
convert mbr
275+
create partition primary
276+
format fs=fat
277+
assign letter="T"
278+
detach vdisk
279+
"@
280+
281+
diskpart.exe /s $create_vhdx
282+
Mount-DiskImage -ImagePath $vhdx > $null
283+
284+
Copy-Item "$env:WinDir\System32\whoami.exe" T:\whoami.exe
285+
}
286+
287+
AfterAll {
288+
if ($IsWindows) {
289+
Dismount-DiskImage -ImagePath $vhdx
290+
Remove-Item $vhdx, $create_vhdx -Force
291+
}
292+
}
293+
294+
It "Should run 'whoami.exe' from FAT file system without error" -Skip:(!$IsWindows) {
295+
$expected = & "$env:WinDir\System32\whoami.exe"
296+
$result = T:\whoami.exe
297+
$result | Should -BeExactly $expected
298+
}
299+
}

0 commit comments

Comments
 (0)