From c8fdace590bde4d5c3af0543365eb7aea5d2352d Mon Sep 17 00:00:00 2001 From: BeatButton Date: Fri, 7 Apr 2023 10:17:57 -0600 Subject: [PATCH] don't segfault if MagickGetImageBlob returns null --- src/wand/magick.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/wand/magick.rs b/src/wand/magick.rs index fb13f23..ba96095 100644 --- a/src/wand/magick.rs +++ b/src/wand/magick.rs @@ -831,13 +831,17 @@ impl MagickWand { bindings::MagickSetImageFormat(self.wand, c_format.as_ptr()); bindings::MagickGetImageBlob(self.wand, &mut length) }; - let mut bytes = Vec::with_capacity(length as usize); - unsafe { - bytes.set_len(length as usize); - ptr::copy_nonoverlapping(blob, bytes.as_mut_ptr(), length as usize); - bindings::MagickRelinquishMemory(blob as *mut c_void); - }; - Ok(bytes) + if blob.is_null() { + Err(MagickError("failed to write image blob")) + } else { + let mut bytes = Vec::with_capacity(length as usize); + unsafe { + bytes.set_len(length as usize); + ptr::copy_nonoverlapping(blob, bytes.as_mut_ptr(), length as usize); + bindings::MagickRelinquishMemory(blob as *mut c_void); + }; + Ok(bytes) + } } /// Write the images in the desired format to a new blob.