Skip to content

Commit

Permalink
text/v2: bug fix: the given slice to MultiFace should be copied
Browse files Browse the repository at this point in the history
  • Loading branch information
hajimehoshi committed Dec 2, 2023
1 parent dfa058a commit fa3ec12
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
7 changes: 4 additions & 3 deletions text/v2/multi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 11 additions & 0 deletions text/v2/text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit fa3ec12

Please sign in to comment.