Permalink
Browse files

Mark quota exceptions as Handled for Json

  • Loading branch information...
1 parent 1c4d4c9 commit cf10ff8b67736bacfa1dace5928027066ac3a059 youssefm committed Apr 4, 2012
@@ -252,21 +252,13 @@ public override Task<object> ReadFromStreamAsync(Type type, Stream stream, HttpC
JsonSerializer jsonSerializer = JsonSerializer.Create(_jsonSerializerSettings);
if (formatterLogger != null)
{
+ // Error must always be marked as handled
+ // Failure to do so can cause the exception to be rethrown at every recursive level and overflow the stack for x64 CLR processes
jsonSerializer.Error += (sender, e) =>
{
Exception exception = e.ErrorContext.Error;
-
- // reader quota exceptions are fatal and cannot be recovered from
- // we need to shortcircuit any further deserialization
- if (exception is JsonReaderQuotaException)
- {
- e.ErrorContext.Handled = false;
- }
- else
- {
- formatterLogger.LogError(e.ErrorContext.Path, exception.Message);
- e.ErrorContext.Handled = true;
- }
+ formatterLogger.LogError(e.ErrorContext.Path, exception.Message);
+ e.ErrorContext.Handled = true;
};
}
return jsonSerializer.Deserialize(jsonTextReader, type);
@@ -29,11 +29,18 @@ public override object Value
public override bool Read()
{
+ int initialDepth = Depth;
+ bool didRead = base.Read();
if (Depth > _maxDepth)
{
+ // Advance the reader past the initial depth to avoid more exceptions from this violation
+ while (Depth > initialDepth)
+ {
+ base.Read();
+ }
throw new JsonReaderQuotaException(RS.Format(Properties.Resources.JsonTooDeep, _maxDepth));
}
- return base.Read();
+ return didRead;
}
private static string FixUpInvalidUnicodeString(string s)

0 comments on commit cf10ff8

Please sign in to comment.