Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stack overflow. when serialize object to Json #13924

Closed
liuguiyou opened this issue Sep 12, 2019 · 3 comments
Closed

Stack overflow. when serialize object to Json #13924

liuguiyou opened this issue Sep 12, 2019 · 3 comments
Assignees
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates ✔️ Resolution: Duplicate Resolved as a duplicate of another issue investigate

Comments

@liuguiyou
Copy link

Describe the bug

When api serialize object to Json, stack overflow

To Reproduce

Steps to reproduce the behavior:

  1. Using this version of ASP.NET Core 3.0.0-preview9-19423-09
  2. Run this code https://localhost:5001/test/json
  3. With these arguments
  4. See error

Expected behavior

Not stack overflow

Screenshots

1568277803(1)

Code solution file

src.zip

@Pilchie Pilchie added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Sep 12, 2019
@pranavkm pranavkm self-assigned this Sep 12, 2019
@pranavkm
Copy link
Contributor

@ahsonkhan here's a minimal repro without MVC: https://github.com/pranavkm/repro/tree/prkrishn/json-stackoverflow. This stackoverflow seems to be resolved using your patched binary, although I do get a null ref:

Unhandled exception. System.ArgumentNullException: Value cannot be null. (Parameter 'text')
   at System.Text.Json.JsonWriterHelper.NeedsEscaping(ReadOnlySpan`1 value, JavaScriptEncoder encoder)
   at System.Text.Json.Utf8JsonWriter.WriteStringEscape(ReadOnlySpan`1 value)
   at System.Text.Json.Utf8JsonWriter.WriteStringValue(ReadOnlySpan`1 value)
   at System.Text.Json.Utf8JsonWriter.WriteStringValue(String value)
   at System.Text.Json.Serialization.Converters.JsonConverterString.Write(Utf8JsonWriter writer, String value, JsonSerializerOptions options)
   at System.Text.Json.JsonPropertyInfoNotNullable`4.OnWrite(WriteStackFrame& current, Utf8JsonWriter writer)
   at System.Text.Json.JsonPropertyInfo.Write(WriteStack& state, Utf8JsonWriter writer)
   at System.Text.Json.JsonSerializer.HandleObject(JsonPropertyInfo jsonPropertyInfo, JsonSerializerOptions options, Utf8JsonWriter writer, WriteStack& state)
   at System.Text.Json.JsonSerializer.WriteObject(JsonSerializerOptions options, Utf8JsonWriter writer, WriteStack& state)
   at System.Text.Json.JsonSerializer.Write(Utf8JsonWriter writer, Int32 originalWriterDepth, Int32 flushThreshold, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.JsonSerializer.WriteAsyncCore(Stream utf8Json, Object value, Type inputType, JsonSerializerOptions options, CancellationToken cancellationToken)
   at ConsoleApp17.Program.Main(String[] args) in C:\Users\Pranav\source\repos\ConsoleApp17\ConsoleApp17\Program.cs:line 29
   at ConsoleApp17.Program.<Main>(String[] args)

Could you have a look?

@ahsonkhan
Copy link
Member

ahsonkhan commented Sep 12, 2019

This issue has been fixed already (in dotnet/corefx#40997) and cannot be reproduced. The root cause was not guarding against null pointers up-front before calling the JavascriptEncoder.

https://github.com/dotnet/corefx/blob/b384d34bcd4674bc0df08a333a4973de8e2ac7ae/src/System.Text.Json/src/System/Text/Json/Writer/JsonWriterHelper.Escaping.cs#L88

although I do get a null ref

Keep in mind, that isn't a null ref, but argument validation - ArgumentNullException.

The patch I shared didn't have that fix at the time, and you'd see that behavior for empty strings in the object you are serializing ("").

@pranavkm
Copy link
Contributor

Thanks for the report @liuguiyou. This issue will be resolved in 3.0 RTM. In the meanwhile, you could consider using Newtonsoft.Json based JSON formatters in your application: https://docs.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-2.2&tabs=visual-studio#jsonnet-support

@pranavkm pranavkm added the ✔️ Resolution: Duplicate Resolved as a duplicate of another issue label Sep 12, 2019
@ghost ghost locked as resolved and limited conversation to collaborators Dec 2, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates ✔️ Resolution: Duplicate Resolved as a duplicate of another issue investigate
Projects
None yet
Development

No branches or pull requests

4 participants