ebiten: Panic immediately when zero size is given to NewImage(FromImage)

This commit is contained in:
Hajime Hoshi 2020-10-23 00:42:57 +09:00
parent d6eac8c5bf
commit 2259378430
2 changed files with 33 additions and 0 deletions

View File

@ -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. // If width or height is less than 1 or more than device-dependent maximum size, NewImage panics.
func NewImage(width, height int) *Image { 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{ i := &Image{
mipmap: mipmap.New(width, height), mipmap: mipmap.New(width, height),
bounds: image.Rect(0, 0, width, height), bounds: image.Rect(0, 0, width, height),
@ -731,6 +737,12 @@ func NewImageFromImage(source image.Image) *Image {
size := source.Bounds().Size() size := source.Bounds().Size()
width, height := size.X, size.Y 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{ i := &Image{
mipmap: mipmap.New(width, height), mipmap: mipmap.New(width, height),

View File

@ -2160,3 +2160,24 @@ func TestImageDrawImageCannotAllocateImageForMipmap(t *testing.T) {
dst.DrawImage(src, op) dst.DrawImage(src, op)
dst.At(0, 0) 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)
}