graphics: Forbid nil at ReplacePixels explicitly

This commit is contained in:
Hajime Hoshi 2018-11-28 22:24:04 +01:00
parent 1f538af440
commit 1a54ff34e6
3 changed files with 23 additions and 3 deletions

View File

@ -15,6 +15,7 @@
package ebiten package ebiten
import ( import (
"fmt"
"image" "image"
"image/color" "image/color"
"math" "math"
@ -616,6 +617,10 @@ func (i *Image) ReplacePixels(p []byte) error {
if i.isSubimage() { if i.isSubimage() {
panic("render to a subimage is not implemented") panic("render to a subimage is not implemented")
} }
s := i.Bounds().Size()
if l := 4 * s.X * s.Y; len(p) != l {
panic(fmt.Sprintf("ebiten: len(p) was %d but must be %d", len(p), l))
}
i.mipmap.original().ReplacePixels(p) i.mipmap.original().ReplacePixels(p)
i.disposeMipmaps() i.disposeMipmaps()
return nil return nil

View File

@ -368,6 +368,22 @@ func TestImageReplacePixels(t *testing.T) {
} }
} }
func TestImageReplacePixelsNil(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("ReplacePixels(nil) must panic")
}
}()
img, err := NewImage(16, 16, FilterNearest)
if err != nil {
t.Fatal(err)
return
}
img.Fill(color.White)
img.ReplacePixels(nil)
}
func TestImageDispose(t *testing.T) { func TestImageDispose(t *testing.T) {
img, err := NewImage(16, 16, FilterNearest) img, err := NewImage(16, 16, FilterNearest)
if err != nil { if err != nil {
@ -799,7 +815,7 @@ func TestImageSize4096(t *testing.T) {
func TestImageCopy(t *testing.T) { func TestImageCopy(t *testing.T) {
defer func() { defer func() {
if r := recover(); r == nil { if r := recover(); r == nil {
t.Errorf("copying image and using it should panic") t.Errorf("copying image and using it must panic")
} }
}() }()

View File

@ -15,7 +15,6 @@
package shareable package shareable
import ( import (
"fmt"
"image" "image"
"image/color" "image/color"
"runtime" "runtime"
@ -259,7 +258,7 @@ func (i *Image) replacePixels(p []byte) {
x, y, w, h := i.region() x, y, w, h := i.region()
if p != nil { if p != nil {
if l := 4 * w * h; len(p) != l { if l := 4 * w * h; len(p) != l {
panic(fmt.Sprintf("shareable: len(p) was %d but must be %d", len(p), l)) panic("not reached")
} }
} }
i.backend.restorable.ReplacePixels(p, x, y, w, h) i.backend.restorable.ReplacePixels(p, x, y, w, h)