Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: fix setting Converter sub char length #38331

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
}