Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check last error after query to FontMetrics to prevent panic (fixes #309) #310

Merged
merged 1 commit into from
Apr 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions imagick/magick_wand.go
Original file line number Diff line number Diff line change
@@ -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))
@@ -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))
18 changes: 18 additions & 0 deletions imagick/magick_wand_test.go
Original file line number Diff line number Diff line change
@@ -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) {
Loading
Oops, something went wrong.