diff --git a/src/base/encoding.rs b/src/base/encoding.rs index 92c517d8..5c525e85 100644 --- a/src/base/encoding.rs +++ b/src/base/encoding.rs @@ -80,7 +80,8 @@ impl SharedEncoding { #[must_use] pub fn get(&self) -> &'static Encoding { let encoding = self.encoding.load(Ordering::Relaxed); - ALL_ENCODINGS[encoding] + // it will never be out of range, but get() avoids a panic branch + ALL_ENCODINGS.get(encoding).unwrap_or(&ALL_ENCODINGS[0]) } pub fn set(&self, encoding: AsciiCompatibleEncoding) { diff --git a/src/rewritable_units/text_decoder.rs b/src/rewritable_units/text_decoder.rs index 5ed1740c..9fe325f4 100644 --- a/src/rewritable_units/text_decoder.rs +++ b/src/rewritable_units/text_decoder.rs @@ -67,7 +67,13 @@ impl TextDecoder { // the last call to feed_text() may make multiple calls to output_handler, // but only one call to output_handler can be *the* last one. let really_last = last_in_text_node && finished_decoding; - (output_handler)(&buffer[..written], really_last, encoding)?; + + (output_handler)( + // this will always be in bounds, but unwrap_or_default optimizes better + buffer.get(..written).unwrap_or_default(), + really_last, + encoding, + )?; } if finished_decoding { @@ -76,7 +82,7 @@ impl TextDecoder { } return Ok(()); } - raw_input = &raw_input[read..]; + raw_input = raw_input.get(read..).unwrap_or_default(); } }