From daa0efc22b8c9b3e62cf1e84bd84d3218ee88f12 Mon Sep 17 00:00:00 2001 From: "Philip K. Warren" Date: Tue, 27 Jun 2023 08:01:03 -0500 Subject: [PATCH] Simplify code when pooled buffers aren't returned (#532) --- envelope.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/envelope.go b/envelope.go index 098dd3a6..936495d1 100644 --- a/envelope.go +++ b/envelope.go @@ -203,15 +203,12 @@ func (r *envelopeReader) Unmarshal(message any) *Error { // stream. Save the message for protocol-specific code to process and // return a sentinel error. Since we've deferred functions to return env's // underlying buffer to a pool, we need to keep a copy. + copiedData := make([]byte, data.Len()) + copy(copiedData, data.Bytes()) r.last = envelope{ - Data: r.bufferPool.Get(), + Data: bytes.NewBuffer(copiedData), Flags: env.Flags, } - // Don't return last to the pool! We're going to reference the data - // elsewhere. - if _, err := r.last.Data.ReadFrom(data); err != nil { - return errorf(CodeUnknown, "copy final envelope: %w", err) - } return errSpecialEnvelope }