summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames M Snell <jasnell@gmail.com>2021-08-11 08:55:28 -0700
committerDanielle Adams <adamzdanielle@gmail.com>2021-08-16 16:35:25 -0400
commit03be967cada17ee81550dea59364e424afea5af8 (patch)
tree3eb7dc277ea34aa2e7e0365beb9bddd38013bb13
parenteb741253fd899088a42bde8f44306d72ab7aa883 (diff)
downloadnode-new-03be967cada17ee81550dea59364e424afea5af8.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 8a08a95bf7..b1b3f5d174 100644
--- a/src/node_i18n.cc
+++ b/src/node_i18n.cc
@@ -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.