Skip to content

Commit 03be967

Browse files
jasnelldanielleadams
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 <[email protected]> PR-URL: #39737 Reviewed-By: Robert Nagy <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent eb74125 commit 03be967

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
@@ -442,11 +442,24 @@ void ConverterObject::Decode(const FunctionCallbackInfo<Value>& args) {
442442
UErrorCode status = U_ZERO_ERROR;
443443
MaybeStackBuffer<UChar> result;
444444
MaybeLocal<Object> ret;
445-
size_t limit = converter->min_char_size() * input.length();
445+
446+
UBool flush = (flags & CONVERTER_FLAGS_FLUSH) == CONVERTER_FLAGS_FLUSH;
447+
448+
// When flushing the final chunk, the limit is the maximum
449+
// of either the input buffer length or the number of pending
450+
// characters times the min char size.
451+
size_t limit = converter->min_char_size() *
452+
(!flush ?
453+
input.length() :
454+
std::max(
455+
input.length(),
456+
static_cast<size_t>(
457+
ucnv_toUCountPending(converter->conv(), &status))));
458+
status = U_ZERO_ERROR;
459+
446460
if (limit > 0)
447461
result.AllocateSufficientStorage(limit);
448462

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

0 commit comments

Comments
 (0)