Skip to content

Commit 03320c1

Browse files
committed
fix: odd utf16 input length in legacyHookDecode
1 parent 51bc0b2 commit 03320c1

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

fallback/encoding.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,18 @@ export function legacyHookDecode(input, fallbackEncoding) {
265265
const bomEncoding = getBOMEncoding(u8)
266266
if (bomEncoding) u8 = u8.subarray(bomEncoding === 'utf-8' ? 3 : 2)
267267
const enc = bomEncoding ?? fallbackEncoding ?? 'utf-8' // "the byte order mark is more authoritative than anything else"
268+
268269
if (enc === 'utf-8') return utf8toStringLoose(u8)
269-
if (enc === 'utf-16le') return utf16toStringLoose(u8, 'uint8-le')
270-
if (enc === 'utf-16be') return utf16toStringLoose(u8, 'uint8-be')
270+
if (enc === 'utf-16le' || enc === 'utf-16be') {
271+
let suffix = ''
272+
if (u8.byteLength % 2 !== 0) {
273+
suffix = replacementChar
274+
u8 = u8.subarray(0, -1)
275+
}
276+
277+
return utf16toStringLoose(u8, enc === 'utf-16le' ? 'uint8-le' : 'uint8-be') + suffix
278+
}
279+
271280
if (!Object.hasOwn(labels, enc)) throw new RangeError(E_ENCODING)
272281

273282
if (multibyteSet.has(enc)) {

0 commit comments

Comments
 (0)