Skip to content

Commit

Permalink
Merge pull request #808 from tomer-amir/fix-hash-code-infinite-recursion
Browse files Browse the repository at this point in the history
Fix infinite recursion of the hash code function of YamlMappingNode

+semver:fix
  • Loading branch information
EdwardCooke authored May 29, 2024
2 parents 22070fd + 057a9c0 commit 53cfe3e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
16 changes: 10 additions & 6 deletions YamlDotNet.Test/RepresentationModel/YamlStreamTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,26 @@ public void LoadSimpleDocument()
Assert.Equal(YamlNodeType.Scalar, stream.Documents[0].RootNode.NodeType);
}

[Fact]
public void AccessingAllNodesOnInfinitelyRecursiveDocumentThrows()
[Theory]
[InlineData("&a [*a]")]
[InlineData("?\n key: &id1\n recursion: *id1\n: foo")]
public void AccessingAllNodesOnInfinitelyRecursiveDocumentThrows(string yaml)
{
var stream = new YamlStream();
stream.Load(Yaml.ParserForText("&a [*a]"));
stream.Load(Yaml.ParserForText(yaml));

var accessAllNodes = new Action(() => stream.Documents.Single().AllNodes.ToList());

accessAllNodes.ShouldThrow<MaximumRecursionLevelReachedException>("because the document is infinitely recursive.");
}

[Fact]
public void InfinitelyRecursiveNodeToStringSucceeds()
[Theory]
[InlineData("&a [*a]")]
[InlineData("?\n key: &id1\n recursion: *id1\n: foo")]
public void InfinitelyRecursiveNodeToStringSucceeds(string yaml)
{
var stream = new YamlStream();
stream.Load(Yaml.ParserForText("&a [*a]"));
stream.Load(Yaml.ParserForText(yaml));

var toString = stream.Documents.Single().RootNode.ToString();

Expand Down
4 changes: 3 additions & 1 deletion YamlDotNet/RepresentationModel/YamlMappingNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,9 @@ public override int GetHashCode()
foreach (var entry in children)
{
hashCode = CombineHashCodes(hashCode, entry.Key);
hashCode = CombineHashCodes(hashCode, entry.Value);
hashCode = entry.Value.Anchor.IsEmpty
? CombineHashCodes(hashCode, entry.Value)
: CombineHashCodes(hashCode, entry.Value.Anchor);
}
return hashCode;
}
Expand Down

0 comments on commit 53cfe3e

Please sign in to comment.