GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — -Fixed JsonWriter.WriteToken(JsonReader) not erroring on incomplete t… · JamesNK/Newtonsoft.Json@1a0ae2a

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • Src
    • Newtonsoft.Json.Tests/Issues
      • Issue1321.cs
    • Newtonsoft.Json
      • JsonWriter.Async.cs
      • JsonWriter.cs
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎Src/Newtonsoft.Json.Tests/Issues/Issue1321.cs‎

Copy file name to clipboard
+202Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
#region License
2+
// Copyright (c) 2007 James Newton-King
3+
//
4+
// Permission is hereby granted, free of charge, to any person
5+
// obtaining a copy of this software and associated documentation
6+
// files (the "Software"), to deal in the Software without
7+
// restriction, including without limitation the rights to use,
8+
// copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the
10+
// Software is furnished to do so, subject to the following
11+
// conditions:
12+
//
13+
// The above copyright notice and this permission notice shall be
14+
// included in all copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18+
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20+
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21+
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22+
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
// OTHER DEALINGS IN THE SOFTWARE.
24+
#endregion
25+
26+
using System;
27+
using System.Collections.Generic;
28+
using System.IO;
29+
using System.Linq;
30+
using System.Text;
31+
#if !(NET20 || NET35 || NET40 || PORTABLE40)
32+
using System.Threading.Tasks;
33+
#endif
34+
using Newtonsoft.Json;
35+
using Newtonsoft.Json.Linq;
36+
using System.Xml;
37+
#if !NET20
38+
using System.Xml.Linq;
39+
#endif
40+
#if DNXCORE50
41+
using Xunit;
42+
using Test = Xunit.FactAttribute;
43+
using Assert = Newtonsoft.Json.Tests.XUnitAssert;
44+
#else
45+
using NUnit.Framework;
46+
#endif
47+
48+
namespace Newtonsoft.Json.Tests.Issues
49+
{
50+
[TestFixture]
51+
public class Issue1321 : TestFixtureBase
52+
{
53+
[Test]
54+
public void Test()
55+
{
56+
ExceptionAssert.Throws<JsonWriterException>(() =>
57+
{
58+
JsonConvert.DeserializeObject(
59+
@"[""1"",",
60+
new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.None, MaxDepth = 1024 });
61+
}, "Unexpected end when reading token. Path ''.");
62+
}
63+
64+
[Test]
65+
public void Test2()
66+
{
67+
JArray a = new JArray();
68+
69+
var writer = a.CreateWriter();
70+
71+
JsonTextReader reader = new JsonTextReader(new StringReader(@"[""1"","));
72+
73+
ExceptionAssert.Throws<JsonWriterException>(() =>
74+
{
75+
writer.WriteToken(reader);
76+
}, "Unexpected end when reading token. Path ''.");
77+
}
78+
79+
#if !(NET20 || NET35 || NET40 || PORTABLE40)
80+
[Test]
81+
public async Task Test2_Async()
82+
{
83+
JArray a = new JArray();
84+
85+
var writer = a.CreateWriter();
86+
87+
JsonTextReader reader = new JsonTextReader(new StringReader(@"[""1"","));
88+
89+
await ExceptionAssert.ThrowsAsync<JsonWriterException>(async () =>
90+
{
91+
await writer.WriteTokenAsync(reader);
92+
}, "Unexpected end when reading token. Path ''.");
93+
}
94+
#endif
95+
96+
[Test]
97+
public void Test3()
98+
{
99+
JArray a = new JArray();
100+
101+
var writer = a.CreateWriter();
102+
103+
JsonTextReader reader = new JsonTextReader(new StringReader(@"[""1"","));
104+
reader.Read();
105+
106+
ExceptionAssert.Throws<JsonWriterException>(() =>
107+
{
108+
writer.WriteToken(reader);
109+
}, "Unexpected end when reading token. Path ''.");
110+
}
111+
112+
#if !(NET20 || NET35 || NET40 || PORTABLE40)
113+
[Test]
114+
public async Task Test3_Async()
115+
{
116+
JArray a = new JArray();
117+
118+
var writer = a.CreateWriter();
119+
120+
JsonTextReader reader = new JsonTextReader(new StringReader(@"[""1"","));
121+
await reader.ReadAsync();
122+
123+
await ExceptionAssert.ThrowsAsync<JsonWriterException>(async () =>
124+
{
125+
await writer.WriteTokenAsync(reader);
126+
}, "Unexpected end when reading token. Path ''.");
127+
}
128+
#endif
129+
130+
[Test]
131+
public void Test4()
132+
{
133+
JArray a = new JArray();
134+
135+
var writer = a.CreateWriter();
136+
137+
JsonTextReader reader = new JsonTextReader(new StringReader(@"[[""1"","));
138+
reader.Read();
139+
reader.Read();
140+
141+
ExceptionAssert.Throws<JsonWriterException>(() =>
142+
{
143+
writer.WriteToken(reader);
144+
}, "Unexpected end when reading token. Path ''.");
145+
}
146+
147+
#if !(NET20 || NET35 || NET40 || PORTABLE40)
148+
[Test]
149+
public async Task Test4_Async()
150+
{
151+
JArray a = new JArray();
152+
153+
var writer = a.CreateWriter();
154+
155+
JsonTextReader reader = new JsonTextReader(new StringReader(@"[[""1"","));
156+
await reader.ReadAsync();
157+
await reader.ReadAsync();
158+
159+
await ExceptionAssert.ThrowsAsync<JsonWriterException>(async () =>
160+
{
161+
await writer.WriteTokenAsync(reader);
162+
}, "Unexpected end when reading token. Path ''.");
163+
}
164+
#endif
165+
166+
[Test]
167+
public void Test5()
168+
{
169+
StringWriter sw = new StringWriter();
170+
JsonTextWriter writer = new JsonTextWriter(sw);
171+
writer.WriteStartArray();
172+
173+
JsonTextReader reader = new JsonTextReader(new StringReader(@"[[""1"","));
174+
reader.Read();
175+
reader.Read();
176+
177+
ExceptionAssert.Throws<JsonWriterException>(() =>
178+
{
179+
writer.WriteToken(reader);
180+
}, "Unexpected end when reading token. Path '[0]'.");
181+
}
182+
183+
#if !(NET20 || NET35 || NET40 || PORTABLE40)
184+
[Test]
185+
public async Task Test5_Async()
186+
{
187+
StringWriter sw = new StringWriter();
188+
JsonTextWriter writer = new JsonTextWriter(sw);
189+
writer.WriteStartArray();
190+
191+
JsonTextReader reader = new JsonTextReader(new StringReader(@"[[""1"","));
192+
await reader.ReadAsync();
193+
await reader.ReadAsync();
194+
195+
await ExceptionAssert.ThrowsAsync<JsonWriterException>(async () =>
196+
{
197+
await writer.WriteTokenAsync(reader);
198+
}, "Unexpected end when reading token. Path '[0]'.");
199+
}
200+
#endif
201+
}
202+
}
Collapse file

‎Src/Newtonsoft.Json/JsonWriter.Async.cs‎

Copy file name to clipboardExpand all lines: Src/Newtonsoft.Json/JsonWriter.Async.cs
+12-2Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ internal Task InternalWriteCommentAsync(CancellationToken cancellationToken)
675675

676676
internal virtual async Task WriteTokenAsync(JsonReader reader, bool writeChildren, bool writeDateConstructorAsDate, bool writeComments, CancellationToken cancellationToken)
677677
{
678-
int initialDepth = CalculateWriteTokenDepth(reader);
678+
int initialDepth = CalculateWriteTokenInitialDepth(reader);
679679

680680
do
681681
{
@@ -696,14 +696,19 @@ internal virtual async Task WriteTokenAsync(JsonReader reader, bool writeChildre
696696
initialDepth - 1 < reader.Depth - (JsonTokenUtils.IsEndToken(reader.TokenType) ? 1 : 0)
697697
&& writeChildren
698698
&& await reader.ReadAsync(cancellationToken).ConfigureAwait(false));
699+
700+
if (initialDepth < CalculateWriteTokenFinalDepth(reader))
701+
{
702+
throw JsonWriterException.Create(this, "Unexpected end when reading token.", null);
703+
}
699704
}
700705

701706
// For internal use, when we know the writer does not offer true async support (e.g. when backed
702707
// by a StringWriter) and therefore async write methods are always in practice just a less efficient
703708
// path through the sync version.
704709
internal async Task WriteTokenSyncReadingAsync(JsonReader reader, CancellationToken cancellationToken)
705710
{
706-
int initialDepth = CalculateWriteTokenDepth(reader);
711+
int initialDepth = CalculateWriteTokenInitialDepth(reader);
707712

708713
do
709714
{
@@ -720,6 +725,11 @@ internal async Task WriteTokenSyncReadingAsync(JsonReader reader, CancellationTo
720725
// stop if we have reached the end of the token being read
721726
initialDepth - 1 < reader.Depth - (JsonTokenUtils.IsEndToken(reader.TokenType) ? 1 : 0)
722727
&& await reader.ReadAsync(cancellationToken).ConfigureAwait(false));
728+
729+
if (initialDepth < CalculateWriteTokenFinalDepth(reader))
730+
{
731+
throw JsonWriterException.Create(this, "Unexpected end when reading token.", null);
732+
}
723733
}
724734

725735
private async Task WriteConstructorDateAsync(JsonReader reader, CancellationToken cancellationToken)
Collapse file

‎Src/Newtonsoft.Json/JsonWriter.cs‎

Copy file name to clipboardExpand all lines: Src/Newtonsoft.Json/JsonWriter.cs
+18-2Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ public void WriteToken(JsonToken token)
641641

642642
internal virtual void WriteToken(JsonReader reader, bool writeChildren, bool writeDateConstructorAsDate, bool writeComments)
643643
{
644-
int initialDepth = CalculateWriteTokenDepth(reader);
644+
int initialDepth = CalculateWriteTokenInitialDepth(reader);
645645

646646
do
647647
{
@@ -662,9 +662,14 @@ internal virtual void WriteToken(JsonReader reader, bool writeChildren, bool wri
662662
initialDepth - 1 < reader.Depth - (JsonTokenUtils.IsEndToken(reader.TokenType) ? 1 : 0)
663663
&& writeChildren
664664
&& reader.Read());
665+
666+
if (initialDepth < CalculateWriteTokenFinalDepth(reader))
667+
{
668+
throw JsonWriterException.Create(this, "Unexpected end when reading token.", null);
669+
}
665670
}
666671

667-
private int CalculateWriteTokenDepth(JsonReader reader)
672+
private int CalculateWriteTokenInitialDepth(JsonReader reader)
668673
{
669674
JsonToken type = reader.TokenType;
670675
if (type == JsonToken.None)
@@ -675,6 +680,17 @@ private int CalculateWriteTokenDepth(JsonReader reader)
675680
return JsonTokenUtils.IsStartToken(type) ? reader.Depth : reader.Depth + 1;
676681
}
677682

683+
private int CalculateWriteTokenFinalDepth(JsonReader reader)
684+
{
685+
JsonToken type = reader.TokenType;
686+
if (type == JsonToken.None)
687+
{
688+
return -1;
689+
}
690+
691+
return JsonTokenUtils.IsEndToken(type) ? reader.Depth - 1 : reader.Depth;
692+
}
693+
678694
private void WriteConstructorDate(JsonReader reader)
679695
{
680696
if (!reader.Read())

0 commit comments

Comments
0 (0)
You are visitor 01402622 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.