diff --git a/crates/raster-codecs/src/dds.rs b/crates/raster-codecs/src/dds.rs index 0f848e38c..b9a716688 100644 --- a/crates/raster-codecs/src/dds.rs +++ b/crates/raster-codecs/src/dds.rs @@ -241,7 +241,8 @@ fn parse(b: &[u8]) -> Result, CodecError> { let levels = declared.min(mip_levels(width, height)); // The same header field is a total level size for BC, but a row stride when DDSD_PITCH is set. let pitch = if flags & DDSD_PITCH != 0 { Some(le32(b, 20)?) } else { None }; - Ok(Parsed { encoding, data: b.get(start..).unwrap_or_default(), width, height, levels, pitch }) + let data = b.get(start..).ok_or_else(|| bad("truncated header"))?; + Ok(Parsed { encoding, data, width, height, levels, pitch }) } pub fn info(b: &[u8]) -> Result { diff --git a/crates/raster-codecs/tests/dds.rs b/crates/raster-codecs/tests/dds.rs index 60fe1366d..c0a06b6de 100644 --- a/crates/raster-codecs/tests/dds.rs +++ b/crates/raster-codecs/tests/dds.rs @@ -421,6 +421,31 @@ fn mip_chain() -> Vec { b } +#[test] +fn import_info_rejects_truncated_legacy_headers() { + let b = compressed(4, 4, b"DXT1"); + for n in 0..b.len() { + assert!(matches!(dds::info(&b[..n]), Err(CodecError::Malformed { .. })), "accepted {n} of {} header bytes", b.len()); + } +} + +#[test] +fn import_info_rejects_truncated_dx10_headers() { + let b = dx10(4, 4, 71, 3, 0, 1); + for n in 0..b.len() { + assert!(matches!(dds::info(&b[..n]), Err(CodecError::Malformed { .. })), "accepted {n} of {} header bytes", b.len()); + } +} + +#[test] +fn import_info_accepts_complete_headers_without_pixel_data() { + for b in [compressed(4, 4, b"DXT1"), dx10(4, 4, 71, 3, 0, 1)] { + assert_eq!(dds::info(&b).unwrap(), dds::DdsInfo { width: 4, height: 4, levels: 1, alpha: true, compression: Some(dds::DdsCompression::Bc1) }); + assert!(matches!(decode(&b), Err(CodecError::Malformed { .. }))); + assert!(matches!(dds::decode_levels(&b, &Limits::default(), &|| false), Err(CodecError::Malformed { .. }))); + } +} + #[test] fn import_info_and_all_mips_use_the_shared_decoder() { let b = mip_chain();