From 7eef36c732df996ab6508bfb5cee17a7763097f1 Mon Sep 17 00:00:00 2001 From: Evan Phoenix Date: Sat, 27 Jan 2024 15:33:55 -0800 Subject: [PATCH] Guard using options to avoid a crash bug --- v5/patch.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/v5/patch.go b/v5/patch.go index d9e23f6..d658b16 100644 --- a/v5/patch.go +++ b/v5/patch.go @@ -142,13 +142,21 @@ func (n *partialDoc) TrustMarshalJSON(buf *bytes.Buffer) error { if err := buf.WriteByte('{'); err != nil { return err } + escaped := true + + // n.opts should always be set, but in case we missed a case, + // guard. + if n.opts != nil { + escaped = n.opts.EscapeHTML + } + for i, k := range n.keys { if i > 0 { if err := buf.WriteByte(','); err != nil { return err } } - key, err := json.MarshalEscaped(k, n.opts.EscapeHTML) + key, err := json.MarshalEscaped(k, escaped) if err != nil { return err } @@ -158,7 +166,7 @@ func (n *partialDoc) TrustMarshalJSON(buf *bytes.Buffer) error { if err := buf.WriteByte(':'); err != nil { return err } - value, err := json.MarshalEscaped(n.obj[k], n.opts.EscapeHTML) + value, err := json.MarshalEscaped(n.obj[k], escaped) if err != nil { return err }