Skip to content

Impossibly large allocations fails but still allocates new memory pages #100

@yvt

Description

@yvt

Describe the Bug

Allocating usize::MAX - 8 bytes fails but allocates new memory pages every time.

Steps to Reproduce

#[test]
fn cannot_alloc_max_usize_m8() {
    let a = &wee_alloc::WeeAlloc::INIT;
    let layout = Layout::from_size_align(std::usize::MAX - 8, 1)
        .expect("should be able to create a `Layout` with size = std::usize::MAX - 8");
    for _ in 0..10000000 {
        let result = unsafe { a.alloc(layout) };
        assert!(result.is_err());
    }
}

Expected Behavior

The test should complete without causing OOM.

Actual Behavior

With debug assertions: thread 'cannot_alloc_max_usize_m8' panicked at 'attempt to add with overflow', .../.cargo/registry/src/gitproxy.zycloud.tk-1ecc6299db9ec823/memory_units-0.4.0/src/lib.rs:166:1

Without debug assertions: The test allocates tens of gigabytes of memory and eventually gets killed by the kernel.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions