Skip to content

Commit c650864

Browse files
addaleaxJonathan Darling
authored and
Jonathan Darling
committed
buffer: fix transcode for single-byte enc to ucs2
Fix `buffer.transcode()` for transcoding from single-byte character encodings to UCS2. These would previously perform out-of-bounds reads due to an incorrect `sizeof()` expression. Fixes: nodejs#9834 PR-URL: nodejs#9838 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Trevor Norris <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 4125d59 commit c650864

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/node_i18n.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ MaybeLocal<Object> TranscodeToUcs2(Isolate* isolate,
179179
MaybeLocal<Object> ret;
180180
MaybeStackBuffer<UChar> destbuf(source_length);
181181
Converter from(fromEncoding);
182-
const size_t length_in_chars = source_length * sizeof(*destbuf);
182+
const size_t length_in_chars = source_length * sizeof(UChar);
183183
ucnv_toUChars(from.conv, *destbuf, length_in_chars,
184184
source, source_length, status);
185185
if (U_SUCCESS(*status))

test/parallel/test-icu-transcode.js

+10
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,13 @@ assert.throws(
4646
() => buffer.transcode(Buffer.from('a'), 'uf8', 'b'),
4747
/Unable to transcode Buffer \[U_ILLEGAL_ARGUMENT_ERROR\]/
4848
);
49+
50+
assert.deepStrictEqual(
51+
buffer.transcode(Buffer.from('hi', 'ascii'), 'ascii', 'utf16le'),
52+
Buffer.from('hi', 'utf16le'));
53+
assert.deepStrictEqual(
54+
buffer.transcode(Buffer.from('hi', 'latin1'), 'latin1', 'utf16le'),
55+
Buffer.from('hi', 'utf16le'));
56+
assert.deepStrictEqual(
57+
buffer.transcode(Buffer.from('hä', 'latin1'), 'latin1', 'utf16le'),
58+
Buffer.from('hä', 'utf16le'));

0 commit comments

Comments
 (0)