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.
This commit is contained in:
Benjamin Brienen 2024-11-08 22:55:34 +01:00 committed by GitHub
parent c29e67153b
commit 56a002b693
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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<Image, TextureError> {
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!(