Skip to content

Commit

Permalink
routing/http: do not leak a bytes.Buffer in drjson
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorropo committed Aug 21, 2023
1 parent db4e43a commit 7240fcf
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions routing/http/internal/drjson/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 7240fcf

Please sign in to comment.