From 7a087373263fa9be5989e8ccf2b8fe991d4e9296 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 19 Nov 2023 23:15:18 +0900 Subject: [PATCH] text/v2: add copyCheck to GoTextFaceSource --- text/v2/gotextfacesource.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/text/v2/gotextfacesource.go b/text/v2/gotextfacesource.go index 92cd96150..3e3c8c45f 100644 --- a/text/v2/gotextfacesource.go +++ b/text/v2/gotextfacesource.go @@ -58,6 +58,8 @@ type GoTextFaceSource struct { outputCache map[goTextOutputCacheKey]*goTextOutputCacheValue + addr *GoTextFaceSource + m sync.Mutex } @@ -95,6 +97,7 @@ func NewGoTextFaceSource(source io.ReadSeeker) (*GoTextFaceSource, error) { f: f, id: nextUniqueID(), } + s.addr = s runtime.SetFinalizer(s, finalizeGoTextFaceSource) return s, nil } @@ -117,6 +120,7 @@ func NewGoTextFaceSourcesFromCollection(source io.ReadSeeker) ([]*GoTextFaceSour f: f, id: nextUniqueID(), } + s.addr = s runtime.SetFinalizer(s, finalizeGoTextFaceSource) sources[i] = s } @@ -130,7 +134,15 @@ func finalizeGoTextFaceSource(source *GoTextFaceSource) { }) } +func (g *GoTextFaceSource) copyCheck() { + if g.addr != g { + panic("text: illegal use of non-zero GoTextFaceSource copied by value") + } +} + func (g *GoTextFaceSource) shape(text string, face *GoTextFace) (shaping.Output, []glyph) { + g.copyCheck() + g.m.Lock() defer g.m.Unlock()