SIGN IN SIGN UP
oven-sh / bun UNCLAIMED

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one

0 0 150 Rust

encoding: don't corrupt encodeInto output when a valid astral char doesn't fit (#31532)

`new TextEncoder().encodeInto("😀", new Uint8Array(3))` — a valid 4-byte
astral character into a 3-byte buffer — should leave the buffer
untouched and return `{read:0, written:0}` per the WHATWG Encoding spec
(it doesn't fit). Bun instead wrote `EF BF BD` (U+FFFD) and returned
`{read:1, written:3}`, corrupting the output and orphaning the surrogate
pair's low half so a resume loop re-replaced it.

`encodeInto16` had a fallback that wrote U+FFFD and reported `{1,3}`
whenever `copy_utf16_into_utf8` returned `{0,0}` for a ≥3-byte buffer —
intended for unpaired surrogates. But `copy_utf16_into_utf8` already
emits U+FFFD for a lone surrogate when it fits (≥3 bytes → `{1,3}`), so
the fallback only ever fired for valid input whose leading 4-byte char
doesn't fit — exactly the case it corrupted. Remove it.

Matches Node across the matrix: astral-no-fit → `{0,0}` untouched,
astral-fits → `{2,4}`, lone surrogate → `{1,3}` U+FFFD, `"a😀"` partial →
`{1,1}`. Also fixes a pre-existing test that mislabeled a *valid*
surrogate pair (`0xD800,0xDC00` = U+10000) as "unpaired" and asserted
the old buggy output. Not a port regression — 1.3.14 has the same
fallback.

---------

Co-authored-by: robobun <117481402+robobun@users.noreply.github.com>
A
Alistair Smith committed
5ba2f652d204293e0f1a9a55dfada8ed73f7b7bf
Parent: 973d1de
Committed by GitHub <noreply@github.com> on 5/29/2026, 2:24:40 AM