From a59cf38c94700bd616ce1f9c1a15617219e50beb Mon Sep 17 00:00:00 2001 From: Noah Treuhaft Date: Wed, 10 Jun 2026 15:03:01 -0400 Subject: [PATCH] fix bug in super.Context.LookupByValue by copying parameter LookupByValue caches its parameter, a byte slice, in the context, so if the backing array is later modified through another reference, the cached value changes. This can cause LookupTypeValue to return an incorrect result since it returns these cached values. Fix this in LookupByValue by caching a copy of the paramter. --- context.go | 2 +- context_test.go | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/context.go b/context.go index c48f8230cd..a0983191af 100644 --- a/context.go +++ b/context.go @@ -386,7 +386,7 @@ func (c *Context) LookupByValue(tv scode.Bytes) (Type, error) { if rest == nil { return nil, errors.New("bad type value encoding") } - c.toValue[typ] = tv + c.toValue[typ] = slices.Clone(tv) c.toType[string(tv)] = typ return typ, nil } diff --git a/context_test.go b/context_test.go index 0548b2179d..f98e1e6ebf 100644 --- a/context_test.go +++ b/context_test.go @@ -8,6 +8,21 @@ import ( "github.com/stretchr/testify/require" ) +func TestContextLookupByValueAndLookupTypeValue(t *testing.T) { + sctx := super.NewContext() + recType := sctx.MustLookupTypeRecord(nil) + + tv := super.EncodeTypeValue(recType) + typ, err := sctx.LookupByValue(tv) + require.NoError(t, err) + assert.Equal(t, recType, typ) + + // Overwriting tv should not affect sctx's cached type value for recType. + super.AppendTypeValue(tv[:0], super.TypeNull) + val := sctx.LookupTypeValue(recType) + assert.Exactly(t, super.EncodeTypeValue(recType), val.Bytes()) +} + func TestContextLookupTypeNamedErrors(t *testing.T) { sctx := super.NewContext()