summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames M Snell <jasnell@gmail.com>2021-08-11 08:55:28 -0700
committerMichaƫl Zasso <targos@protonmail.com>2021-09-04 15:14:51 +0200
commitc4686fa5a7c0e6ad1e118c30e01ebfec7c7a8be5 (patch)
treeb19703da5a9041cfee9c3110060669befe455b32
parentf8fee449f7bd5f6704b5eb75befe05a79cfa77f5 (diff)
downloadnode-new-c4686fa5a7c0e6ad1e118c30e01ebfec7c7a8be5.tar.gz
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: https://github.com/nodejs/node/pull/39737 Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
-rw-r--r--src/node_i18n.cc17
1 files changed, 15 insertions, 2 deletions
diff --git a/src/node_i18n.cc b/src/node_i18n.cc
index 63a6db7a49..e4bc79ebc2 100644
--- a/src/node_i18n.cc
+++ b/src/node_i18n.cc
@@ -441,11 +441,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.