diff --git a/src/node_i18n.cc b/src/node_i18n.cc index cc2d245b8a77d9..3d36e4677c9ca9 100644 --- a/src/node_i18n.cc +++ b/src/node_i18n.cc @@ -147,8 +147,13 @@ MaybeLocal Transcode(Environment* env, *status = U_ZERO_ERROR; MaybeLocal ret; MaybeStackBuffer result; - Converter to(toEncoding, "?"); + Converter to(toEncoding); Converter from(fromEncoding); + + size_t sublen = ucnv_getMinCharSize(to.conv()); + std::string sub(sublen, '?'); + to.set_subst_chars(sub.c_str()); + const uint32_t limit = source_length * to.max_char_size(); result.AllocateSufficientStorage(limit); char* target = *result; @@ -189,7 +194,12 @@ MaybeLocal TranscodeFromUcs2(Environment* env, *status = U_ZERO_ERROR; MaybeStackBuffer sourcebuf; MaybeLocal ret; - Converter to(toEncoding, "?"); + Converter to(toEncoding); + + size_t sublen = ucnv_getMinCharSize(to.conv()); + std::string sub(sublen, '?'); + to.set_subst_chars(sub.c_str()); + const size_t length_in_chars = source_length / sizeof(UChar); CopySourceBuffer(&sourcebuf, source, source_length, length_in_chars); MaybeStackBuffer destbuf(length_in_chars); diff --git a/test/parallel/test-icu-transcode.js b/test/parallel/test-icu-transcode.js index 20ecab7213b292..e9aced128eec21 100644 --- a/test/parallel/test-icu-transcode.js +++ b/test/parallel/test-icu-transcode.js @@ -83,3 +83,8 @@ assert.deepStrictEqual( const dest = buffer.transcode(new Uint8Array(), 'utf8', 'latin1'); assert.strictEqual(dest.length, 0); } + +// Test that it doesn't crash +{ + buffer.transcode(new buffer.SlowBuffer(1), 'utf16le', 'ucs2'); +}