Merge branch 'patch-1' of https://github.com/Romasz/Newtonsoft.Json into Romasz-patch-1

This commit is contained in:
James Newton-King
2017-10-15 11:31:48 +13:00
3 changed files with 45 additions and 1 deletions
@@ -4,4 +4,7 @@
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<runtime>
<gcAllowVeryLargeObjects enabled="true"/>
</runtime>
</configuration>
@@ -273,6 +273,46 @@ namespace Newtonsoft.Json.Tests.JsonTextReaderTests
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
}
[Test]
public void ReadLargeObjects()
{
const int nrItems = 2;
const int length = int.MaxValue / 3;
byte apostrophe = Encoding.ASCII.GetBytes(@"""").First();
byte openingBracket = Encoding.ASCII.GetBytes(@"[").First();
byte comma = Encoding.ASCII.GetBytes(@",").First();
byte closingBracket = Encoding.ASCII.GetBytes(@"]").First();
using (MemoryStream ms = new MemoryStream())
{
ms.WriteByte(openingBracket);
for (int i = 0; i < nrItems; i++)
{
ms.WriteByte(apostrophe);
for (int j = 0; j <= length; j++)
ms.WriteByte(7);
ms.WriteByte(apostrophe);
if (i < nrItems - 1)
ms.WriteByte(comma);
}
ms.WriteByte(closingBracket);
ms.Seek(0, SeekOrigin.Begin);
var reader = new JsonTextReader(new StreamReader(ms));
Assert.IsTrue(reader.Read());
for (int i = 0; i < nrItems; i++)
Assert.IsTrue(reader.Read());
Assert.IsTrue(reader.Read());
Assert.IsFalse(reader.Read());
}
}
[Test]
public void ReadSingleBytes()
{
+2 -1
View File
@@ -237,8 +237,9 @@ namespace Newtonsoft.Json
{
// once in the last 10% of the buffer shift the remaining content to the start to avoid
// unnecessarily increasing the buffer size when reading numbers/strings
//romasz: do it also if we are reading large objects that can cause the buffer's new length to exceed int.MaxValue
int length = _chars.Length;
if (length - _charPos <= length * 0.1)
if (length - _charPos <= length * 0.1 || length > int.MaxValue / 2 - 1)
{
int count = _charsUsed - _charPos;
if (count > 0)