Skip to content

Commit

Permalink
Add check for cols/rows params to avoid divide-by-zero error (SIGFPE)…
Browse files Browse the repository at this point in the history
… in C (refs #278)
  • Loading branch information
justinfx committed Jun 9, 2022
1 parent eed3318 commit 88a9b60
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions imagick/magick_wand_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ import (
"unsafe"
)

func checkColsRows(cols, rows uint) error {
if cols == 0 {
return errors.New("bad parameter value: cols=0")
}
if rows == 0 {
return errors.New("bad parameter value: rows=0")
}
return nil
}

// Returns the current image from the magick wand
func (mw *MagickWand) GetImageFromMagickWand() *Image {
return &Image{C.GetImageFromMagickWand(mw.mw)}
Expand All @@ -48,6 +58,9 @@ func (mw *MagickWand) AdaptiveBlurImage(radius, sigma float64) error {

// Adaptively resize image with data dependent triangulation
func (mw *MagickWand) AdaptiveResizeImage(cols, rows uint) error {
if err := checkColsRows(cols, rows); err != nil {
return err
}
ok := C.MagickAdaptiveResizeImage(mw.mw, C.size_t(cols), C.size_t(rows))
return mw.getLastErrorIfFailed(ok)
}
Expand Down Expand Up @@ -526,6 +539,9 @@ func (mw *MagickWand) CycleColormapImage(displace int) error {
// type.
//
func (mw *MagickWand) ConstituteImage(cols, rows uint, pmap string, stype StorageType, pixels interface{}) error {
if err := checkColsRows(cols, rows); err != nil {
return err
}
cspmap := C.CString(pmap)
defer C.free(unsafe.Pointer(cspmap))
ptr, calculatedStype, err := pixelInterfaceToPtr(pixels)
Expand Down Expand Up @@ -1493,6 +1509,10 @@ func pixelInterfaceToPtr(pixels interface{}) (unsafe.Pointer, StorageType, error
func (mw *MagickWand) ImportImagePixels(x, y int, cols, rows uint, pmap string,
stype StorageType, pixels interface{}) error {

if err := checkColsRows(cols, rows); err != nil {
return err
}

cspmap := C.CString(pmap)
defer C.free(unsafe.Pointer(cspmap))

Expand Down Expand Up @@ -1563,6 +1583,9 @@ func (mw *MagickWand) LinearStretchImage(blackPoint, whitePoint float64) error {
// rigidity: introduce a bias for non-straight seams (typically 0).
//
func (mw *MagickWand) LiquidRescaleImage(cols, rows uint, deltaX, rigidity float64) error {
if err := checkColsRows(cols, rows); err != nil {
return err
}
ok := C.MagickLiquidRescaleImage(mw.mw, C.size_t(cols), C.size_t(rows), C.double(deltaX), C.double(rigidity))
return mw.getLastErrorIfFailed(ok)
}
Expand Down Expand Up @@ -2068,6 +2091,9 @@ func (mw *MagickWand) ResetImagePage(page string) error {
// filter: Image filter to use.
//
func (mw *MagickWand) ResizeImage(cols, rows uint, filter FilterType) error {
if err := checkColsRows(cols, rows); err != nil {
return err
}
ok := C.MagickResizeImage(mw.mw, C.size_t(cols), C.size_t(rows), C.FilterType(filter))
return mw.getLastErrorIfFailed(ok)
}
Expand Down Expand Up @@ -2100,12 +2126,18 @@ func (mw *MagickWand) RotateImage(background *PixelWand, degrees float64) error
// scaling methods, this method does not introduce any additional color into
// the scaled image.
func (mw *MagickWand) SampleImage(cols, rows uint) error {
if err := checkColsRows(cols, rows); err != nil {
return err
}
ok := C.MagickSampleImage(mw.mw, C.size_t(cols), C.size_t(rows))
return mw.getLastErrorIfFailed(ok)
}

// Scales the size of an image to the given dimensions.
func (mw *MagickWand) ScaleImage(cols, rows uint) error {
if err := checkColsRows(cols, rows); err != nil {
return err
}
ok := C.MagickScaleImage(mw.mw, C.size_t(cols), C.size_t(rows))
return mw.getLastErrorIfFailed(ok)
}
Expand Down Expand Up @@ -2738,6 +2770,9 @@ func (mw *MagickWand) ThresholdImageChannel(channel ChannelType, threshold float
// associated profiles. The goal is to produce small low cost thumbnail images
// suited for display on the Web.
func (mw *MagickWand) ThumbnailImage(cols, rows uint) error {
if err := checkColsRows(cols, rows); err != nil {
return err
}
ok := C.MagickThumbnailImage(mw.mw, C.size_t(cols), C.size_t(rows))
return mw.getLastErrorIfFailed(ok)
}
Expand Down

0 comments on commit 88a9b60

Please sign in to comment.