Skip to content

Commit e683907

Browse files
authored
Merge branch 'master' into sync-error
2 parents bd0b928 + 8b2f413 commit e683907

File tree

3 files changed

+22
-19
lines changed

3 files changed

+22
-19
lines changed

src/decoder.rs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ impl<R: Read> Decoder<R> {
232232
Marker::DQT => {
233233
let tables = parse_dqt(&mut self.reader)?;
234234

235-
for (i, &table) in tables.into_iter().enumerate() {
235+
for (i, &table) in tables.iter().enumerate() {
236236
if let Some(table) = table {
237237
let mut unzigzagged_table = [0u16; 64];
238238

@@ -333,23 +333,22 @@ impl<R: Read> Decoder<R> {
333333
}
334334

335335
fn read_marker(&mut self) -> Result<Marker> {
336-
// This should be an error as the JPEG spec doesn't allow extraneous data between marker segments.
337-
// libjpeg allows this though and there are images in the wild utilising it, so we are
338-
// forced to support this behavior.
339-
// Sony Ericsson P990i is an example of a device which produce this sort of JPEGs.
340-
while self.reader.read_u8()? != 0xFF {}
341-
342-
let mut byte = self.reader.read_u8()?;
343-
344-
// Section B.1.1.2
345-
// "Any marker may optionally be preceded by any number of fill bytes, which are bytes assigned code X’FF’."
346-
while byte == 0xFF {
347-
byte = self.reader.read_u8()?;
348-
}
349-
350-
match byte {
351-
0x00 => Err(Error::Format("FF 00 found where marker was expected".to_owned())),
352-
_ => Ok(Marker::from_u8(byte).unwrap()),
336+
loop {
337+
// This should be an error as the JPEG spec doesn't allow extraneous data between marker segments.
338+
// libjpeg allows this though and there are images in the wild utilising it, so we are
339+
// forced to support this behavior.
340+
// Sony Ericsson P990i is an example of a device which produce this sort of JPEGs.
341+
while self.reader.read_u8()? != 0xFF {}
342+
343+
// Section B.1.1.2
344+
// All markers are assigned two-byte codes: an X’FF’ byte followed by a
345+
// byte which is not equal to 0 or X’FF’ (see Table B.1). Any marker may
346+
// optionally be preceded by any number of fill bytes, which are bytes
347+
// assigned code X’FF’.
348+
let byte = self.reader.read_u8()?;
349+
if byte != 0x00 && byte != 0xFF {
350+
return Ok(Marker::from_u8(byte).unwrap());
351+
}
353352
}
354353
}
355354

@@ -520,7 +519,10 @@ impl<R: Read> Decoder<R> {
520519
}
521520
}
522521

523-
let marker = huffman.take_marker(&mut self.reader)?;
522+
let mut marker = huffman.take_marker(&mut self.reader)?;
523+
while let Some(Marker::RST(_)) = marker {
524+
marker = self.read_marker().ok();
525+
}
524526

525527
if produce_data {
526528
// Retrieve all the data from the worker thread.

tests/crashtest/images/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ imagetestsuite/ | The files in this directory were taken from https://
55
dc-predictor-overflow.jpg | Found by Wim Looman (@Nemo157) while fuzzing
66
derive-huffman-codes-overflow.jpg | Found by Pascal Hertleif (@killercup) while fuzzing
77
missing-sof.jpg | Found by Corey Farwell (@frewsxcv) when fuzz testing
8+
extraneous-bytes-after-sos.jpg | Scan from brother DSmobile 920DW provided by Filip Lundborg (@filipl)
Loading

0 commit comments

Comments
 (0)