Skip to content

Commit

Permalink
don't segfault if MagickGetImageBlob returns null
Browse files Browse the repository at this point in the history
  • Loading branch information
BeatButton committed Apr 7, 2023
1 parent 1cfbc83 commit c8fdace
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/wand/magick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit c8fdace

Please sign in to comment.