diff --git a/render-wasm/src/wasm/fills/image.rs b/render-wasm/src/wasm/fills/image.rs index ce245055fb..65a4697181 100644 --- a/render-wasm/src/wasm/fills/image.rs +++ b/render-wasm/src/wasm/fills/image.rs @@ -70,22 +70,20 @@ pub struct ShapeImageIds { image_id: Uuid, } -impl From<[u8; IMAGE_IDS_SIZE]> for ShapeImageIds { - fn from(bytes: [u8; IMAGE_IDS_SIZE]) -> Self { - // FIXME: this should probably be a try_from instead - let shape_id = Uuid::try_from(&bytes[0..16]).unwrap(); - let image_id = Uuid::try_from(&bytes[16..32]).unwrap(); - ShapeImageIds { shape_id, image_id } - } -} - -impl TryFrom> for ShapeImageIds { +impl TryFrom<&[u8]> for ShapeImageIds { type Error = Error; - fn try_from(value: Vec) -> Result { - let mut arr = [0u8; IMAGE_IDS_SIZE]; - arr.copy_from_slice(&value); - Ok(ShapeImageIds::from(arr)) + fn try_from(bytes: &[u8]) -> Result { + if bytes.len() < IMAGE_IDS_SIZE { + return Err(Error::CriticalError(format!( + "Invalid image ids byte length: expected at least {}, got {}", + IMAGE_IDS_SIZE, + bytes.len() + ))); + } + let shape_id = Uuid::try_from(&bytes[0..16]).map_err(Error::CriticalError)?; + let image_id = Uuid::try_from(&bytes[16..32]).map_err(Error::CriticalError)?; + Ok(ShapeImageIds { shape_id, image_id }) } } @@ -93,7 +91,7 @@ impl TryFrom> for ShapeImageIds { #[wasm_error] pub extern "C" fn store_image() -> Result<()> { let bytes = mem::bytes(); - let ids = ShapeImageIds::try_from(bytes[0..IMAGE_IDS_SIZE].to_vec())?; + let ids = ShapeImageIds::try_from(&bytes[0..IMAGE_IDS_SIZE])?; // Read is_thumbnail flag (4 bytes as u32) let is_thumbnail_bytes = &bytes[IMAGE_IDS_SIZE..IMAGE_HEADER_SIZE]; @@ -139,7 +137,7 @@ pub extern "C" fn store_image_from_texture() -> Result<()> { )); } - let ids = ShapeImageIds::try_from(bytes[0..IMAGE_IDS_SIZE].to_vec()) + let ids = ShapeImageIds::try_from(&bytes[0..IMAGE_IDS_SIZE]) .map_err(|_| Error::CriticalError("Invalid image ids".to_string()))?; // FIXME: read bytes in a safe way