Skip to content

Commit b5b1a1e

Browse files
kornelskiorium
authored andcommitted
Avoid panic branches in TextDecoder
1 parent 944abea commit b5b1a1e

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/base/encoding.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ impl SharedEncoding {
8080
#[must_use]
8181
pub fn get(&self) -> &'static Encoding {
8282
let encoding = self.encoding.load(Ordering::Relaxed);
83-
ALL_ENCODINGS[encoding]
83+
// it will never be out of range, but get() avoids a panic branch
84+
ALL_ENCODINGS.get(encoding).unwrap_or(&ALL_ENCODINGS[0])
8485
}
8586

8687
pub fn set(&self, encoding: AsciiCompatibleEncoding) {

src/rewritable_units/text_decoder.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,13 @@ impl TextDecoder {
6767
// the last call to feed_text() may make multiple calls to output_handler,
6868
// but only one call to output_handler can be *the* last one.
6969
let really_last = last_in_text_node && finished_decoding;
70-
(output_handler)(&buffer[..written], really_last, encoding)?;
70+
71+
(output_handler)(
72+
// this will always be in bounds, but unwrap_or_default optimizes better
73+
buffer.get(..written).unwrap_or_default(),
74+
really_last,
75+
encoding,
76+
)?;
7177
}
7278

7379
if finished_decoding {
@@ -76,7 +82,7 @@ impl TextDecoder {
7682
}
7783
return Ok(());
7884
}
79-
raw_input = &raw_input[read..];
85+
raw_input = raw_input.get(read..).unwrap_or_default();
8086
}
8187
}
8288

0 commit comments

Comments
 (0)