From 77cb49d0beef8b5ea8dd6e8bcbd219dc7d7b2c2b Mon Sep 17 00:00:00 2001 From: Ian Krieger Date: Fri, 26 Jan 2024 12:23:11 -0500 Subject: [PATCH] fix: ensure that there is room for length header in byte array --- wasm-thumbnail/src/lib.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/wasm-thumbnail/src/lib.rs b/wasm-thumbnail/src/lib.rs index 3d962d0..5db08bf 100644 --- a/wasm-thumbnail/src/lib.rs +++ b/wasm-thumbnail/src/lib.rs @@ -34,12 +34,12 @@ pub extern "C" fn resize_and_pad( let slice: &[u8] = unsafe { std::slice::from_raw_parts(pointer, length) }; let mut out: Cursor> = Cursor::new(Vec::with_capacity(nsize)); - // Reserve space at the start for length header - let _ = out.write_all(&[0,0,0,0]); + // Add additional space at the start for the length header + out.get_mut().extend_from_slice(&[0,0,0,0]); match _resize_and_pad(slice, &mut out, nwidth, nheight, nsize, nquality) { Ok(thumbnail_len) => { - out.get_mut().splice(..4, thumbnail_len.to_be_bytes().iter().cloned()); + out.get_mut()[0..4].copy_from_slice(&thumbnail_len.to_be_bytes()); } _ => { panic!("Image too large") @@ -95,4 +95,4 @@ pub extern "C" fn deallocate(pointer: *mut c_void, capacity: usize) { unsafe { let _ = Vec::from_raw_parts(pointer, 0, capacity); } -} \ No newline at end of file +}