Repository navigation
Conversation
A Buffer view can start at an odd byte offset. Reading a code unit through a uint16_t pointer then requires alignment the view may lack. Rounding a byte hit down to an absolute address can also put it in the wrong code unit, causing a search to hang or miss a match. Read code units with memcpy and calculate the index of each byte hit from its offset within the haystack. Test forward and reverse searches with the haystack and needle aligned independently. Refs: nodejs/node#65968 Co-authored-by: Paul Bouchon <mail@bitpshr.net>
inoway46
force-pushed
the
fix-unaligned-two-byte-search
branch
from
December 4, 2026 13:26
82bd3e8 to
67ccb73
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This ports @bitpshr's fix from nodejs/node#65968 to nbytes. A Node.js Buffer view can start at an odd byte offset. In that case a two-byte search can hang or miss a match.
Read code units with
memcpyand calculate the index of each byte hit relative to the start of the haystack. The regression test covers forward and reverse searches with independently aligned or unaligned haystacks and needles. It also exercises the search template outside Node.js, so the constructor uses an explicitabort()in place of Node.js'sCHECKmacro.All 9 CTest checks pass on macOS arm64 in Release builds with and without UBSan.
Refs: nodejs/node#65959