Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infinite loop for FileInfo and DirectoryInfo due to Root properties in DirectoryInfo. Should we restrict depth as a general fix in addition to specialcasing DirectoryInfo in Get-DisplayProperty2? #2474

Open
nohwnd opened this issue May 20, 2024 · 1 comment
Labels
Assertions For issues related with assertions
Milestone

Comments

@nohwnd
Copy link
Member

nohwnd commented May 20, 2024

Infinite loop for FileInfo and DirectoryInfo due to Root properties in DirectoryInfo. Should we restrict depth as a general fix in addition to specialcasing DirectoryInfo in Get-DisplayProperty2?

Originally posted by @fflaten in #2428 (comment)

@nohwnd nohwnd added the Assertions For issues related with assertions label May 20, 2024
@nohwnd nohwnd added this to the 6.0.0 milestone May 20, 2024
@nohwnd
Copy link
Member Author

nohwnd commented May 20, 2024

fflaten wrote:

Experimented a little with this. Example:

# root of Format2.ps1
$script:defaultDisplayPropertyCache = [System.Collections.Generic.Dictionary[type,string[]]]::new()

function Get-DisplayProperty2 ($Object) {
    $propertyMap = @{
        [System.Diagnostics.Process] = 'Id', 'Name'
    }

    $type = $Object.GetType()

    $properties = $propertyMap[$type]
    if ($null -ne $properties) { return $properties }

    # Fallback to DefaultDisplayPropertySet if defined for type
    if ($script:defaultDisplayPropertyCache.TryGetValue($type,[ref]$properties)) { return $properties }

    # Alterantive to enable inheritance: Loop $Object.PSTypeNames with (Get-TypeData $typename).DefaultDisplayPropertySet.ReferencedProperties
    # Would be slower and not sure if any types work this way
    if (($standardMembers = $Object.psobject.Members['PSStandardMembers']) -and
        ($defaultDisplayPropSet = $standardMembers.psobject.Members['DefaultDisplayPropertySet'])) {
        # Is this a live reference we need to break? CopyTo new stringarray?
        $properties = $defaultDisplayPropSet.ReferencedPropertyNames -as [string[]]
    }
    # Always cache to skip repeating PSStandardMembers check
    $script:defaultDisplayPropertyCache.Add($type, $properties)
    $properties
}

Thoughts:

  • Types like DirectoryInfo do not have standard member set, so needs to go in the $propertyMap to avoid infinite loop due to Root-property.
  • Is it a problem that Get-DisplayProperty2 could exclude properties that were different? Should be visible with -Verbose but still?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Assertions For issues related with assertions
Projects
None yet
Development

No branches or pull requests

1 participant