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

Wrong ptr dereferencing in Alloc::alloc_one #42827

Closed
lseugene opened this issue Jun 22, 2017 · 6 comments
Closed

Wrong ptr dereferencing in Alloc::alloc_one #42827

lseugene opened this issue Jun 22, 2017 · 6 comments
Labels
T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@lseugene
Copy link

lseugene commented Jun 22, 2017

    fn alloc_one<T>(&mut self) -> Result<Unique<T>, AllocErr>
        where Self: Sized
    {
        let k = Layout::new::<T>();
        if k.size() > 0 {
            unsafe { self.alloc(k).map(|p|Unique::new(*p as *mut T)) }
        } else {
            Err(AllocErr::invalid_input("zero-sized type invalid for alloc_one"))
        }
    }

@pnkfelix

@Mark-Simulacrum
Copy link
Member

For context:

fn alloc_one<T>(&mut self) -> Result<Unique<T>, AllocErr>
where Self: Sized
{
let k = Layout::new::<T>();
if k.size() > 0 {
unsafe { self.alloc(k).map(|p|Unique::new(*p as *mut T)) }
} else {
Err(AllocErr::invalid_input("zero-sized type invalid for alloc_one"))
}
}
.

@Mark-Simulacrum Mark-Simulacrum added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Jun 23, 2017
@sfackler
Copy link
Member

Could you clarify what specifically is the problem with that implementation?

@lseugene
Copy link
Author

I guess it's just typo.
We don't need to deref raw pointer p before cast it in Unique::new(*p as *mut T)

@sfackler
Copy link
Member

p isn't a raw pointer - it's a Unique<T>.

@lseugene
Copy link
Author

Underlying alloc:

unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr>;

returns raw pointer *mut u8. alloc_one should just cast it to *mut T and wrap in Unique.
Please see correct implementation in alloc_array:
Unique::new(p as *mut T)

the diff and bug is in Unique::new(*p as *mut T) vs Unique::new(p as *mut T)

@sfackler
Copy link
Member

Oh no, casts u8 to *mut T strike again! :(

alexcrichton added a commit to alexcrichton/rust that referenced this issue Jun 25, 2017
This had an accidental `u8 as *mut T` where it was intended to have just a
normal pointer-to-pointer cast.

Closes rust-lang#42827
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this issue Jun 28, 2017
std: Fix implementation of `Alloc::alloc_one`

This had an accidental `u8 as *mut T` where it was intended to have just a
normal pointer-to-pointer cast.

Closes rust-lang#42827
frewsxcv added a commit to frewsxcv/rust that referenced this issue Jun 29, 2017
std: Fix implementation of `Alloc::alloc_one`

This had an accidental `u8 as *mut T` where it was intended to have just a
normal pointer-to-pointer cast.

Closes rust-lang#42827
arielb1 pushed a commit to arielb1/rust that referenced this issue Jun 29, 2017
std: Fix implementation of `Alloc::alloc_one`

This had an accidental `u8 as *mut T` where it was intended to have just a
normal pointer-to-pointer cast.

Closes rust-lang#42827
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants