Skip to content

Commit ac70bfd

Browse files
author
Treicy Sanchez Gutierrez (from Dev Box)
committed
uint instead
1 parent a1869ef commit ac70bfd

3 files changed

Lines changed: 30 additions & 34 deletions

File tree

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#nullable enable
2-
const Microsoft.OpenApi.YamlReader.YamlConverter.DefaultMaxDepth = 64 -> int
3-
const Microsoft.OpenApi.YamlReader.YamlConverter.DefaultMaxNodeCount = 5000000 -> int
4-
static Microsoft.OpenApi.YamlReader.YamlConverter.MaxDepth.get -> int
2+
const Microsoft.OpenApi.YamlReader.YamlConverter.DefaultMaxDepth = 64 -> uint
3+
const Microsoft.OpenApi.YamlReader.YamlConverter.DefaultMaxNodeCount = 5000000 -> uint
4+
static Microsoft.OpenApi.YamlReader.YamlConverter.MaxDepth.get -> uint
55
static Microsoft.OpenApi.YamlReader.YamlConverter.MaxDepth.set -> void
6-
static Microsoft.OpenApi.YamlReader.YamlConverter.MaxNodeCount.get -> int
6+
static Microsoft.OpenApi.YamlReader.YamlConverter.MaxNodeCount.get -> uint
77
static Microsoft.OpenApi.YamlReader.YamlConverter.MaxNodeCount.set -> void

src/Microsoft.OpenApi.YamlReader/YamlConverter.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,30 @@ public static class YamlConverter
1919
/// Mirrors the default System.Text.Json depth limit (64) that already bounds the JSON reader path,
2020
/// protecting the recursive conversion from stack exhaustion on deeply nested documents.
2121
/// </summary>
22-
public const int DefaultMaxDepth = 64;
22+
public const uint DefaultMaxDepth = 64;
2323

2424
/// <summary>
2525
/// Default maximum number of JSON nodes that may be materialized from a single YAML document.
2626
/// Guards against YAML anchor/alias expansion ("billion laughs") attacks, where a tiny document
2727
/// expands exponentially when its shared node graph is materialized into an independent JSON tree.
2828
/// </summary>
29-
public const int DefaultMaxNodeCount = 5_000_000;
29+
public const uint DefaultMaxNodeCount = 5_000_000;
3030

31-
private static int _maxDepth = DefaultMaxDepth;
32-
private static int _maxNodeCount = DefaultMaxNodeCount;
31+
private static uint _maxDepth = DefaultMaxDepth;
32+
private static uint _maxNodeCount = DefaultMaxNodeCount;
3333

3434
/// <summary>
3535
/// Gets or sets the maximum nesting depth allowed when converting a YAML node graph into JSON nodes.
3636
/// Defaults to <see cref="DefaultMaxDepth"/>. Raise this if legitimate deeply nested documents are
3737
/// being rejected, or lower it to fail faster when only shallow documents are expected.
3838
/// </summary>
39-
/// <exception cref="ArgumentOutOfRangeException">Thrown when set to a value less than 1.</exception>
40-
public static int MaxDepth
39+
/// <exception cref="ArgumentOutOfRangeException">Thrown when set to zero.</exception>
40+
public static uint MaxDepth
4141
{
4242
get => _maxDepth;
4343
set
4444
{
45-
if (value < 1)
45+
if (value == 0)
4646
{
4747
throw new ArgumentOutOfRangeException(nameof(value), "MaxDepth must be greater than zero.");
4848
}
@@ -57,13 +57,13 @@ public static int MaxDepth
5757
/// ("billion laughs") attacks. Raise this if legitimate large documents are being rejected, or lower
5858
/// it to fail faster when only small documents are expected.
5959
/// </summary>
60-
/// <exception cref="ArgumentOutOfRangeException">Thrown when set to a value less than 1.</exception>
61-
public static int MaxNodeCount
60+
/// <exception cref="ArgumentOutOfRangeException">Thrown when set to zero.</exception>
61+
public static uint MaxNodeCount
6262
{
6363
get => _maxNodeCount;
6464
set
6565
{
66-
if (value < 1)
66+
if (value == 0)
6767
{
6868
throw new ArgumentOutOfRangeException(nameof(value), "MaxNodeCount must be greater than zero.");
6969
}
@@ -78,17 +78,17 @@ public static int MaxNodeCount
7878
/// </summary>
7979
private sealed class YamlConversionBudget
8080
{
81-
private readonly int _maxDepth;
82-
private readonly int _maxNodeCount;
83-
private int _nodeCount;
81+
private readonly uint _maxDepth;
82+
private readonly uint _maxNodeCount;
83+
private uint _nodeCount;
8484

85-
public YamlConversionBudget(int maxDepth, int maxNodeCount)
85+
public YamlConversionBudget(uint maxDepth, uint maxNodeCount)
8686
{
8787
_maxDepth = maxDepth;
8888
_maxNodeCount = maxNodeCount;
8989
}
9090

91-
public void EnterNode(int depth)
91+
public void EnterNode(uint depth)
9292
{
9393
if (depth > _maxDepth)
9494
{
@@ -133,7 +133,7 @@ public static JsonNode ToJsonNode(this YamlNode yaml)
133133
return yaml.ToJsonNode(new YamlConversionBudget(MaxDepth, MaxNodeCount), 0);
134134
}
135135

136-
private static JsonNode ToJsonNode(this YamlNode yaml, YamlConversionBudget budget, int depth)
136+
private static JsonNode ToJsonNode(this YamlNode yaml, YamlConversionBudget budget, uint depth)
137137
{
138138
budget.EnterNode(depth);
139139
return yaml switch
@@ -176,7 +176,7 @@ public static JsonObject ToJsonObject(this YamlMappingNode yaml)
176176
return yaml.ToJsonObject(new YamlConversionBudget(MaxDepth, MaxNodeCount), 0);
177177
}
178178

179-
private static JsonObject ToJsonObject(this YamlMappingNode yaml, YamlConversionBudget budget, int depth)
179+
private static JsonObject ToJsonObject(this YamlMappingNode yaml, YamlConversionBudget budget, uint depth)
180180
{
181181
var node = new JsonObject();
182182
foreach (var keyValuePair in yaml)
@@ -206,7 +206,7 @@ public static JsonArray ToJsonArray(this YamlSequenceNode yaml)
206206
return yaml.ToJsonArray(new YamlConversionBudget(MaxDepth, MaxNodeCount), 0);
207207
}
208208

209-
private static JsonArray ToJsonArray(this YamlSequenceNode yaml, YamlConversionBudget budget, int depth)
209+
private static JsonArray ToJsonArray(this YamlSequenceNode yaml, YamlConversionBudget budget, uint depth)
210210
{
211211
var node = new JsonArray();
212212
foreach (var value in yaml)

test/Microsoft.OpenApi.Readers.Tests/YamlConverterTests.cs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -384,28 +384,24 @@ public void LegitimateAliasesStillConvert()
384384
[Fact]
385385
public void ConversionLimitsDefaultToDocumentedValues()
386386
{
387-
Assert.Equal(64, YamlConverter.DefaultMaxDepth);
388-
Assert.Equal(5_000_000, YamlConverter.DefaultMaxNodeCount);
387+
Assert.Equal(64u, YamlConverter.DefaultMaxDepth);
388+
Assert.Equal(5_000_000u, YamlConverter.DefaultMaxNodeCount);
389389
Assert.Equal(YamlConverter.DefaultMaxDepth, YamlConverter.MaxDepth);
390390
Assert.Equal(YamlConverter.DefaultMaxNodeCount, YamlConverter.MaxNodeCount);
391391
}
392392

393-
[Theory]
394-
[InlineData(0)]
395-
[InlineData(-1)]
396-
public void SettingMaxDepthBelowOneThrows(int value)
393+
[Fact]
394+
public void SettingMaxDepthToZeroThrows()
397395
{
398-
Assert.Throws<ArgumentOutOfRangeException>(() => YamlConverter.MaxDepth = value);
396+
Assert.Throws<ArgumentOutOfRangeException>(() => YamlConverter.MaxDepth = 0);
399397
// The invalid assignment must not have changed the effective limit.
400398
Assert.Equal(YamlConverter.DefaultMaxDepth, YamlConverter.MaxDepth);
401399
}
402400

403-
[Theory]
404-
[InlineData(0)]
405-
[InlineData(-1)]
406-
public void SettingMaxNodeCountBelowOneThrows(int value)
401+
[Fact]
402+
public void SettingMaxNodeCountToZeroThrows()
407403
{
408-
Assert.Throws<ArgumentOutOfRangeException>(() => YamlConverter.MaxNodeCount = value);
404+
Assert.Throws<ArgumentOutOfRangeException>(() => YamlConverter.MaxNodeCount = 0);
409405
// The invalid assignment must not have changed the effective limit.
410406
Assert.Equal(YamlConverter.DefaultMaxNodeCount, YamlConverter.MaxNodeCount);
411407
}

0 commit comments

Comments
 (0)