Skip to content

Commit

Permalink
Check last error after query to FontMetrics to prevent panic (fixes #309
Browse files Browse the repository at this point in the history
)
  • Loading branch information
justinfx committed Apr 3, 2024
1 parent 734ab12 commit 31e5640
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions imagick/magick_wand.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ func (mw *MagickWand) QueryFontMetrics(dw *DrawingWand, textLine string) *FontMe
cstext := C.CString(textLine)
defer C.free(unsafe.Pointer(cstext))
cdoubles := C.MagickQueryFontMetrics(mw.mw, dw.dw, cstext)
if err := mw.GetLastError(); err != nil {
return nil
}
runtime.KeepAlive(mw)
runtime.KeepAlive(dw)
defer relinquishMemory(unsafe.Pointer(cdoubles))
Expand All @@ -141,6 +144,9 @@ func (mw *MagickWand) QueryMultilineFontMetrics(dw *DrawingWand, textParagraph s
cstext := C.CString(textParagraph)
defer C.free(unsafe.Pointer(cstext))
cdoubles := C.MagickQueryMultilineFontMetrics(mw.mw, dw.dw, cstext)
if err := mw.GetLastError(); err != nil {
return nil
}
runtime.KeepAlive(mw)
runtime.KeepAlive(dw)
defer relinquishMemory(unsafe.Pointer(cdoubles))
Expand Down
18 changes: 18 additions & 0 deletions imagick/magick_wand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,24 @@ func TestQueryFonts(t *testing.T) {
}
}

func TestQueryFontMetrics(t *testing.T) {
Initialize()
defer func(t *testing.T) {
checkGC(t)
}(t)
defer Terminate()

mw := NewMagickWand()
// Create an empty wand. Don't ready anything.
//if err := mw.ReadImage("xc:black"); err != nil {
// panic(err)
//}
fonts := mw.QueryFontMetrics(NewDrawingWand(), "")
if fonts != nil {
t.Fatal("Expected a nil FontMetrics when passing a bad MagickWand")
}
}

func TestQueryFormats(t *testing.T) {
Initialize()
defer func(t *testing.T) {
Expand Down

0 comments on commit 31e5640

Please sign in to comment.