Fix Uri.TryUnescapeDataString throwing instead of returning false with small destination buffers#124655
Open
Fix Uri.TryUnescapeDataString throwing instead of returning false with small destination buffers#124655
Conversation
Contributor
|
Tagging subscribers to this area: @karelz, @dotnet/ncl |
…h small destination buffers Co-authored-by: MihaZupan <25307628+MihaZupan@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix ArgumentOutOfRangeException in Uri.TryUnescapeDataString
Fix Uri.TryUnescapeDataString throwing instead of returning false with small destination buffers
Feb 20, 2026
MihaZupan
reviewed
Feb 20, 2026
Co-authored-by: MihaZupan <25307628+MihaZupan@users.noreply.github.com>
MihaZupan
approved these changes
Feb 20, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a bug in Uri.TryUnescapeDataString where the method threw ArgumentOutOfRangeException instead of returning false when the destination buffer was smaller than the index of the first % character in the input. The fix adds an early-out guard that checks if the destination can hold the literal prefix before the first escape sequence.
Changes:
- Added an early-out check in
TryUnescapeDataStringto returnfalsewhendestination.Length < indexOfFirstToUnescape - Added test cases covering the bug scenario with various input strings and destination sizes
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/libraries/System.Private.Uri/src/System/UriExt.cs |
Added guard check to prevent ArgumentOutOfRangeException when destination is too small to hold the literal prefix before first % |
src/libraries/System.Private.Uri/tests/FunctionalTests/UriEscapingTest.cs |
Added [Theory] test with 5 test cases covering the bug scenario with different input strings and destination buffer sizes |
This was referenced Feb 20, 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.
Uri.TryUnescapeDataStringthrewArgumentOutOfRangeExceptioninstead of returningfalsewhen the destination span was shorter than the index of the first%in the input:Description
Root cause: When the input contains
%, the method usesdestination.Slice(indexOfFirstToUnescape)as the initial buffer forValueStringBuilder. Ifdestination.Length < indexOfFirstToUnescape, thisSlicecall throws instead of returningfalsegracefully.Fix: Added an early-out guard — when
destination.Length < indexOfFirstToUnescape, the destination cannot hold even the literal prefix before the first%, sofalseis returned immediately withcharsWritten = 0. This check is placed before the overlapped-buffer detection, so it fires unconditionally in both overlapped and non-overlapped cases.Tests: Added
[Theory]cases covering the reported scenario and similar variants ("aa%"and"aaa%41"with destination sizes smaller than the prefix length).Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.