From 8a7c94001fc715f7e34b6aac83a6079546901af1 Mon Sep 17 00:00:00 2001 From: Justin Israel Date: Thu, 4 Apr 2024 09:20:55 +1300 Subject: [PATCH] Check last error after getting blob, before calling conversions (refs #308) --- imagick/magick_wand_image.go | 6 ++++++ imagick/magick_wand_test.go | 8 +++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/imagick/magick_wand_image.go b/imagick/magick_wand_image.go index 1ca6ff8..75009ea 100644 --- a/imagick/magick_wand_image.go +++ b/imagick/magick_wand_image.go @@ -942,6 +942,9 @@ func (mw *MagickWand) GetImageBlob() []byte { clen := C.size_t(0) csblob := C.MagickGetImageBlob(mw.mw, &clen) defer relinquishMemory(unsafe.Pointer(csblob)) + if err := mw.GetLastError(); err != nil { + return nil + } ret := C.GoBytes(unsafe.Pointer(csblob), C.int(clen)) runtime.KeepAlive(mw) return ret @@ -957,6 +960,9 @@ func (mw *MagickWand) GetImagesBlob() []byte { clen := C.size_t(0) csblob := C.MagickGetImagesBlob(mw.mw, &clen) defer relinquishMemory(unsafe.Pointer(csblob)) + if err := mw.GetLastError(); err != nil { + return nil + } runtime.KeepAlive(mw) return C.GoBytes(unsafe.Pointer(csblob), C.int(clen)) } diff --git a/imagick/magick_wand_test.go b/imagick/magick_wand_test.go index 1e80846..5779dcd 100644 --- a/imagick/magick_wand_test.go +++ b/imagick/magick_wand_test.go @@ -233,8 +233,14 @@ func TestReadImageBlob(t *testing.T) { mw := NewMagickWand() + // Try to read an empty wand + blob := mw.GetImageBlob() + if blob != nil { + t.Fatal("Expected nil blob on invalid wand") + } + // Read an invalid blob - blob := []byte{} + blob = []byte{} if err := mw.ReadImageBlob(blob); err == nil { t.Fatal("Expected a failure when passing a zero length blob") }