Skip to content

Bounds check after overlapping span length checks #121262

Closed

Description

@MihaZupan

https://godbolt.org/z/3arPqT9oP

public static bool Foo(ReadOnlySpan<char> span)
{
    for (int i = 0; i < span.Length; i++)
    {
        if (span[i] == '%' && (uint)(i + 2) < (uint)span.Length)
        {
            if (span[i + 1] == 'F' && // Bounds check
                span[i + 2] == 'F')
            {
                return true;
            }
        }
    }
    
    return false;
}

(uint)i < (uint)span.Length && (uint)(i + 2) < (uint)span.Length implies that i + 1 is also in range.

E.g.

if ((uint)(i + 2) >= (uint)uriString.Length ||
colonOffset == 0 ||
// Redundant checks to eliminate range checks below
(uint)i >= (uint)uriString.Length ||
(uint)(i + 1) >= (uint)uriString.Length)

or
https://github.com/MihaZupan/runtime/blob/299818ab507ff37b0ef74918fdc35463b7e2d5e1/src/libraries/System.Private.Uri/src/System/UriHelper.cs#L321-L325 from #121261

Activity

  1. added
    area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
    on Nov 1, 2025
  2. dotnet-policy-service commented on Nov 1, 2025

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
    See info in area-owners.md if you want to be subscribed.

  3. JulieLeeMSFT commented on Dec 11, 2025

    @JulieLeeMSFT
    Member

    This issue is being tracked in #109677. Marking this as future for now.

  4. removed
    untriagedNew issue has not been triaged by the area owner
    on Dec 11, 2025
  5. added this to the Future milestone on Dec 11, 2025
  6. modified the milestones: Future, 11.0.0 on Feb 10, 2026
  7. self-assigned this
    on Feb 10, 2026
  8. locked and limited conversation to collaborators on Mar 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions