diff --git a/Src/Newtonsoft.Json.TestConsole/App.config b/Src/Newtonsoft.Json.TestConsole/App.config index 5c73fc53..1893e583 100644 --- a/Src/Newtonsoft.Json.TestConsole/App.config +++ b/Src/Newtonsoft.Json.TestConsole/App.config @@ -4,4 +4,7 @@ + + + \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/JsonTextReaderTests/ReadTests.cs b/Src/Newtonsoft.Json.Tests/JsonTextReaderTests/ReadTests.cs index 30fa4e00..c895bc11 100644 --- a/Src/Newtonsoft.Json.Tests/JsonTextReaderTests/ReadTests.cs +++ b/Src/Newtonsoft.Json.Tests/JsonTextReaderTests/ReadTests.cs @@ -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() { diff --git a/Src/Newtonsoft.Json/JsonTextReader.cs b/Src/Newtonsoft.Json/JsonTextReader.cs index b273b031..7a282c0c 100644 --- a/Src/Newtonsoft.Json/JsonTextReader.cs +++ b/Src/Newtonsoft.Json/JsonTextReader.cs @@ -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)