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.)
pull/5349/head
Jon Skeet 6 years ago committed by Jie Luo
parent ebfc0432c1
commit 627cc48f5a
  1. 10
      csharp/compatibility_tests/v3.0.0/src/Google.Protobuf.Test/CodedInputStreamTest.cs
  2. 10
      csharp/src/Google.Protobuf.Test/CodedInputStreamTest.cs
  3. 4
      csharp/src/Google.Protobuf/CodedInputStream.cs

@ -313,14 +313,14 @@ namespace Google.Protobuf
[Test] [Test]
public void MaliciousRecursion() public void MaliciousRecursion()
{ {
ByteString data64 = MakeRecursiveMessage(64).ToByteString(); ByteString atRecursiveLimit = MakeRecursiveMessage(CodedInputStream.DefaultRecursionLimit).ToByteString();
ByteString data65 = MakeRecursiveMessage(65).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)); Assert.Throws<InvalidProtocolBufferException>(() => TestRecursiveMessage.Parser.ParseFrom(input));
} }

@ -327,14 +327,14 @@ namespace Google.Protobuf
[Test] [Test]
public void MaliciousRecursion() public void MaliciousRecursion()
{ {
ByteString data64 = MakeRecursiveMessage(64).ToByteString(); ByteString atRecursiveLimit = MakeRecursiveMessage(CodedInputStream.DefaultRecursionLimit).ToByteString();
ByteString data65 = MakeRecursiveMessage(65).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)); Assert.Throws<InvalidProtocolBufferException>(() => TestRecursiveMessage.Parser.ParseFrom(input));
} }

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

Loading…
Cancel
Save