raster-codecs: reject incomplete DDS metadata headers

Keep complete header-only probes valid while rejecting truncated legacy
and DX10 headers before returning metadata. Cover every truncated prefix
and preserve missing-pixel-data errors in both decoding paths.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
blenuxandDevin committed 2026-11-13 19:50:43 +11:00
1 parent 18f529112e
commit 332c5abd8d
2 files changed
+27 -1

No files matched your search

+2 -1
View File
@@ -241,7 +241,8 @@ fn parse(b: &[u8]) -> Result<Parsed<'_>, 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<DdsInfo, CodecError> {
+25
View File
@@ -421,6 +421,31 @@ fn mip_chain() -> Vec<u8> {
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();