diff options
author | 2023-09-12 08:27:12 +0800 | |
---|---|---|
committer | 2023-09-11 17:27:12 -0700 | |
commit | c4507a5db33910258278bd6641dcbe34ab33628e (patch) | |
tree | 4a2ea5b4e27ff4be9a24284a1ed8b18eee01a3e9 | |
parent | c9a0ea96cd13df39fdfc4c1ebb897453517718f3 (diff) | |
download | bun-c4507a5db33910258278bd6641dcbe34ab33628e.tar.gz bun-c4507a5db33910258278bd6641dcbe34ab33628e.tar.zst bun-c4507a5db33910258278bd6641dcbe34ab33628e.zip |
Fix `Buffer.from` to handle double-byte hex encoding strings (#4933)
Close: #4919
-rw-r--r-- | src/bun.js/bindings/JSBuffer.cpp | 1 | ||||
-rw-r--r-- | test/js/node/buffer.test.js | 13 |
2 files changed, 14 insertions, 0 deletions
diff --git a/src/bun.js/bindings/JSBuffer.cpp b/src/bun.js/bindings/JSBuffer.cpp index ad901b0e4..9227e47b3 100644 --- a/src/bun.js/bindings/JSBuffer.cpp +++ b/src/bun.js/bindings/JSBuffer.cpp @@ -351,6 +351,7 @@ static EncodedJSValue constructFromEncoding(JSGlobalObject* lexicalGlobalObject, case WebCore::BufferEncodingType::utf8: case WebCore::BufferEncodingType::base64: case WebCore::BufferEncodingType::base64url: + case WebCore::BufferEncodingType::hex: case WebCore::BufferEncodingType::ascii: case WebCore::BufferEncodingType::latin1: { result = Bun__encoding__constructFromUTF16(lexicalGlobalObject, view.characters16(), view.length(), static_cast<uint8_t>(encoding)); diff --git a/test/js/node/buffer.test.js b/test/js/node/buffer.test.js index 7c3d16536..afc9cdee8 100644 --- a/test/js/node/buffer.test.js +++ b/test/js/node/buffer.test.js @@ -2572,3 +2572,16 @@ it("construct buffer from UTF16, issue #3914", () => { const buf = Buffer.from(str, "latin1"); expect(buf).toStrictEqual(raw); }); + +it("construct buffer from hex, issue #4919", () => { + const data = "测试63e9f6c4b04fa8c80f3fb0ee"; + + const slice1 = data.substring(0, 2); + const slice2 = data.substring(2); + + const buf1 = Buffer.from(slice1, "hex"); + const buf2 = Buffer.from(slice2, "hex"); + + expect(buf1).toStrictEqual(Buffer.from([])); + expect(buf2).toStrictEqual(Buffer.from([0x63, 0xe9, 0xf6, 0xc4, 0xb0, 0x4f, 0xa8, 0xc8, 0x0f, 0x3f, 0xb0, 0xee])); +}); |