Skip to content

ScriptProperty extensions on String via Update-TypeData break ConvertTo-Json #7091

@rmbolger

Description

@rmbolger

Steps to reproduce

# add a ScriptProperty to System.String
Update-TypeData -TypeName System.String -MemberType ScriptProperty -MemberName Foo -Value { $this.ToUpper() }

# call ConvertTo-Json with an object that contains at least one string
'asdf' | ConvertTo-Json

Expected behavior

"asdf"

Actual behavior

Process is terminating due to StackOverflowException.
Aborted (core dumped)

In this simple example where the script value is $this.ToUpper(), crashing with a StackOverflowException happens reliably and relatively quickly. If the script value is changed to something more complex, the PowerShell process tends to hang and consume massive amounts of memory until it crashes or is terminated.

Environment data

This seems to affect both Windows PowerShell and PowerShell Core 6.0.2 regardless of OS platform. The tests I did were from Windows 10 1803 running Windows PowerShell 5.1 and PowerShell Core 6.0.2 both on Windows and Ubuntu 16.04 via WSL. I've included the $PSVersionTable output for all below.

# Win PowerShell 5.1
Name                           Value
----                           -----
PSVersion                      5.1.17134.112
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.17134.112
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

# PowerShell Core 6.0.2 (Windows)
Name                           Value
----                           -----
PSVersion                      6.0.2
PSEdition                      Core
GitCommitId                    v6.0.2
OS                             Microsoft Windows 10.0.17134
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

# PowerShell Core 6.0.2 (Ubuntu 16.04)
Name                           Value
----                           -----
PSVersion                      6.0.2
PSEdition                      Core
GitCommitId                    v6.0.2
OS                             Linux 4.4.0-17134-Microsoft #112-Microsoft Thu Jun 07 22:57:00 PST 2018
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-DuplicateThe issue is a duplicate.WG-Cmdlets-Utilitycmdlets in the Microsoft.PowerShell.Utility module

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions