Skip to content

fix(buffer): keep map_each_in_place output aligned to the mapped type - #10282

Closed
jackylee-ch wants to merge 1 commit into
vortex-data:developfrom
jackylee-ch:fix/map-each-in-place-alignment
Closed

jackylee-ch wants to merge 1 commit into
vortex-data:developfrom
jackylee-ch:fix/map-each-in-place-alignment

Conversation

@jackylee-ch

Copy link
Copy Markdown
Contributor

What

map_each_in_place transmuted BufferMut<T> into BufferMut<R> but kept T's
alignment. Mapping to a more strictly aligned type (e.g. [u8; 4] → u32) left
the result claiming the weaker alignment, so reading it back as R was UB.

Fix

After mapping, raise the alignment to Alignment::of::<R>() via the existing
aligned helper — metadata-only when the data is already aligned, so the common
same-alignment path is unchanged. This requires R: Copy, which every caller
already satisfies.

The regression test is the one from the issue.

Fixes #9746

AI assistance

Prepared with AI assistance; I have reviewed and verified the change.

`map_each_in_place` transmuted `BufferMut<T>` to `BufferMut<R>` while keeping
`T`'s alignment, so mapping to a type with stricter alignment (e.g. `[u8; 4]`
to `u32`) produced an under-aligned buffer, and reading it back as `R` was UB.

Raise the alignment to `Alignment::of::<R>()` after mapping, reusing `aligned`
(metadata-only when the data is already aligned). This requires `R: Copy`,
which every caller already satisfies.

The regression test is taken from the issue.

Fixes vortex-data#9746

Signed-off-by: jackylee-ch <qcsd2011@gmail.com>
@robert3005

Copy link
Copy Markdown
Contributor

This function is going to get removed... it's used in one place and we have better versions of this logic

@jackylee-ch

Copy link
Copy Markdown
Contributor Author

Thanks for flagging. This fixes the under-alignment UB from #9746 (filed with a failing test), so it closes that soundness hole while map_each_in_place is still around. Happy to close this if the removal is imminent or you'd rather fold the fix into that work — just let me know.

@jackylee-ch

Copy link
Copy Markdown
Contributor Author

map_each_in_place is slated for removal with better replacements, so I'll close this rather than point-patch a function on its way out. The under-alignment UB stays tracked in #9746 for the replacement to cover. Thanks for the context!

@jackylee-ch jackylee-ch closed this Oct 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

map_each_in_place doesn't check or increase alignment

2 participants