Skip to content

Fix undefined behavior in UTF-16 parsing from unaligned buffers - #62

Open
natecook1000 wants to merge 2 commits into
mainfrom
fix/utf16-unaligned-load
Open

Fix undefined behavior in UTF-16 parsing from unaligned buffers#62
natecook1000 wants to merge 2 commits into
mainfrom
fix/utf16-unaligned-load

Conversation

@natecook1000

Copy link
Copy Markdown
Member

URBP.assumingMemoryBound(to:) requires that the buffer is UInt16- aligned, which doesn't necessarily hold when reading a binary file. This change adds an _UnalignedUnsafeBufferPointer and uses it when the memory to be parsed as a UTF16 string is out of alignment with UInt16.

This will go through a slow path in UTF16 -> UTF8 string initialization (since the new unaligned buffer isn't one of the always-specialized types) so we should benchmark this change.

Checklist

  • I've added at least one test that validates that my change is working, if appropriate
  • I've followed the code style of the rest of the project
  • I've run Scripts/format.sh to correctly format my change
  • I've read the Contribution Guidelines
  • I've updated the documentation if necessary

URBP.assumingMemoryBound(to:) requires that the buffer is UInt16-
aligned, which doesn't necessarily hold when reading a binary file.
This change adds an `_UnalignedUnsafeBufferPointer` and uses it
when the memory to be parsed as a UTF16 string is out of alignment
with `UInt16`.
@natecook1000
natecook1000 force-pushed the fix/utf16-unaligned-load branch from 4663706 to 5a00bb2 Compare August 18, 2026 18:39
@natecook1000 natecook1000 reopened this Aug 21, 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.

1 participant