Skip to content

Commit

Permalink
Keep value in scope until CBOR encoding completes to avoid a finalize…
Browse files Browse the repository at this point in the history
…r running prematurely

Signed-off-by: Ben Krieger <[email protected]>
  • Loading branch information
ben-krieger committed Dec 4, 2024
1 parent a93f721 commit 2181edd
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cbor/cbor.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ import (
"io"
"math"
"reflect"
"runtime"
"slices"
"sort"
"strconv"
Expand Down Expand Up @@ -1084,6 +1085,11 @@ func (e *Encoder) write(b []byte) error {
//
//nolint:gocyclo // Dispatch will always have naturally high complexity.
func (e *Encoder) Encode(v any) error {
// Reflection does not keep the underlying value in scope, so this is
// needed to keep finalizers from running and possibly modifying the value
// being encoded (such as zeroing secrets).
defer runtime.KeepAlive(v)

// Use reflection to dereference pointers, get concrete types out of
// interfaces, and unwrap named types
rv := reflect.ValueOf(v)
Expand Down

0 comments on commit 2181edd

Please sign in to comment.