dotnet / dotnet/wpf

BindUriHelper.UriToString is technically broken as it fails to truncate the uri

Open
#9,530 6 comments 1 reaction 0 assignees View on GitHub
Investigate
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

### Description

`UriToString` is technically broken (both in .NET Framework and .NET Core) as it is supposed to truncate for lengths over 2083 chars but it fails to do so due to a misunderstanding how `StringBuilder`'s capacity works from the original developer.

https://github.com/dotnet/wpf/blob/bdec3d515f52c55bc3a6431e71db97c88f868215/src/Microsoft.DotNet.Wpf/src/Shared/MS/Utility/BindUriHelper.cs#L67-L71

It is the same in .NET Framework: https://referencesource.microsoft.com/#PresentationFramework/src/Shared/MS/Utility/BindUriHelper.cs,60

### Expected behavior

The URI is trimmed on string conversion if it exceeds `MAX_URL_LENGTH`.

### Actual behavior

Unnecessary allocations are made and the string is not trimmed as intended.

### Next steps

- We should definitely fix the unnecessary allocations.
- However, should we change it to the intended behavior, or should we keep the current one?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.