Skip to content

Fixup a couple ucs2 alignment issues#6171

Merged
jasnell merged 1 commit intomainfrom
jasnell/fixup-ucs2-alignment-issues
Feb 25, 2026
Merged

Fixup a couple ucs2 alignment issues#6171
jasnell merged 1 commit intomainfrom
jasnell/fixup-ucs2-alignment-issues

Conversation

@jasnell
Copy link
Copy Markdown
Collaborator

@jasnell jasnell commented Feb 25, 2026

No description provided.

@jasnell jasnell requested a review from anonrig February 25, 2026 03:46
@jasnell jasnell requested review from a team as code owners February 25, 2026 03:46
@ask-bonk
Copy link
Copy Markdown
Contributor

ask-bonk bot commented Feb 25, 2026

OK — many tests omit compatibilityDate, which defaults to the oldest. That's normal for this directory.

Now let me do a final check — the textdecoder-utf16-odd-offset-test.wd-test sets nodejs_compat_v2 but the test only uses TextDecoder, not a Node API. This isn't wrong (it will still work), but it's unnecessary. However, it's not an actionable issue worth flagging.

Let me now review the complete picture. Here's my analysis:

encoding.c++: The fix replaces an unsafe reinterpret_cast<const char16_t*> on a potentially unaligned buffer with a copy into a kj::SmallArray<char16_t, 256>. The inline buffer of 256 char16_t elements (512 bytes) handles typical cases without heap allocation. Correct.

buffer.c++ (indexOfBuffer):

  1. Rounds down hayStackLength for UTF16LE to ensure even byte count.
  2. Copies haystack and needle into aligned kj::SmallArray<uint16_t, 1024> buffers (2048 bytes inline).
  3. The needleLen = needle.size() & ~1 rounds needle size down similarly.
  4. All comparisons consistently use hayStackLength instead of hayStack.size().
  5. Correct.

buffer.c++ (indexOfString):

  1. Similarly rounds down hayStackLength.
  2. Copies haystack into aligned buffer.
  3. The decodedNeedle comes from decodeStringImpl which allocates fresh via V8's backing store allocator — this is guaranteed to be properly aligned, so the reinterpret_cast<const uint16_t*> on line 417 is safe.
  4. Correct.

Tests: Properly test the exact scenarios — odd byte offsets for both TextDecoder and Buffer.indexOf with UCS-2/UTF-16LE encoding.

Backward compatibility: This is a pure bug fix (fixing UB from alignment violations). The behavioral change is going from crashing/UB to correct behavior, which doesn't need a compat flag.

I don't see any actionable issues with this PR. The alignment fixes are correct, the approach is sound (copy to aligned buffer), and the tests cover the scenarios.

LGTM

github run

@jasnell jasnell enabled auto-merge February 25, 2026 03:51
@jasnell jasnell merged commit 522277a into main Feb 25, 2026
25 of 26 checks passed
@jasnell jasnell deleted the jasnell/fixup-ucs2-alignment-issues branch February 25, 2026 05:22
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.

2 participants