Skip to content

fix: handle unaligned two-byte string searches - #23

Open
inoway46 wants to merge 1 commit into
nodejs:mainfrom
inoway46:fix-unaligned-two-byte-search
Open

inoway46 wants to merge 1 commit into
nodejs:mainfrom
inoway46:fix-unaligned-two-byte-search

Conversation

@inoway46

@inoway46 inoway46 commented Dec 4, 2026

Copy link
Copy Markdown

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 memcpy and 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 explicit abort() in place of Node.js's CHECK macro.

All 9 CTest checks pass on macOS arm64 in Release builds with and without UBSan.

Refs: nodejs/node#65959

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>
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