Skip to content

Commit

Permalink
Merge pull request #102 from BeatButton/write-image-blob-null
Browse files Browse the repository at this point in the history
don't segfault if MagickGetImageBlob returns null
  • Loading branch information
nlfiedler authored Apr 7, 2023
2 parents 1cfbc83 + c8fdace commit 257243c
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 257243c

Please sign in to comment.