text/v2: bug fix: the given slice to MultiFace should be copied

Updates #2845
This commit is contained in:
Hajime Hoshi 2023-12-02 22:59:06 +09:00
parent dfa058a961
commit fa3ec12731
2 changed files with 15 additions and 3 deletions

View File

@ -32,9 +32,10 @@ type MultiFace struct {
// NewMultiFace creates a new MultiFace from the given faces.
func NewMultiFace(faces ...Face) *MultiFace {
return &MultiFace{
faces: faces,
}
m := &MultiFace{}
m.faces = make([]Face, len(faces))
copy(m.faces, faces)
return m
}
// Metrics implements Face.

View File

@ -240,3 +240,14 @@ func TestUnhashableFace(t *testing.T) {
}
}
}
func TestMultiFace(t *testing.T) {
faces := []text.Face{text.NewStdFace(bitmapfont.Face)}
f := text.NewMultiFace(faces...)
img := ebiten.NewImage(30, 30)
text.Draw(img, "Hello", f, nil)
// Confirm that the given slice doesn't cause crash.
faces[0] = nil
text.Draw(img, "World", f, nil)
}