Skip to content

Bounds check after overlapping span length checks #121262

@MihaZupan

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIuntriagedNew issue has not been triaged by the area owner

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions