Increase C# default recursion limit to 100 (#5339)

* Increase C# default recursion limit to 100

This matches the Java and C++ defaults.

* Change compatibility tests to use execution-time default recursion limit

This way the same tests should pass against all versions, even
if the recursion limit changes. (The tests will be testing whether
different messages work, admittedly - but that's probably fine.)
This commit is contained in:
Jon Skeet 2018-11-13 18:21:30 +00:00 committed by Jie Luo
parent ebfc0432c1
commit 627cc48f5a
3 changed files with 12 additions and 12 deletions

View File

@ -313,14 +313,14 @@ namespace Google.Protobuf
[Test]
public void MaliciousRecursion()
{
ByteString data64 = MakeRecursiveMessage(64).ToByteString();
ByteString data65 = MakeRecursiveMessage(65).ToByteString();
ByteString atRecursiveLimit = MakeRecursiveMessage(CodedInputStream.DefaultRecursionLimit).ToByteString();
ByteString beyondRecursiveLimit = MakeRecursiveMessage(CodedInputStream.DefaultRecursionLimit + 1).ToByteString();
AssertMessageDepth(TestRecursiveMessage.Parser.ParseFrom(data64), 64);
AssertMessageDepth(TestRecursiveMessage.Parser.ParseFrom(atRecursiveLimit), CodedInputStream.DefaultRecursionLimit);
Assert.Throws<InvalidProtocolBufferException>(() => TestRecursiveMessage.Parser.ParseFrom(data65));
Assert.Throws<InvalidProtocolBufferException>(() => TestRecursiveMessage.Parser.ParseFrom(beyondRecursiveLimit));
CodedInputStream input = CodedInputStream.CreateWithLimits(new MemoryStream(data64.ToByteArray()), 1000000, 63);
CodedInputStream input = CodedInputStream.CreateWithLimits(new MemoryStream(atRecursiveLimit.ToByteArray()), 1000000, CodedInputStream.DefaultRecursionLimit - 1);
Assert.Throws<InvalidProtocolBufferException>(() => TestRecursiveMessage.Parser.ParseFrom(input));
}

View File

@ -327,14 +327,14 @@ namespace Google.Protobuf
[Test]
public void MaliciousRecursion()
{
ByteString data64 = MakeRecursiveMessage(64).ToByteString();
ByteString data65 = MakeRecursiveMessage(65).ToByteString();
ByteString atRecursiveLimit = MakeRecursiveMessage(CodedInputStream.DefaultRecursionLimit).ToByteString();
ByteString beyondRecursiveLimit = MakeRecursiveMessage(CodedInputStream.DefaultRecursionLimit + 1).ToByteString();
AssertMessageDepth(TestRecursiveMessage.Parser.ParseFrom(data64), 64);
AssertMessageDepth(TestRecursiveMessage.Parser.ParseFrom(atRecursiveLimit), CodedInputStream.DefaultRecursionLimit);
Assert.Throws<InvalidProtocolBufferException>(() => TestRecursiveMessage.Parser.ParseFrom(data65));
Assert.Throws<InvalidProtocolBufferException>(() => TestRecursiveMessage.Parser.ParseFrom(beyondRecursiveLimit));
CodedInputStream input = CodedInputStream.CreateWithLimits(new MemoryStream(data64.ToByteArray()), 1000000, 63);
CodedInputStream input = CodedInputStream.CreateWithLimits(new MemoryStream(atRecursiveLimit.ToByteArray()), 1000000, CodedInputStream.DefaultRecursionLimit - 1);
Assert.Throws<InvalidProtocolBufferException>(() => TestRecursiveMessage.Parser.ParseFrom(input));
}

View File

@ -93,7 +93,7 @@ namespace Google.Protobuf
private uint nextTag = 0;
private bool hasNextTag = false;
internal const int DefaultRecursionLimit = 64;
internal const int DefaultRecursionLimit = 100;
internal const int DefaultSizeLimit = Int32.MaxValue;
internal const int BufferSize = 4096;
@ -260,7 +260,7 @@ namespace Google.Protobuf
/// to avoid maliciously-recursive data.
/// </summary>
/// <remarks>
/// The default limit is 64.
/// The default limit is 100.
/// </remarks>
/// <value>
/// The recursion limit for this stream.