Skip to content

Bounds check not elided for slice::split_at after loop #143027

Open
@okaneco

Description

@okaneco

split_at panics when mid > len.

In the following code for trimming the start of a slice, i will only be less than or equal to len but the information seems lost after the loop.

#[inline(never)]
pub fn src(s: &[u8]) -> &[u8] {
    let mut i = 0;
    while i < s.len() {
        if s[i] != 0 {
            break;
        }
        i += 1;
    }
    s.split_at(i).1
}

#[inline(never)]
pub const fn tgt(s: &[u8]) -> &[u8] {
    let mut i = 0;
    while i < s.len() {
        if s[i] != 0 {
            return s.split_at(i).1;
        }
        i += 1;
    }
    &[]
}

https://rust.godbolt.org/z/EWPbf4Pdd

Assembly outputsrc function

example::src::he3649242b6e21a77:
        xor     eax, eax
        test    rsi, rsi
        je      .LBB0_6
.LBB0_1:
        cmp     byte ptr [rdi + rax], 0
        jne     .LBB0_5
        inc     rax
        cmp     rsi, rax
        jne     .LBB0_1
        mov     rax, rsi
        jmp     .LBB0_6
.LBB0_5:
        cmp     rax, rsi
        ja      .LBB0_4
.LBB0_6:
        add     rdi, rax
        sub     rsi, rax
        mov     rax, rdi
        mov     rdx, rsi
        ret
.LBB0_4:
        sub     rsp, 56
        lea     rax, [rip + .Lanon.7d93f8663e2093497ca272392f600a6e.1]
        mov     qword ptr [rsp + 8], rax
        mov     qword ptr [rsp + 16], 1
        mov     qword ptr [rsp + 24], 8
        xorps   xmm0, xmm0
        movups  xmmword ptr [rsp + 32], xmm0
        lea     rsi, [rip + .Lanon.7d93f8663e2093497ca272392f600a6e.3]
        lea     rdi, [rsp + 8]
        call    qword ptr [rip + core::panicking::panic_fmt::hf1310915e65e5ab4@GOTPCREL]

.Lanon.7d93f8663e2093497ca272392f600a6e.0:
        .ascii  "mid > len"

.Lanon.7d93f8663e2093497ca272392f600a6e.1:
        .quad   .Lanon.7d93f8663e2093497ca272392f600a6e.0
        .asciz  "\t\000\000\000\000\000\000"

.Lanon.7d93f8663e2093497ca272392f600a6e.2:
        .asciz  "/app/example.rs"

.Lanon.7d93f8663e2093497ca272392f600a6e.3:
        .quad   .Lanon.7d93f8663e2093497ca272392f600a6e.2
        .asciz  "\020\000\000\000\000\000\000\000\n\000\000\000\007\000\000"

tgt function

example::tgt::hfc7b3a5f713674dc:
        mov     eax, 1
        test    rsi, rsi
        je      .LBB0_1
        mov     rdx, rsi
.LBB0_4:
        cmp     byte ptr [rdi], 0
        jne     .LBB0_5
        inc     rdi
        dec     rdx
        jne     .LBB0_4
.LBB0_1:
        xor     edx, edx
        ret
.LBB0_5:
        mov     rax, rdi
        ret

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions