Skip to content

Commit 9b0aa08

Browse files
authored
Merge pull request #100 from filipl/extraneous-bytes-after-sos
Extraneous bytes after SOS
2 parents b25744e + 83931ea commit 9b0aa08

File tree

3 files changed

+21
-18
lines changed

3 files changed

+21
-18
lines changed

src/decoder.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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)