Skip to content

Instantly share code, notes, and snippets.

@MoSal
Last active July 28, 2022 14:42
Show Gist options
  • Save MoSal/54108caaf3330e4a47d0022a03cf58ce to your computer and use it in GitHub Desktop.
Save MoSal/54108caaf3330e4a47d0022a03cf58ce to your computer and use it in GitHub Desktop.
jpeg-decoder *fix*
src/decoder.rs | 40 ++++++++++++++++++++++++++++++----------
1 file changed, 30 insertions(+), 10 deletions(-)
diff --git a/src/decoder.rs b/src/decoder.rs
index 11de44d..cc8d195 100644
--- a/src/decoder.rs
+++ b/src/decoder.rs
@@ -288,11 +288,19 @@ impl<R: Read> Decoder<R> {
.map_or(0, |frame| frame.components.len())
];
- loop {
- let marker = match pending_marker.take() {
- Some(m) => m,
- None => self.read_marker()?,
- };
+ 'R: loop {
+ let marker;
+ match pending_marker.take() {
+ Some(m) => marker = m,
+ None => match self.read_marker() {
+ Ok(m) => marker = m,
+ Err(e) => {
+ eprintln!("failed to read marker: {e}");
+ // don't fail for now, explicitly break loop with label
+ break 'R;
+ }
+ },
+ }
match marker {
// Frame header
@@ -848,7 +856,7 @@ impl<R: Read> Decoder<R> {
for (i, component) in components.iter().enumerate() {
for v_pos in 0..mcu_vertical_samples[i] {
- for h_pos in 0..mcu_horizontal_samples[i] {
+ 'H_POS: for h_pos in 0..mcu_horizontal_samples[i] {
let coefficients = if is_progressive {
let block_y = (mcu_y * mcu_vertical_samples[i] + v_pos) as usize;
let block_x = (mcu_x * mcu_horizontal_samples[i] + h_pos) as usize;
@@ -879,7 +887,7 @@ impl<R: Read> Decoder<R> {
.unwrap();
if scan.successive_approximation_high == 0 {
- decode_block(
+ let res = decode_block(
&mut self.reader,
coefficients,
&mut huffman,
@@ -889,9 +897,15 @@ impl<R: Read> Decoder<R> {
scan.successive_approximation_low,
&mut eob_run,
&mut dc_predictors[i],
- )?;
+ );
+ if let Err(e) = res {
+ // Ignore error for now to continue decoding
+ // explicit continue with label
+ eprintln!("decode_block failed, component_idx={i}, v_pos={v_pos}, h_pos={h_pos}: {e}");
+ continue 'H_POS;
+ }
} else {
- decode_block_successive_approximation(
+ let res = decode_block_successive_approximation(
&mut self.reader,
coefficients,
&mut huffman,
@@ -899,7 +913,13 @@ impl<R: Read> Decoder<R> {
scan.spectral_selection.clone(),
scan.successive_approximation_low,
&mut eob_run,
- )?;
+ );
+ if let Err(e) = res {
+ // Ignore error for now to continue decoding
+ // explicit continue with label
+ eprintln!("decode_block_successive_approximation failed, component_idx={i}, v_pos={v_pos}, h_pos={h_pos}: {e}");
+ continue 'H_POS;
+ }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment