Skip to content

ConvertTo-Xml doesn't limit the depth for (recursive) HashTables (Crash) #7290

@iRon7

Description

@iRon7

Steps to reproduce

Insert the following commands

$a = @{Key = "Test"}
$a.Parent = $a
$a | ConvertTo-Xml -As String

Expected behavior

I would expect an XML string with the output limited to the default depth of 3:

<Property Name="Key" Type="System.String">Key</Property>
<Property Name="Value" Type="System.String">Test</Property>
<Property Name="Key" Type="System.String">Parent</Property>
<Property Name="Value" Type="System.Collections.Hashtable">
  <Property Name="Key" Type="System.String">Key</Property>
  <Property Name="Value" Type="System.String">Test</Property>
  <Property Name="Key" Type="System.String">Parent</Property>
  <Property Name="Value" Type="System.Collections.Hashtable">
    <Property Name="Key" Type="System.String">Key</Property>
    <Property Name="Value" Type="System.String">Test</Property>
    <Property Name="Key" Type="System.String">Parent</Property>
    <Property Name="Value" Type="System.String">end</Property>
  </Property>
</Property>

Actual behavior

Instead, it first shows:

Process is terminating due to StackOverflowException.

And a few seconds later the whole shell is terminated.

  • Note: the same issue appears when the depth is explicitly given (e.g. -Depth 3)

PowerShell Windows
A similar issue exist in PowerShell Windows (5.1) which reports:

ConvertTo-Xml : Stream was too long.
At line:1 char:6

  • $a | ConvertTo-Xml
  •  ~~~~~~~~~~~~~
    
    • CategoryInfo : NotSpecified: (:) [ConvertTo-Xml], IOException
    • FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.ConvertToXmlCommand

But does not crash.

Environment data

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

ConvertTo-Xml version: 3.1.0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Breaking-Changebreaking change that may affect usersCommittee-ReviewedPS-Committee has reviewed this and made a decisionIssue-BugIssue has been identified as a bug in the productKeepOpenThe bot will ignore these and not auto-closeWG-Cmdlets-Utilitycmdlets in the Microsoft.PowerShell.Utility module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions