From 7240fcfa95e1772f752425d8edfa048affe62d12 Mon Sep 17 00:00:00 2001 From: Jorropo Date: Fri, 18 Aug 2023 15:51:07 +0200 Subject: [PATCH] routing/http: do not leak a bytes.Buffer in drjson --- routing/http/internal/drjson/json.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/routing/http/internal/drjson/json.go b/routing/http/internal/drjson/json.go index 3bc3ab942..91b91332d 100644 --- a/routing/http/internal/drjson/json.go +++ b/routing/http/internal/drjson/json.go @@ -5,22 +5,22 @@ import ( "encoding/json" ) -func marshalJSON(val any) (*bytes.Buffer, error) { +func marshalJSON(val any) ([]byte, error) { buf := &bytes.Buffer{} enc := json.NewEncoder(buf) enc.SetEscapeHTML(false) err := enc.Encode(val) - return buf, err + return buf.Bytes(), err } // MarshalJSONBytes is needed to avoid changes // on the original bytes due to HTML escapes. func MarshalJSONBytes(val any) ([]byte, error) { - buf, err := marshalJSON(val) + bytes, err := marshalJSON(val) if err != nil { return nil, err } // remove last \n added by Encode - return buf.Bytes()[:buf.Len()-1], nil + return bytes[:len(bytes)-1], nil }