Skip to content

Commit

Permalink
src: fix setting Converter sub char length
Browse files Browse the repository at this point in the history
Signed-off-by: James M Snell <jasnell@gmail.com>
Fixes: #38330

PR-URL: #38331
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
  • Loading branch information
jasnell authored and targos committed Jun 11, 2021
1 parent e279b02 commit d177541
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/node_i18n.cc
Expand Up @@ -147,8 +147,13 @@ MaybeLocal<Object> Transcode(Environment* env,
*status = U_ZERO_ERROR;
MaybeLocal<Object> ret;
MaybeStackBuffer<char> 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;
Expand Down Expand Up @@ -189,7 +194,12 @@ MaybeLocal<Object> TranscodeFromUcs2(Environment* env,
*status = U_ZERO_ERROR;
MaybeStackBuffer<UChar> sourcebuf;
MaybeLocal<Object> 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<char> destbuf(length_in_chars);
Expand Down
5 changes: 5 additions & 0 deletions test/parallel/test-icu-transcode.js
Expand Up @@ -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');
}

0 comments on commit d177541

Please sign in to comment.