diff options
author | 2022-12-05 12:05:16 -0800 | |
---|---|---|
committer | 2022-12-05 12:05:16 -0800 | |
commit | e23832d1ce9dc21265ef627e2d5deedf74642738 (patch) | |
tree | 3c3c94059967495f893c0d1706854ed253951494 /src/bun.js/bindings/JSBuffer.cpp | |
parent | 9665d7d21636b0eb344bc83c104f1796e96809bf (diff) | |
download | bun-e23832d1ce9dc21265ef627e2d5deedf74642738.tar.gz bun-e23832d1ce9dc21265ef627e2d5deedf74642738.tar.zst bun-e23832d1ce9dc21265ef627e2d5deedf74642738.zip |
Fix regression
Diffstat (limited to 'src/bun.js/bindings/JSBuffer.cpp')
-rw-r--r-- | src/bun.js/bindings/JSBuffer.cpp | 17 |
1 files changed, 11 insertions, 6 deletions
diff --git a/src/bun.js/bindings/JSBuffer.cpp b/src/bun.js/bindings/JSBuffer.cpp index b3e5f0b4e..29774f3c5 100644 --- a/src/bun.js/bindings/JSBuffer.cpp +++ b/src/bun.js/bindings/JSBuffer.cpp @@ -1151,12 +1151,17 @@ static inline JSC::EncodedJSValue jsBufferPrototypeFunction_toStringBody(JSC::JS case WebCore::BufferEncodingType::ucs2: case WebCore::BufferEncodingType::utf16le: { UChar* data = nullptr; - size_t u16length = length > 1 ? length / 2 : 1; - auto str = String::createUninitialized(u16length, data); - // always zero out the last byte of the string incase the buffer is not a multiple of 2 - data[u16length - 1] = 0; - memcpy(data, reinterpret_cast<const char*>(castedThis->typedVector() + offset), length); - ret = JSC::JSValue::encode(JSC::jsString(vm, WTFMove(str))); + size_t u16length = length / 2; + if (u16length == 0) { + ret = JSC::JSValue::encode(JSC::jsEmptyString(vm)); + } else { + auto str = String::createUninitialized(u16length, data); + // always zero out the last byte of the string incase the buffer is not a multiple of 2 + data[u16length - 1] = 0; + memcpy(data, reinterpret_cast<const char*>(castedThis->typedVector() + offset), length); + ret = JSC::JSValue::encode(JSC::jsString(vm, WTFMove(str))); + } + break; } |