Skip to content

Undefined behavior in split_at_unchecked #141425

@Lysxia

Description

@Lysxia

split_at_unchecked calls ptr.add(mid) which may be UB if mid is the length of the slice. Making it ptr.wrapping_add(mid) or changing the precondition to mid < len would fix this.

let len = self.len();
let ptr = self.as_ptr();
assert_unsafe_precondition!(
check_library_ub,
"slice::split_at_unchecked requires the index to be within the slice",
(mid: usize = mid, len: usize = len) => mid <= len,
);
// SAFETY: Caller has to check that `0 <= mid <= self.len()`
unsafe { (from_raw_parts(ptr, mid), from_raw_parts(ptr.add(mid), unchecked_sub(len, mid))) }

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-discussionCategory: Discussion or questions that doesn't represent real issues.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions