Merge pull request #2723 from dotCooCoo/fix/jpegls-restart-marker-after-segment

image-codecs: reject a JPEG-LS restart marker that follows another segment
This commit is contained in:
BFlat
2026-11-26 10:11:33 +07:00
committed by GitHub
3 changed files with 60 additions and 9 deletions
+7 -6
View File
@@ -673,7 +673,13 @@ pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result<Image, CodecError> {
if marker == 0xD9 {
return finish(frame, planes, scans_done);
}
if marker == 0x00 || (0xD0..=0xD7).contains(&marker) || marker == 0x01 {
let restart = (0xD0..=0xD7).contains(&marker);
// A restart marker comes only between two intervals inside a scan, and every scan the
// decoder reads fits in one interval, so none may follow it, before or after other segments.
if restart && restart_interval != 0 {
return Err(bad("a restart marker outside a scan's restart intervals"));
}
if marker == 0x00 || restart || marker == 0x01 {
pos += 2;
continue;
}
@@ -758,11 +764,6 @@ pub(crate) fn decode(b: &[u8], limits: &Limits) -> Result<Image, CodecError> {
samples.try_reserve_exact(f.width * f.height).map_err(|_| bad("pixel buffer is too large"))?;
samples.resize(f.width * f.height, 0u16);
decode_scan(&b[start..end], f.width, f.height, &params, &mut samples)?;
// A restart marker comes only between two intervals, so none follows a scan that
// fits in one. Fill bytes (`0xFF`) may come before the marker.
if restart_interval != 0 && b[end..].iter().find(|&&v| v != 0xFF).is_some_and(|m| (0xD0..=0xD7).contains(m)) {
return Err(bad("a restart marker after the last restart interval"));
}
planes[idx] = samples;
scans_done += 1;
pos = end;
+3 -2
View File
@@ -411,8 +411,9 @@ impl PrefixDetector {
/// magic and is recognized last, by header plausibility.
///
/// JPEG-LS is told from JPEG by the frame header that follows the marker segments, so `bytes`
/// must reach that header; a buffer that ends before it is reported as [`Format::Jpeg`]. Use
/// [`detect_prefix`] on a buffer that may hold only the start of a file.
/// must reach that header; a buffer that ends before it is reported as [`Format::Jpeg`] when it
/// starts with `FF D8 FF`, and a buffer of only `FF D8` matches no format. Use [`detect_prefix`] on
/// a buffer that may hold only the start of a file: for `FF D8` it asks for a 4-byte prefix.
pub fn detect(bytes: &[u8]) -> Option<Format> {
let b = bytes;
if b.starts_with(b"\x89PNG\r\n\x1a\n") {
+50 -1
View File
@@ -361,6 +361,7 @@ fn a_cut_off_jpeg_asks_for_more_bytes() {
assert_eq!(detect_prefix(&bytes[..5]), Detection::NeedBytes(6));
assert_eq!(detect_prefix(&bytes[..3]), Detection::NeedBytes(4));
assert_eq!(detect_prefix(&[0xFF, 0xD8]), Detection::NeedBytes(4));
assert_eq!(detect(&[0xFF, 0xD8]), None);
assert_eq!(detect(&bytes[..50]), Some(Format::Jpeg));
assert!(decode_as(Format::JpegLs, &bytes[..50]).is_err());
// A byte that is not a marker where one should be ends the walk: no frame header follows.
@@ -394,7 +395,12 @@ fn a_restart_interval_before_the_frame_header_is_jpeg_ls() {
bytes.extend(std::iter::repeat_n(0, size));
bytes.extend_from_slice(&plain[2..]);
assert_eq!(detect(&bytes), Some(Format::JpegLs));
assert_eq!(decode(&bytes).is_ok(), ok, "{size} interval bytes");
let decoded = decode(&bytes);
if ok {
assert_eq!(decoded.unwrap().data(), img.data(), "{size} interval bytes");
} else {
assert!(matches!(decoded, Err(CodecError::Malformed { .. })), "{size} interval bytes: {decoded:?}");
}
}
}
@@ -424,6 +430,28 @@ fn the_restart_interval_in_effect_at_the_scan_decides_decoding() {
assert!(matches!(decode(&with_dri(&[0, 2])), Err(CodecError::Unsupported { .. })));
}
#[test]
fn each_component_scan_uses_the_restart_interval_in_effect_at_it() {
// The 5x3 RGB file codes each component in its own scan.
let img = Image::from_u8(5, 3, ChannelLayout::Rgb, (0..45).collect()).unwrap();
let plain = encode(&img, Format::JpegLs, &EncodeOptions::default()).unwrap();
let scans: Vec<usize> = plain.windows(2).enumerate().filter(|(_, w)| *w == [0xFF, 0xDA]).map(|(i, _)| i).collect();
assert_eq!(scans.len(), 3);
// A DRI segment with `first` before the first scan and one with `second` before the second.
let with_dri = |first: u16, second: u16| {
let mut bytes = plain[..scans[0]].to_vec();
for (interval, part) in [(first, &plain[scans[0]..scans[1]]), (second, &plain[scans[1]..])] {
bytes.extend_from_slice(&[0xFF, 0xDD, 0, 4]);
bytes.extend_from_slice(&interval.to_be_bytes());
bytes.extend_from_slice(part);
}
bytes
};
assert_eq!(decode(&with_dri(3, 3)).unwrap().data(), img.data());
assert_eq!(decode(&with_dri(3, 0)).unwrap().data(), img.data());
assert!(matches!(decode(&with_dri(3, 2)), Err(CodecError::Unsupported { .. })));
}
#[test]
fn a_restart_marker_after_a_scan_of_one_interval_is_an_error() {
// With an interval of 3 lines the 5x3 scan is one interval, which no restart marker follows.
@@ -442,6 +470,27 @@ fn a_restart_marker_after_a_scan_of_one_interval_is_an_error() {
assert!(decode(&bytes).is_err());
}
#[test]
fn a_restart_marker_after_a_comment_is_an_error_while_an_interval_is_set() {
let (img, plain) = jpeg_ls_file();
let eoi = plain.len() - 2;
// `after` goes between the scan data and the EOI of the 5x3 file with an interval of `dri`.
let with = |dri: u8, after: &[u8]| {
let mut bytes = plain[..15].to_vec();
bytes.extend_from_slice(&[0xFF, 0xDD, 0, 4, 0, dri]);
bytes.extend_from_slice(&plain[15..eoi]);
bytes.extend_from_slice(after);
bytes.extend_from_slice(&plain[eoi..]);
bytes
};
const COMMENT: [u8; 4] = [0xFF, 0xFE, 0, 2];
assert_eq!(decode(&with(3, &COMMENT)).unwrap().data(), img.data());
let decoded = decode(&with(3, &[COMMENT.as_slice(), &[0xFF, 0xD0]].concat()));
assert!(matches!(decoded, Err(CodecError::Malformed { .. })), "{decoded:?}");
// With restarts off, a stray restart marker is skipped as before.
assert_eq!(decode(&with(0, &[COMMENT.as_slice(), &[0xFF, 0xD0]].concat())).unwrap().data(), img.data());
}
#[test]
fn a_stuffed_zero_where_a_marker_should_be_is_jpeg() {
// `FF 00` is not a marker, so the walk ends there without asking for a length field.