From fa3ec1273187f0eb12a11ee8eeac11c30d3b1e2d Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 2 Dec 2023 22:59:06 +0900 Subject: [PATCH] text/v2: bug fix: the given slice to MultiFace should be copied Updates #2845 --- text/v2/multi.go | 7 ++++--- text/v2/text_test.go | 11 +++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/text/v2/multi.go b/text/v2/multi.go index a44169fdb..8797255f4 100644 --- a/text/v2/multi.go +++ b/text/v2/multi.go @@ -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. diff --git a/text/v2/text_test.go b/text/v2/text_test.go index 21b258012..a76051d27 100644 --- a/text/v2/text_test.go +++ b/text/v2/text_test.go @@ -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) +}