Ensure from_box works if boxed.addr() == 1 #52
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
SmallBox::from_box will reuse the allocation from its argument as ptr in smallbox. However, said allocation might have an address of 1, which is the INLINE_SENTINAL value.
This would cause the resulting smallbox to read from its uninitialized inline storage space when de-referenced.
Note that this problem likely cannot be triggered on a desktop OS since the first page is typically set up to segfault when read. Thus if the type is not a ZST, then the allocator won't have given out a pointer in the first page, and if the type is a ZST, then reading from the uninitialized storage space is perfectly fine behavior.
However, bare metal programs could conceivably run into this problem, so this commit causes from_box to copy from boxed into a newly created smallbox if boxed.addr() == INLINE_SENTINEL.