From 56a002b693a4fc0c7ffd1aacd3b01ac3de8fe299 Mon Sep 17 00:00:00 2001 From: Benjamin Brienen Date: Fri, 8 Nov 2024 22:55:34 +0100 Subject: [PATCH] Return Error instead of panicking when reading invalid dds file (#16283) # Objective Fixes #15928 ## Solution return Error instead of panic ## Testing I don't know if we need to add a test for this. It is pretty straightforward. --- crates/bevy_image/src/dds.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/bevy_image/src/dds.rs b/crates/bevy_image/src/dds.rs index 4ba311377d..f146f07c71 100644 --- a/crates/bevy_image/src/dds.rs +++ b/crates/bevy_image/src/dds.rs @@ -1,3 +1,5 @@ +//! [DirectDraw Surface](https://en.wikipedia.org/wiki/DirectDraw_Surface) functionality. + #[cfg(debug_assertions)] use bevy_utils::warn_once; use ddsfile::{Caps2, D3DFormat, Dds, DxgiFormat}; @@ -16,7 +18,8 @@ pub fn dds_buffer_to_image( is_srgb: bool, ) -> Result { let mut cursor = Cursor::new(buffer); - let dds = Dds::read(&mut cursor).expect("Failed to parse DDS file"); + let dds = Dds::read(&mut cursor) + .map_err(|error| TextureError::InvalidData(format!("Failed to parse DDS file: {error}")))?; let texture_format = dds_format_to_texture_format(&dds, is_srgb)?; if !supported_compressed_formats.supports(texture_format) { return Err(TextureError::UnsupportedTextureFormat(format!(