From 98083ccd0a6d45e4d87a60faa1b1481d81491c52 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 31 Oct 2021 01:11:09 +0900 Subject: [PATCH] internal/graphicsdriver/opengl: Avoid heap allocation of a local variable If a variable is passed to an argument interface{}, the variable might be allocated on the heap unexpectedly. --- internal/graphicsdriver/opengl/program.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/internal/graphicsdriver/opengl/program.go b/internal/graphicsdriver/opengl/program.go index f1777d52c..4b00f53f7 100644 --- a/internal/graphicsdriver/opengl/program.go +++ b/internal/graphicsdriver/opengl/program.go @@ -273,7 +273,7 @@ func (g *Graphics) useProgram(program program, uniforms []uniformVariable, textu if len(u.value.Float32s) == 0 { if u.typ.Main != shaderir.Float { expected := &shaderir.Type{Main: shaderir.Float} - got := &u.typ + got := u.typ return fmt.Errorf("opengl: uniform variable %s type doesn't match: expected %s but %s", u.name, expected.String(), got.String()) } @@ -289,7 +289,11 @@ func (g *Graphics) useProgram(program program, uniforms []uniformVariable, textu g.state.lastUniforms[u.name] = u.value } else { if got, expected := len(u.value.Float32s), u.typ.FloatNum(); got != expected { - return fmt.Errorf("opengl: length of a uniform variables %s (%s) doesn't match: expected %d but %d", u.name, u.typ.String(), expected, got) + // Copy a shaderir.Type value once. Do not pass u.typ directly to fmt.Errorf arguments, or + // the value u would be allocated on heap. + typ := u.typ + return fmt.Errorf("opengl: length of a uniform variables %s (%s) doesn't match: expected %d but %d", u.name, typ.String(), expected, got) + return nil } cached, ok := g.state.lastUniforms[u.name]