From d177541b0efadf5bb62809ce1f3454303323d314 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Wed, 21 Apr 2021 08:23:55 -0700 Subject: [PATCH] src: fix setting Converter sub char length MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: James M Snell Fixes: https://github.com/nodejs/node/issues/38330 PR-URL: https://github.com/nodejs/node/pull/38331 Reviewed-By: Juan José Arboleda Reviewed-By: Benjamin Gruenbaum --- src/node_i18n.cc | 14 ++++++++++++-- test/parallel/test-icu-transcode.js | 5 +++++ 2 files changed, 17 insertions(+), 2 deletions(-) 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'); +}