Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LinkedList::remove forgets the allocator #125950

Closed
xTachyon opened this issue Jun 4, 2024 · 1 comment · Fixed by #125982
Closed

LinkedList::remove forgets the allocator #125950

xTachyon opened this issue Jun 4, 2024 · 1 comment · Fixed by #125982
Assignees
Labels
A-allocators Area: Custom and system allocators A-collections Area: `std::collection` C-bug Category: This is a bug. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@xTachyon
Copy link
Contributor

xTachyon commented Jun 4, 2024

I tried this code:

#![feature(allocator_api)]
#![feature(linked_list_remove)]

use std::{
    alloc::{AllocError, Allocator, Layout},
    collections::LinkedList,
    process::abort,
    ptr::NonNull,
};

struct M;

unsafe impl Allocator for M {
    fn allocate(&self, layout: std::alloc::Layout) -> Result<NonNull<[u8]>, AllocError> {
        unsafe {
            if layout.align() > std::mem::align_of::<usize>() {
                abort();
            }
            let ptr = libc::malloc(layout.size()) as *mut u8;
            if ptr.is_null() {
                abort();
            }
            println!("alloc: {:?}", ptr);
            let nonnull = NonNull::new_unchecked(ptr);
            Ok(NonNull::slice_from_raw_parts(nonnull, layout.size()))
        }
    }

    unsafe fn deallocate(&self, ptr: NonNull<u8>, _layout: Layout) {
        unsafe {
            println!("dealloc: {:?}", ptr);
            libc::free(ptr.as_ptr() as _);
        }
    }
}

fn main() {
    let alloc = &M;
    let mut list = LinkedList::new_in(alloc);
    list.push_back(5);
    list.remove(0);
}

I expected to see this happen: alloc and dealloc is printed

Instead, this happened: only alloc is printed

Meta

rustc --version --verbose:

rustc 1.80.0-nightly (f67a1acc0 2024-06-01)
binary: rustc
commit-hash: f67a1acc04c7ecbf05751b17592dd8d245b75256
commit-date: 2024-06-01
host: x86_64-unknown-linux-gnu
release: 1.80.0-nightly
LLVM version: 18.1.6

LinkedList::remove forgets that it's been initialized with an allocator, and always deletes the node with the global allocator.

let unlinked_node = Box::from_raw(unlinked_node.as_ptr());

There seems to be one more place where this happens in the same file.

@xTachyon xTachyon added the C-bug Category: This is a bug. label Jun 4, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jun 4, 2024
@xTachyon
Copy link
Contributor Author

xTachyon commented Jun 4, 2024

@rustbot claim

@fmease fmease added A-allocators Area: Custom and system allocators A-collections Area: `std::collection` T-libs Relevant to the library team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jun 4, 2024
@bors bors closed this as completed in 448159c Jun 5, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jun 5, 2024
Rollup merge of rust-lang#125982 - xTachyon:fix-linked-list, r=jhpratt

Make deleting on LinkedList aware of the allocator

Fixed rust-lang#125950
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-allocators Area: Custom and system allocators A-collections Area: `std::collection` C-bug Category: This is a bug. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants