Skip to content

vec::test_drain_range exposes overflow "bug" in slice code #24997

Closed
@pnkfelix

Description

@pnkfelix

While doing a full make check-stage1 on a rust build configured with --enable-debug, I encountered this failure in collectionstest:

---- vec::test_drain_range stdout ----
    thread 'vec::test_drain_range' panicked at 'arithmetic operation overflowed', /Users/fklock/Dev/Mozilla/rust-span_to_lines/src/libcore/slice.rs:639

That line is here in this macro:

macro_rules! slice_offset {
    ($ptr:expr, $by:expr) => {{
        let ptr = $ptr;
        if size_from_ptr(ptr) == 0 {
            transmute(ptr as usize + $by) // <=== this line
        } else {
            ptr.offset($by)
        }
    }};
}

According to preliminary analysis, this arises because of this code elsewhere in slice:

                        self.end = slice_offset!(self.end, -1);

The -1 there is going to get interpreted as a usize, and thus its going to cause the overflow check to fire.

Note that there is no actual bug being caught in this case; in fact the cases where it is signaling overflow (namely where self.end is nonzero) are exactly the only cases that should be treated as "working."


Anyway, an easy short-term fix for this is to introduce distinct slice_add_offset! and slice_sub_offset! macros that both take non-negative input values. I'm testing that change locally now.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions