From 385d440694a95bc4bfee25daa602c5c4363b6423 Mon Sep 17 00:00:00 2001 From: Ai Hoshino Date: Thu, 10 Aug 2023 00:45:50 +0800 Subject: Fix constructing buffer from a UTF16 string with the Latin1 encoding. (#4086) Close: #3914 --- test/js/node/buffer.test.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'test/js/node/buffer.test.js') diff --git a/test/js/node/buffer.test.js b/test/js/node/buffer.test.js index 129ceb02b..834536020 100644 --- a/test/js/node/buffer.test.js +++ b/test/js/node/buffer.test.js @@ -2552,3 +2552,15 @@ it("write alias", () => { shouldBeSame("writeBigUint64BE", "writeBigUInt64BE", BigInt(1000)); shouldBeSame("writeBigUint64LE", "writeBigUInt64LE", BigInt(1000)); }); + +it("construct buffer from UTF16, issue #3914", () => { + const raw = Buffer.from([0, 104, 0, 101, 0, 108, 0, 108, 0, 111]); + const data = new Uint16Array(raw); + + const decoder = new TextDecoder("UTF-16"); + const str = decoder.decode(data); + expect(str).toStrictEqual("\x00h\x00e\x00l\x00l\x00o"); + + const buf = Buffer.from(str, "latin1"); + expect(buf).toStrictEqual(raw); +}); -- cgit v1.2.3