Skip to content

Truly independent web browser

LibWeb+LibTextCodec: Reset TextDecoder when decode() throws

Shannon Boothfb45f5736160
diff --git a/Libraries/LibTextCodec/Rust/src/streaming_decoder.rs b/Libraries/LibTextCodec/Rust/src/streaming_decoder.rs
index f972cefc71b..94a0aae3740 100644
--- a/Libraries/LibTextCodec/Rust/src/streaming_decoder.rs
+++ b/Libraries/LibTextCodec/Rust/src/streaming_decoder.rs
@@ -47,14 +47,13 @@ impl StreamingDecoder {
/// Decodes the next bytes of the input to UTF-16. Unless more of the input follows, the bytes are its end: the
/// decoder then flushes what it kept, and starts over for the next input, whether or not decoding succeeded.
- /// A failure discards the rest of the bytes, and with them whatever the decoder kept of them.
+ /// A failure starts over too, discarding the rest of the bytes and whatever the decoder kept of the input.
pub fn decode(&mut self, input: &[u8], more_input_follows: bool) -> Result<Vec<u16>, DecodingFailed> {
let mut decoded = self.decode_with_current_decoder(input, !more_input_follows);
- match &mut decoded {
- Ok(output) => self.remove_bom_from_output(output),
- Err(DecodingFailed) => self.forget_rest_of_input(),
+ if let Ok(output) = &mut decoded {
+ self.remove_bom_from_output(output);
}
- if !more_input_follows {
+ if decoded.is_err() || !more_input_follows {
self.start_over();
}
decoded
@@ -82,14 +81,6 @@ impl StreamingDecoder {
self.bom_seen = false;
}
- fn forget_rest_of_input(&mut self) {
- // NB: Every decoder but ISO-2022-JP's resets its own state on an error, so a new decoder only drops the bytes
- // encoding_rs kept of the input that the error discards. ISO-2022-JP's mode outlasts its errors.
- if self.encoding != encoding_rs::ISO_2022_JP {
- self.decoder = self.encoding.new_decoder_without_bom_handling();
- }
- }
-
fn decode_with_current_decoder(&mut self, input: &[u8], last: bool) -> Result<Vec<u16>, DecodingFailed> {
let output_capacity = self
.decoder
diff --git a/Libraries/LibWeb/Bindings/Rust/src/encoding/text_decoder.rs b/Libraries/LibWeb/Bindings/Rust/src/encoding/text_decoder.rs
index d2c5b2621b3..43b69881be2 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/encoding/text_decoder.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/encoding/text_decoder.rs
@@ -99,14 +99,14 @@ impl TextDecoder {
// 1. Let item be the result of reading from this's I/O queue.
// 2. If item is end-of-queue and this's do not flush is true, then return the result of running
// serialize I/O queue with this and output.
- // 3. Otherwise:
- // 1. Let result be the result of processing an item with item, this's decoder, this's I/O queue,
- // output, and this's error mode.
- // 2. If result is finished, then return the result of running serialize I/O queue with this and
- // output.
- // 3. Otherwise, if result is error, throw a TypeError.
- // NB: The decoder holds the I/O queue and whether the BOM was seen, and is replaced when a call that does not
- // stream returns, where step 1 replaces it in the next call.
+ // 3. Let result be the result of processing an item with item, this's decoder, this's I/O queue, output,
+ // and this's error mode.
+ // 4. If result is finished, then return the result of running serialize I/O queue with this and output.
+ // 5. If result is error:
+ // 1. Set this's do not flush to false.
+ // 2. Throw a TypeError.
+ // NB: The decoder holds the I/O queue and whether the BOM was seen, and is replaced at the end of a call that
+ // does not stream or that throws, where step 1 replaces it in the next call.
let mut decoder = self.decoder.borrow_mut();
let input = input.map(BufferSource::from);
let decoded = match &input {
diff --git a/Tests/LibWeb/Text/expected/wpt-import/encoding/textdecoder-fatal-streaming.any.txt b/Tests/LibWeb/Text/expected/wpt-import/encoding/textdecoder-fatal-streaming.any.txt
index cc52d710252..d41042691c6 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/encoding/textdecoder-fatal-streaming.any.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/encoding/textdecoder-fatal-streaming.any.txt
@@ -2,13 +2,12 @@ Harness status: OK
Found 8 tests
-5 Pass
-3 Fail
+8 Pass
Pass Fatal flag, non-streaming cases
Pass Fatal flag, streaming cases
Pass Fatal flag, streaming error discards the remaining input: utf-8
-Fail Fatal flag, streaming error resets BOM handling: utf-8
+Pass Fatal flag, streaming error resets BOM handling: utf-8
Pass Fatal flag, streaming error discards the remaining input: utf-16le
-Fail Fatal flag, streaming error resets BOM handling: utf-16le
+Pass Fatal flag, streaming error resets BOM handling: utf-16le
Pass Fatal flag, streaming error discards the remaining input: utf-16be
-Fail Fatal flag, streaming error resets BOM handling: utf-16be
\ No newline at end of file
+Pass Fatal flag, streaming error resets BOM handling: utf-16be
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/encoding/textdecoder-mistakes.any.txt b/Tests/LibWeb/Text/expected/wpt-import/encoding/textdecoder-mistakes.any.txt
index 23a3381637c..b8ab009afa9 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/encoding/textdecoder-mistakes.any.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/encoding/textdecoder-mistakes.any.txt
@@ -2,8 +2,7 @@ Harness status: OK
Found 108 tests
-107 Pass
-1 Fail
+108 Pass
Pass Invalid Unicode input is replaced: utf-8
Pass Invalid Unicode input is replaced: utf-16le
Pass Invalid Unicode input is replaced: utf-16be
@@ -106,7 +105,7 @@ Pass stream: iso-2022-jp
Pass fatal stream: utf-8
Pass fatal stream: utf-16le
Pass fatal stream: utf-16be
-Fail fatal stream: iso-2022-jp
+Pass fatal stream: iso-2022-jp
Pass WPT mislabels: euc-jp
Pass WPT mislabels: shift_jis
Pass WPT mislabels: iso-2022-jp