fix(serialization): Replace redundant round-trip in JsonSerializer.Parse with Clone#8536
Merged
michaelstaib merged 3 commits intoAug 14, 2025
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
Contributor
Author
|
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. |
Member
|
@SindriFr thanks for the PR! |
michaelstaib
approved these changes
Aug 14, 2025
michaelstaib
added a commit
that referenced
this pull request
Aug 27, 2025
This was referenced Aug 28, 2025
This was referenced Sep 8, 2025
Closed
This was referenced Oct 13, 2025
This was referenced Oct 27, 2025
This was referenced May 13, 2026
Closed
This was referenced May 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary of the changes (Less than 80 chars)