mirror of
https://github.com/oven-sh/bun
synced 2026-02-10 10:58:56 +00:00
## Summary - Fixes a panic: "exact division produced remainder" that occurs when reading files with odd number of bytes using utf16le/ucs2 encoding - The crash happened in `encoding.zig:136` when `std.mem.bytesAsSlice(u16, input)` was called on a byte slice with odd length - Fixed by properly checking for odd-length input and truncating to the nearest even length ## Test plan - Added regression tests in `test/regression/issue/utf16-encoding-crash.test.ts` - Tests verify that reading files with odd byte counts doesn't crash - Tests verify correct truncation behavior matches Node.js expectations - Verified edge cases (0, 1 byte inputs) return empty strings ## Root Cause The original code checked `if (input.len / 2 == 0)` which only caught 0 and 1-byte inputs, but `std.mem.bytesAsSlice(u16, input)` panics on any odd-length input (3, 5, 7, etc. bytes). ## Fix Details - Changed condition to check `input.len % 2 != 0` for any odd length - Truncate odd-length inputs to the nearest even length for valid UTF-16 processing - Handle edge cases by returning empty string for 0 or 1-byte inputs 🤖 Generated with [Claude Code](https://claude.ai/code) --------- Co-authored-by: Claude Bot <claude-bot@bun.sh> Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Jarred Sumner <jarred@jarredsumner.com> Co-authored-by: Dylan Conway <dylan.conway567@gmail.com>
2.0 KiB
2.0 KiB