Skip to content

Commit c4686fa

Browse files
jasnelltargos
authored andcommitted
src: fix TextDecoder final flush size calculation
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>
1 parent f8fee44 commit c4686fa

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/node_i18n.cc

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,11 +441,24 @@ void ConverterObject::Decode(const FunctionCallbackInfo<Value>& args) {
441441
UErrorCode status = U_ZERO_ERROR;
442442
MaybeStackBuffer<UChar> result;
443443
MaybeLocal<Object> ret;
444-
size_t limit = converter->min_char_size() * input.length();
444+
445+
UBool flush = (flags & CONVERTER_FLAGS_FLUSH) == CONVERTER_FLAGS_FLUSH;
446+
447+
// When flushing the final chunk, the limit is the maximum
448+
// of either the input buffer length or the number of pending
449+
// characters times the min char size.
450+
size_t limit = converter->min_char_size() *
451+
(!flush ?
452+
input.length() :
453+
std::max(
454+
input.length(),
455+
static_cast<size_t>(
456+
ucnv_toUCountPending(converter->conv(), &status))));
457+
status = U_ZERO_ERROR;
458+
445459
if (limit > 0)
446460
result.AllocateSufficientStorage(limit);
447461

448-
UBool flush = (flags & CONVERTER_FLAGS_FLUSH) == CONVERTER_FLAGS_FLUSH;
449462
auto cleanup = OnScopeLeave([&]() {
450463
if (flush) {
451464
// Reset the converter state.

0 commit comments

Comments
 (0)