Skip to content

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

Closed

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

Activity

  1. mklement0 commented on Jun 17, 2018

    @mklement0
    Contributor

    Good find.

    #5797 is essentially the same issue with respect to NoteProperty members (but without a crash) in that it also asks that such ETS properties be ignored during to-JSON serialization.

  2. added
    Issue-Discussion
    on Jun 19, 2018
  3. msftrncs commented on Mar 17, 2019

    @msftrncs
    Contributor

    Still happening in 6.2 RC 1, and per #6847, the object might be converted to a hash first, something must be going wrong during that pre-conversion before being sent to NewtonSoft?

  4. kganjam commented on Oct 28, 2020

    @kganjam

    Still happening in 7.0.3. This bug caused me to lose more than a week dealing with an internal support team as I thought the issue was due to something wrong in their code. In fact, their code was simple calling ConvertTo-Json and I happened to have had some extension methods for System.String.

  5. SteveL-MSFT commented on Nov 4, 2020

    @SteveL-MSFT
    Member

    Closing as dupe of #5797, carry on discussion in that one

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-DiscussionResolution-DuplicateWG-Cmdlets

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions