Repository navigation
src: fix TextDecoder large-input and error paths - #65634
JosephDoUrden wants to merge 2 commits into
Conversation
|
Benchmark GHA (util / text-decoder): https://github.com/nodejs/node/actions/runs/33238297431 🟢 Results
Benchmark results:
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #65634 +/- ##
==========================================
- Coverage 90.52% 90.50% -0.02%
==========================================
Files 805 805
Lines 285812 285815 +3
Branches 55177 55190 +13
==========================================
- Hits 258720 258685 -35
- Misses 17190 17241 +51
+ Partials 9902 9889 -13
🚀 New features to boost your workflow:
|
This comment was marked as resolved.
This comment was marked as resolved.
953b4f5 to
4524fad
Compare
|
hey @MikeMcC399, the branch has been rebased, and all checks have been passed. Thanks |
This comment was marked as outdated.
This comment was marked as outdated.
4524fad to
77c5bcd
Compare
ConverterObject::Decode() sized its ICU target buffer as the input length, or the pending byte count when flushing if that is larger, times min_char_size(), times 2. min_char_size() is the minimum number of bytes per character, so multiplying by it inflates the bound instead of tightening it: for UTF-16 (min_char_size() == 2) a 256 MiB input requested 2^30 UChars, which fails ucnv_toUnicode()'s internal targetLimit validation before any input is examined, and the failure was then reported as ERR_ENCODING_INVALID_ENCODED_DATA. Bound the buffer by 2 * (input length + pending bytes) / min_char_size instead: each character consumes at least min_char_size bytes and emits at most one surrogate pair, and bytes carried over from previous chunks complete a character in this one. The request is also clamped to String::kMaxLength + 1 UChars, one extra for a leading BOM that the success path strips: a result that overflows the clamped buffer cannot become a V8 string even after the strip, so it is reported as ERR_STRING_TOO_LONG, the same error StringBytes::Encode() throws for oversized results. This decodes every input whose result fits in a V8 string; inputs above ICU's 2 GiB single-call source limit keep their existing error behaviour. Also return after a failed StringBytes::Encode() instead of falling through, so the exception it scheduled (such as ERR_STRING_TOO_LONG for results beyond the string limit) is no longer masked by ERR_ENCODING_INVALID_ENCODED_DATA. The `2 *` factor dates to 98ec909, which restored the effective capacity that an earlier targetLimit arithmetic bug had provided by accident. The min_char_size() multiplier itself is older, from ed21cb1. Fixes: nodejs#47645 Refs: nodejs#41026 Refs: nodejs#61559 Signed-off-by: Yusufhan Sacak <yusufhansacak@icloud.com>
A too-long decode() result left the ICU converter as it was when the error was thrown, so a streaming decoder continued from there. When the result overflowed the target buffer between the halves of a surrogate pair, ICU kept the trail surrogate in its overflow buffer and emitted it at the start of the next call. When the result fitted the buffer but not a string, the BOM stayed marked as seen and the next stream kept its BOM. Reset the converter on every error exit, as decoding errors already do, so the next call starts fresh. Refs: nodejs#67610 Signed-off-by: Yusufhan Sacak <yusufhansacak@icloud.com>
77c5bcd to
7c5433a
Compare
|
rebased onto main now that #67610 has landed, and added a commit so the too-long paths reset the decoder as well. details are in the description. |
Fixes #47645.
TextDecoder('utf-16le').decode(new Uint16Array(2**27))throwsERR_ENCODING_INVALID_ENCODED_DATAeven though the input is valid and the result (134M chars) fits comfortably in a V8 string. Root cause analysis is in the issue thread:ConverterObject::Decode()sizes the ICU target as2 * min_char_size * input lengthUChars.min_char_size()is the minimum number of bytes per character, so multiplying by it inflates the bound instead of tightening it. For UTF-16 that requests 4x the input in UChars, which crossesucnv_toUnicode()'s target range validation (0x3fffffff UChars) at exactly 2**27 elements, and ICU rejects the call before looking at a single byte. The failure then gets reported by the blanket invalid-data throw at the bottom of the function.Two changes, same function:
The bound becomes
2 * (input length + pending bytes) / min_char_size, clamped to the ICU cap. Each character consumes at leastmin_char_sizebytes and emits at most a surrogate pair, and bytes carried over from a previous chunk complete a character in this one, so they count too. For everymin_char_size == 1encoding (all the CJK ones) this is the same as or larger than the old bound, so nothing tightens there. Only utf-16le/be change, from 4x to 1x. After the change the whole representable range decodes: an input of2 * MAX_STRING_LENGTHbytes produces exactly aMAX_STRING_LENGTHstring.Second,
Decode()now returns after a failedStringBytes::Encode()instead of falling through, so the exception Encode scheduled (ERR_STRING_TOO_LONGfor oversized results) is no longer replaced byERR_ENCODING_INVALID_ENCODED_DATA. Boundary behaviour measured locally:2 * MAX_STRING_LENGTHbytes decodes,+2bytes throwsERR_STRING_TOO_LONG, lone surrogate withfatal: truestill throwsERR_ENCODING_INVALID_ENCODED_DATA.On the interaction with #61559 / #61041: I know the direction is to move TextDecoder off ICU, and #61559 would take utf-16 out of this path entirely. But big5, euc-jp, euc-kr, gb18030, shift_jis and iso-2022-jp keep routing through
ConverterObject::Decode(), and the error-path fix applies to all of them, so this stands regardless of when the fast-path work lands. Happy to rebase if #61559 moves first.Tests are in pummel because of the working set (about 1.6 GiB and 4.6 GiB peaks as measured again on 26 Nov; the ~3 GiB I first wrote for the second one was low). The streaming case uses an odd byte split so a code unit actually stays pending across the chunk boundary, and compares content, not just length. Everything ran locally on both a small-icu and a full-icu build: the two pummel tests (including the gb18030
ERR_STRING_TOO_LONGcase, which needs full-icu),parallel/test-whatwg-encoding*andparallel/test-icu-*, plus an A/B sweep against an unpatched build of the same tree (91 sizes per endianness, no behaviour change other than the fixed cases) and a mid-character gb18030 streaming split to exercise the pending-bytes term on amin_char_size == 1encoding.Update 26 Nov, after #67610 landed: rebased onto main and added a second commit. #67610 resets the converter before the invalid-data throw. The two exits this PR adds sat above that reset, so a too-long result in stream mode left the ICU converter as it was. If the target filled between the halves of a surrogate pair, ICU kept the trail surrogate and emitted it at the start of the next
decode(). IfStringBytes::Encode()rejected the result, the BOM stayed marked as seen. The second commit moves the reset ahead of all three error exits, which is what the UTF-8 JS path'scatchalready does. The stream cases are in their own pummel test,test-whatwg-encoding-custom-textdecoder-toolong-reset.js(about 3.1 GiB peak). With the reset removed that test fails with'\ude00A' !== 'A'and a leading BOM on'B'. This round ran on a full-icu build only: the three pummel tests,parallel/test-whatwg-encoding*,parallel/test-icu-*andmake lint-cpp. I did not re-run small-icu or the JS linter.Known remaining limitation: results that could never fit in a string (more than 0x3fffffff UChars, i.e. inputs over ~2 GiB) still surface the blanket error rather than
ERR_STRING_TOO_LONG. Fixing that needs a chunked conversion loop; it did not seem worth the extra risk in this change.AI disclosure: I used an AI coding agent for parts of the investigation and drafting. I verified the root cause against the ICU and Node sources myself, and every number in this description comes from runs on my own machine.