Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: ensure TextDecoder only removes utf8 BOM on utf8 encoding #42779

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/internal/encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ function makeTextDecoderJS() {
this[kHandle].write(input);

if (result.length > 0 &&
this[kEncoding] === 'utf-8' &&
!this[kBOMSeen] &&
!(this[kFlags] & CONVERTER_FLAGS_IGNORE_BOM)) {
// If the very first result in the stream is a BOM, and we are not
Expand Down
15 changes: 15 additions & 0 deletions test/parallel/test-whatwg-encoding-custom-textdecoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,21 @@ assert(TextDecoder);
});
}

// Test TextDecoder, UTF-16LE, fatal: false, ignoreBOM: false
{
['utf-16', 'utf-16le'].forEach((i) => {
// This is a utf16le buffer with a utf8 BOM,
// which should not be removed
const buf = Buffer.from([0xef, 0xbb, 0xbf, 0x74, 0x00, 0x65,
0x00, 0x73, 0x00, 0x74, 0x00, 0xac,
0x20], 'utf16le');
const dec = new TextDecoder(i);
assert.strictEqual(dec.encoding, 'utf-16le');
const res = dec.decode(buf);
assert.strictEqual(res, '믯璿攀猀琀가');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I tried decoding this on current node 16, the bug shows itself in an invalid character at the end: '믯璿攀猀琀가�'

});
}

// Test TextDecoder, UTF-8, fatal: false, ignoreBOM: true
{
['unicode-1-1-utf-8', 'utf8', 'utf-8'].forEach((i) => {
Expand Down