Skip to content
New issue

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

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

Already on GitHub? # to your account

alloc::realloc does not follow requirements of Layout::from_size_align_unchecked #108546

Closed
overlookmotel opened this issue Feb 28, 2023 · 3 comments · Fixed by #108630
Closed
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools C-bug Category: This is a bug. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@overlookmotel
Copy link
Contributor

The docs for Layout::from_size_align_unchecked state that:

size, when rounded up to the nearest multiple of align, must not overflow isize (i.e., the rounded value must be less than or equal to isize::MAX)

alloc::realloc's docs state a different constraint:

new_size, when rounded up to the nearest multiple of layout.align(), must not overflow (i.e., the rounded value must be less than usize::MAX).

So this would be legal according to alloc::realloc's docs:

let layout = alloc::Layout::from_size_align(1, 1);
let ptr1 = alloc::alloc(layout);
let ptr2 = alloc::realloc(ptr1, layout, usize::MAX);

But this results in a call internally:

Layout::from_size_align_unchecked(usize::MAX, 1)

...which is meant to be UB.

I assume this is just that the docs are out of date after #95295.

@overlookmotel overlookmotel added the C-bug Category: This is a bug. label Feb 28, 2023
@overlookmotel overlookmotel changed the title alloc::realloc does not follow requirements of Layout:: from_size_align_unchecked alloc::realloc does not follow requirements of Layout::from_size_align_unchecked Feb 28, 2023
@thomcc thomcc added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools labels Feb 28, 2023
@thomcc
Copy link
Member

thomcc commented Feb 28, 2023

Yes, this seems like a documentation bug to me.

overlookmotel added a commit to overlookmotel/rust that referenced this issue Mar 1, 2023
@cuviper
Copy link
Member

cuviper commented Mar 2, 2023

This was noted in #95295 (comment), discussed at length, and deferred in #95295 (comment): "This PR does not make a decision about realloc." There is also an ACP: rust-lang/libs-team#61

@overlookmotel
Copy link
Contributor Author

Apologies. I didn't read the whole (lengthy) thread. Just noticed the discrepancy in the docs, and was thoroughly confused why they didn't correspond.

Closing this in favour of rust-lang/libs-team#61

I'll leave the PR open as I see you've tagged it "waiting for team", but feel free to just close it if you prefer. It's a trivial change anyway.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue May 27, 2023
…Amanieu

Fix docs for `alloc::realloc`

Fixes rust-lang#108546.

Corrects the docs for `alloc::realloc` to bring the safety constraints into line with `Layout::from_size_align_unchecked`'s constraints.
xobs pushed a commit to betrusted-io/rust that referenced this issue Aug 5, 2023
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools C-bug Category: This is a bug. T-libs-api Relevant to the library API 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