Skip to content

Conversation

tarekgh
Copy link
Member

@tarekgh tarekgh commented Jul 24, 2025

Fixes #118017

In .NET we have introduced the extension method

public static bool TryNormalize(this ReadOnlySpan<char> source, Span<char> destination, out int charsWritten, NormalizationForm normalizationForm = NormalizationForm.FormC) =>

If the user provides overlapping source and destination spans, it leads to undefined behavior, since we rely on underlying OS components for normalization. The fix is straightforward: disallow overlapping spans and throw an exception. This API is new and hasn’t shipped in any released version yet, so there are no compatibility concerns at this point—we’re addressing it now to prevent future issues.

@Copilot Copilot AI review requested due to automatic review settings July 24, 2025 22:46
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds validation to prevent undefined behavior in the TryNormalize method by detecting and rejecting overlapping source and destination spans. The fix addresses a potential issue where overlapping spans could cause unpredictable results due to the underlying OS normalization components.

Key changes:

  • Added overlap detection in the TryNormalize method with appropriate exception throwing
  • Introduced a new error message resource for the overlap validation
  • Added comprehensive test cases to verify the overlap detection works correctly

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/libraries/System.Private.CoreLib/src/System/Globalization/Normalization.cs Added overlap validation check that throws ArgumentException when source and destination spans overlap
src/libraries/System.Private.CoreLib/src/Resources/Strings.resx Added new error message resource for overlapping spans validation
src/libraries/System.Runtime/tests/System.Globalization.Extensions.Tests/Normalization/StringNormalizationTests.cs Added test cases to verify overlap detection throws appropriate exceptions
Comments suppressed due to low confidence (1)

src/libraries/System.Runtime/tests/System.Globalization.Extensions.Tests/Normalization/StringNormalizationTests.cs:133

  • This test case doesn't actually test overlapping spans. The source span starts at index 0 with length 5, while the destination span starts at index 4 with length 1. These spans overlap by only 1 character at index 4, but this is a very minimal overlap case. Consider adding a test case where the spans have more significant overlap to ensure the validation works correctly in various overlap scenarios.
            Assert.Throws<ArgumentException>("destination", () => overlappingDestination.AsSpan().TryNormalize(overlappingDestination.AsSpan(4), out int charsWritten, NormalizationForm.FormC));

Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-globalization
See info in area-owners.md if you want to be subscribed.

@tarekgh tarekgh added this to the 10.0.0 milestone Jul 24, 2025
…ions.Tests/Normalization/StringNormalizationTests.cs

Co-authored-by: Stephen Toub <[email protected]>
@jkotas jkotas merged commit 7f96aef into dotnet:main Jul 26, 2025
143 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Aug 26, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

New span overloads of StringNormalizationExtensions do not check memory overlapping
5 participants