diff --git a/image.go b/image.go index f043eda0e..72fef61c6 100644 --- a/image.go +++ b/image.go @@ -716,6 +716,12 @@ func (i *Image) ReplacePixels(pixels []byte) { // // If width or height is less than 1 or more than device-dependent maximum size, NewImage panics. func NewImage(width, height int) *Image { + if width <= 0 { + panic(fmt.Sprintf("ebiten: width at NewImage must be positive but %d", width)) + } + if height <= 0 { + panic(fmt.Sprintf("ebiten: height at NewImage must be positive but %d", height)) + } i := &Image{ mipmap: mipmap.New(width, height), bounds: image.Rect(0, 0, width, height), @@ -731,6 +737,12 @@ func NewImageFromImage(source image.Image) *Image { size := source.Bounds().Size() width, height := size.X, size.Y + if width <= 0 { + panic(fmt.Sprintf("ebiten: source width at NewImageFromImage must be positive but %d", width)) + } + if height <= 0 { + panic(fmt.Sprintf("ebiten: source height at NewImageFromImage must be positive but %d", height)) + } i := &Image{ mipmap: mipmap.New(width, height), diff --git a/image_test.go b/image_test.go index cf9def802..952c29d84 100644 --- a/image_test.go +++ b/image_test.go @@ -2160,3 +2160,24 @@ func TestImageDrawImageCannotAllocateImageForMipmap(t *testing.T) { dst.DrawImage(src, op) dst.At(0, 0) } + +func TestImageNewImageWithZeroSize(t *testing.T) { + defer func() { + if r := recover(); r == nil { + t.Errorf("DrawImage must panic but not") + } + }() + + _ = NewImage(0, 1) +} + +func TestImageNewImageFromImageWithZeroSize(t *testing.T) { + defer func() { + if r := recover(); r == nil { + t.Errorf("DrawImage must panic but not") + } + }() + + img := image.NewRGBA(image.Rect(0, 0, 0, 1)) + _ = NewImageFromImage(img) +}