-
-
Notifications
You must be signed in to change notification settings - Fork 799
fix(serialization): Replace redundant round-trip in JsonSerializer.Parse with Clone #8536
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
fix(serialization): Replace redundant round-trip in JsonSerializer.Parse with Clone #8536
Conversation
…rse with Clone The JsonSerializer.Parse method was performing an unnecessary round-trip serialization (JsonElement -> Utf8JsonWriter -> Utf8JsonReader -> JsonElement). More critically, the WriteTo() operation triggered a buffer overflow bug in System.Text.Json's WriteComplexElement when processing complex nested JSON objects larger than Utf8JsonWriter's internal buffer of 4KB, leading to memory corruption and undefined behaviour. Replace the round-trip with JsonElement.Clone() which: - Preserves identical semantics - Eliminates the buffer overflow risk entirely - Improves performance by avoiding unnecessary serialization - Maintains proper memory management for the cloned element
The JsonSerializer.Parse method was performing an unnecessary round-trip serialization (JsonElement -> Utf8JsonWriter -> Utf8JsonReader -> JsonElement). More critically, the WriteTo() operation triggered a buffer overflow bug in System.Text.Json's WriteComplexElement when processing complex nested JSON objects larger than Utf8JsonWriter's internal buffer of 4KB, leading to memory corruption and undefined behaviour. Replace the round-trip with JsonElement.Clone() which:
Sorry I didn't set up a bug report before opening the pull request, but reproducing this bug outside my application has been a challenge. |
@SindriFr thanks for the PR! |
Summary of the changes (Less than 80 chars)