Skip to content

Reset TextDecoder when decode() throws - #369

Open
annevk wants to merge 1 commit into
mainfrom
annevk/reset-on-throw
Open

annevk wants to merge 1 commit into
mainfrom
annevk/reset-on-throw

Conversation

@annevk

@annevk annevk commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

When decode() threw while streaming, the next invocation continued with the decoder, I/O queue, and BOM seen as they were when the error occurred. As the output up to the error is not returned, continuing from there is not useful, and implementations disagreed on what remained. Instead the next invocation now starts anew.

TextDecoderStream is unaffected, as an error there errors the stream.

Also remove the "Otherwise" steps that follow early returns in decode(), which removes a level of nesting.

Fixes #358 and fixes #359.

(See WHATWG Working Mode: Changes for more details.)


Preview | Diff

When decode() threw while streaming, the next invocation continued with the decoder, I/O queue, and BOM seen as they were when the error occurred. As the output up to the error is not returned, continuing from there is not useful, and implementations disagreed on what remained. Instead the next invocation now starts anew.

TextDecoderStream is unaffected, as an error there errors the stream.

Also remove the "Otherwise" steps that follow early returns in decode(), which removes a level of nesting.

Fixes #358 and fixes #359.
@hsivonen

Copy link
Copy Markdown
Member

It seems I don't have the permission to edit the issue, but the Gecko implementation bug is: https://bugzilla.mozilla.org/show_bug.cgi?id=2094779

annevk added a commit to annevk/WebKit that referenced this pull request Nov 19, 2026
https://bugs.webkit.org/show_bug.cgi?id=332174
rdar://193118928

Reviewed by NOBODY (OOPS!).

The Encoding Standard now has a decode() call that throws reset the decoder, so
that the next call doesn't continue with state from before the error, such as
the ISO-2022-JP mode or whether a BOM was seen:
whatwg/encoding#369

security/decode-buffer-size.html relied on that state for its second exception,
so give that decode() call its own partial sequence.

Tests: imported/w3c/web-platform-tests/encoding/textdecoder-fatal-streaming.any.html
       imported/w3c/web-platform-tests/encoding/textdecoder-mistakes.any.html
       security/decode-buffer-size.html
annevk added a commit to annevk/WebKit that referenced this pull request Nov 20, 2026
https://bugs.webkit.org/show_bug.cgi?id=332174
rdar://193118928

Reviewed by NOBODY (OOPS!).

The Encoding Standard now has a decode() call that throws reset the decoder, so
that the next call doesn't continue with state from before the error, such as
the ISO-2022-JP mode or whether a BOM was seen:
whatwg/encoding#369

security/decode-buffer-size.html relied on that state for its second exception,
so give that decode() call its own partial sequence.

Tests: imported/w3c/web-platform-tests/encoding/textdecoder-fatal-streaming.any.html
       imported/w3c/web-platform-tests/encoding/textdecoder-mistakes.any.html
       security/decode-buffer-size.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

UTF-8 does not reset state when returning error ISO-2022-JP does not reset state when returning error

2 participants