Skip to content

Commit

Permalink
src: fix TextDecoder final flush size calculation
Browse files Browse the repository at this point in the history
Flushing a TextDecoder with a zero-sized input and pending
incomplete characters was failing when fatal: false.

Signed-off-by: James M Snell <jasnell@gmail.com>

PR-URL: #39737
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information
jasnell authored and danielleadams committed Aug 16, 2021
1 parent eb74125 commit 03be967
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/node_i18n.cc
Original file line number Diff line number Diff line change
Expand Up @@ -442,11 +442,24 @@ void ConverterObject::Decode(const FunctionCallbackInfo<Value>& args) {
UErrorCode status = U_ZERO_ERROR;
MaybeStackBuffer<UChar> result;
MaybeLocal<Object> ret;
size_t limit = converter->min_char_size() * input.length();

UBool flush = (flags & CONVERTER_FLAGS_FLUSH) == CONVERTER_FLAGS_FLUSH;

// When flushing the final chunk, the limit is the maximum
// of either the input buffer length or the number of pending
// characters times the min char size.
size_t limit = converter->min_char_size() *
(!flush ?
input.length() :
std::max(
input.length(),
static_cast<size_t>(
ucnv_toUCountPending(converter->conv(), &status))));
status = U_ZERO_ERROR;

if (limit > 0)
result.AllocateSufficientStorage(limit);

UBool flush = (flags & CONVERTER_FLAGS_FLUSH) == CONVERTER_FLAGS_FLUSH;
auto cleanup = OnScopeLeave([&]() {
if (flush) {
// Reset the converter state.
Expand Down

0 comments on commit 03be967

Please sign in to comment.