Skip to content

Commit 09cacd7

Browse files
jasnelltargos
authored andcommitted
src: fix setting Converter sub char length
Signed-off-by: James M Snell <[email protected]> Fixes: #38330 PR-URL: #38331 Reviewed-By: Juan José Arboleda <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent 3649ec5 commit 09cacd7

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/node_i18n.cc

+12-2
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,13 @@ MaybeLocal<Object> Transcode(Environment* env,
148148
*status = U_ZERO_ERROR;
149149
MaybeLocal<Object> ret;
150150
MaybeStackBuffer<char> result;
151-
Converter to(toEncoding, "?");
151+
Converter to(toEncoding);
152152
Converter from(fromEncoding);
153+
154+
size_t sublen = ucnv_getMinCharSize(to.conv());
155+
std::string sub(sublen, '?');
156+
to.set_subst_chars(sub.c_str());
157+
153158
const uint32_t limit = source_length * to.max_char_size();
154159
result.AllocateSufficientStorage(limit);
155160
char* target = *result;
@@ -190,7 +195,12 @@ MaybeLocal<Object> TranscodeFromUcs2(Environment* env,
190195
*status = U_ZERO_ERROR;
191196
MaybeStackBuffer<UChar> sourcebuf;
192197
MaybeLocal<Object> ret;
193-
Converter to(toEncoding, "?");
198+
Converter to(toEncoding);
199+
200+
size_t sublen = ucnv_getMinCharSize(to.conv());
201+
std::string sub(sublen, '?');
202+
to.set_subst_chars(sub.c_str());
203+
194204
const size_t length_in_chars = source_length / sizeof(UChar);
195205
CopySourceBuffer(&sourcebuf, source, source_length, length_in_chars);
196206
MaybeStackBuffer<char> destbuf(length_in_chars);

test/parallel/test-icu-transcode.js

+5
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,8 @@ assert.deepStrictEqual(
8383
const dest = buffer.transcode(new Uint8Array(), 'utf8', 'latin1');
8484
assert.strictEqual(dest.length, 0);
8585
}
86+
87+
// Test that it doesn't crash
88+
{
89+
buffer.transcode(new buffer.SlowBuffer(1), 'utf16le', 'ucs2');
90+
}

0 commit comments

Comments
 (0)