Skip to content

Redundant bound checks around span.Slice(span.Length - CNS) #127486

Closed

Description

@EgorBo

Found in #127485

int Test(ReadOnlySpan<byte> span)
{
    if (span.Length >= sizeof(int))
        return BinaryPrimitives.ReadInt32BigEndian(span.Slice(span.Length - sizeof(int)));
    return -1;
}

Currently emits:

; Method Bench:Test(System.ReadOnlySpan`1[byte]):int:this (FullOpts)
G_M27777_IG01:  ;; offset=0x0000
       sub      rsp, 40
						;; size=4 bbWeight=1 PerfScore 0.25

G_M27777_IG02:  ;; offset=0x0004
       mov      rcx, bword ptr [rdx]
       mov      edx, dword ptr [rdx+0x08]
       cmp      edx, 4
       jge      SHORT G_M27777_IG05
						;; size=11 bbWeight=1 PerfScore 5.25

G_M27777_IG03:  ;; offset=0x000F
       mov      eax, -1
						;; size=5 bbWeight=0.50 PerfScore 0.12

G_M27777_IG04:  ;; offset=0x0014
       add      rsp, 40
       ret      
						;; size=5 bbWeight=0.50 PerfScore 0.62

G_M27777_IG05:  ;; offset=0x0019
       lea      eax, [rdx-0x04]
       cmp      eax, edx
       ja       SHORT G_M27777_IG07
       mov      r8d, eax
       add      rcx, r8
       sub      edx, eax
       cmp      edx, 4
       jl       SHORT G_M27777_IG08
       movbe    eax, dword ptr [rcx]
						;; size=24 bbWeight=0.50 PerfScore 2.88

G_M27777_IG06:  ;; offset=0x0031
       add      rsp, 40
       ret      
						;; size=5 bbWeight=0.50 PerfScore 0.62

G_M27777_IG07:  ;; offset=0x0036
       call     [System.ThrowHelper:ThrowArgumentOutOfRangeException()]
       int3     
						;; size=7 bbWeight=0 PerfScore 0.00

G_M27777_IG08:  ;; offset=0x003D
       mov      ecx, 40
       call     [System.ThrowHelper:ThrowArgumentOutOfRangeException(int)]
       int3     
						;; size=12 bbWeight=0 PerfScore 0.00
; Total bytes of code: 73

Activity

  1. self-assigned this
    on Apr 28, 2026
  2. added
    untriaged
    on Apr 28, 2026
  3. added
    area-CodeGen-coreclr
    on Apr 28, 2026
  4. added and removed
    untriaged
    on Apr 28, 2026
  5. added this to the 11.0.0 milestone on Apr 28, 2026
  6. dotnet-policy-service commented on Apr 28, 2026

    @dotnet-policy-service
    Contributor

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

  7. MihuBot commented on Apr 28, 2026

    @MihuBot

    I'm a bot. Here is a possible related and/or duplicate issue (I may be wrong):

  8. added a commit that references this issue on Apr 28, 2026
    490ce9b
  9. added 2 commits that reference this issue on Apr 28, 2026
    4c86aad
    a96c392
  10. locked and limited conversation to collaborators on May 29, 2026
  11. added a commit that references this issue on Jul 15, 2026
    b32e5f0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrreduce-unsafe

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions