From b5fbe9e613ca20bd2dfaba3b0ed433e738a7daa0 Mon Sep 17 00:00:00 2001 From: Armaan Mcleod Date: Fri, 27 Dec 2024 15:09:32 +1100 Subject: [PATCH] Fix `Get-ItemProperty` to report non-terminating error for cast exception (#21115) --- .../namespaces/RegistryProvider.cs | 15 ++++++- .../Registry.Tests.ps1 | 41 +++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/src/System.Management.Automation/namespaces/RegistryProvider.cs b/src/System.Management.Automation/namespaces/RegistryProvider.cs index ecf36bec4c..db90116091 100644 --- a/src/System.Management.Automation/namespaces/RegistryProvider.cs +++ b/src/System.Management.Automation/namespaces/RegistryProvider.cs @@ -1825,8 +1825,19 @@ namespace Microsoft.PowerShell.Commands notePropertyName = LocalizedDefaultToken; } - propertyResults.Properties.Add(new PSNoteProperty(notePropertyName, key.GetValue(valueName))); - valueAdded = true; + try + { + propertyResults.Properties.Add(new PSNoteProperty(notePropertyName, key.GetValue(valueName))); + valueAdded = true; + } + catch (InvalidCastException invalidCast) + { + WriteError(new ErrorRecord( + invalidCast, + invalidCast.GetType().FullName, + ErrorCategory.ReadError, + path)); + } } key.Close(); diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Registry.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Registry.Tests.ps1 index 635e3f2940..06e2cf81ac 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Registry.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Registry.Tests.ps1 @@ -471,6 +471,47 @@ Describe "Extended Registry Provider Tests" -Tags @("Feature", "RequireAdminOnWi } } } + + Context "Validate Get-ItemProperty Cast Exception" { + BeforeAll { + if ($IsWindows) { + $registrySubkeyPath = 'HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\badreg' + + # Below will import .reg file with 64 bit integer in 32 bit DWORD + $badRegistryContent = @" +Windows Registry Editor Version 5.00 + +[$registrySubkeyPath] +"NoModify"=hex(4):01,00,00,00,00,00,00,00 +"@ + + $badRegistryPath = Join-Path -Path $TestDrive -ChildPath badreg.reg + $badRegistryContent | Set-Content -Path $badRegistryPath + reg.exe import $badRegistryPath + + $registryProviderSubkeyPath = 'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\badreg' + } + } + + It "Validate non-terminating error for cast" { + Get-ItemProperty -Path $registryProviderSubkeyPath -ErrorVariable err -ErrorAction SilentlyContinue + $err | Should -HaveCount 1 + $err[0].Exception | Should -BeOfType [System.InvalidCastException] + $err[0].TargetObject | Should -BeExactly $registrySubkeyPath + $err[0].CategoryInfo.Category | Should -BeExactly 'ReadError' + $err[0].FullyQualifiedErrorId | Should -BeExactly 'System.InvalidCastException,Microsoft.PowerShell.Commands.GetItemPropertyCommand' + } + + It "Validate terminating error for cast" { + { Get-ItemProperty -Path $registryProviderSubkeyPath -ErrorAction Stop } | Should -Throw -ErrorId 'System.InvalidCastException,Microsoft.PowerShell.Commands.GetItemPropertyCommand' + } + + AfterAll { + if ($IsWindows) { + reg.exe delete $registrySubkeyPath /f + } + } + } } } finally {