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 <[email protected]>
Fixes: #38330

PR-URL: #38331
Reviewed-By: Juan José Arboleda <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
  • Loading branch information
jasnell authored and targos committed Apr 29, 2021
1 parent 3649ec5 commit 09cacd7
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
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,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 @@ -190,7 +195,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
Original file line number Diff line number Diff line change
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 09cacd7

Please sign in to comment.