[go] encoding/json/jsontext: allow nil io.Reader and io.Writer in Reset

0 views
Skip to first unread message

Ilya Gellert (Gerrit)

unread,
Jun 30, 2026, 7:13:22 PM (5 hours ago) Jun 30
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Ilya Gellert has uploaded the change for review

Commit message

encoding/json/jsontext: allow nil io.Reader and io.Writer in Reset

This change removes the panic when a nil io.Reader or io.Writer
is passed to Decoder.Reset and Encoder.Reset. This allows users
to safely clear references to underlying streams before returning
Decoders and Encoders to a sync.Pool, enabling the garbage
collector to reclaim the stream memory while preserving the
internal buffer capacity for future reuse.

Subsequent reads or writes will still panic as expected when
operating on a nil stream.

Fixes golang/go#80117
Change-Id: I7ec702a12a2b2f1ae00b93787bc74bf41cfd0bc0

Change diff

diff --git a/src/encoding/json/jsontext/decode.go b/src/encoding/json/jsontext/decode.go
index 0e1ff4b..e04ecfc 100644
--- a/src/encoding/json/jsontext/decode.go
+++ b/src/encoding/json/jsontext/decode.go
@@ -133,8 +133,6 @@
switch {
case d == nil:
panic("jsontext: invalid nil Decoder")
- case r == nil:
- panic("jsontext: invalid nil io.Reader")
case d.s.Flags.Get(jsonflags.WithinArshalCall):
panic("jsontext: cannot reset Decoder passed to json.UnmarshalerFrom")
}
diff --git a/src/encoding/json/jsontext/decode_test.go b/src/encoding/json/jsontext/decode_test.go
index 3f48cae..93a6023 100644
--- a/src/encoding/json/jsontext/decode_test.go
+++ b/src/encoding/json/jsontext/decode_test.go
@@ -1347,4 +1347,13 @@
t.Fatalf("decoder buffer aliases bytes.Buffer")
}
})
+
+ t.Run("Test ability to reset nil reader", func(t *testing.T) {
+ defer func() {
+ if recover() != nil {
+ t.Fatalf("decoder.Reset(nil) shouldn`t panic")
+ }
+ }()
+ dec.Reset(nil)
+ })
}
diff --git a/src/encoding/json/jsontext/encode.go b/src/encoding/json/jsontext/encode.go
index 116e150..c5fedbe 100644
--- a/src/encoding/json/jsontext/encode.go
+++ b/src/encoding/json/jsontext/encode.go
@@ -102,8 +102,6 @@
switch {
case e == nil:
panic("jsontext: invalid nil Encoder")
- case w == nil:
- panic("jsontext: invalid nil io.Writer")
case e.s.Flags.Get(jsonflags.WithinArshalCall):
panic("jsontext: cannot reset Encoder passed to json.MarshalerTo")
}
diff --git a/src/encoding/json/jsontext/encode_test.go b/src/encoding/json/jsontext/encode_test.go
index c5dcbdf..fa1046c 100644
--- a/src/encoding/json/jsontext/encode_test.go
+++ b/src/encoding/json/jsontext/encode_test.go
@@ -826,4 +826,13 @@
t.Fatalf("encoder buffer aliases bytes.Buffer")
}
})
+
+ t.Run("Test ability to reset nil writer", func(t *testing.T) {
+ defer func() {
+ if recover() != nil {
+ t.Fatalf("encoder.Reset(nil) shouldn`t panic")
+ }
+ }()
+ enc.Reset(nil)
+ })
}

Change information

Files:
  • M src/encoding/json/jsontext/decode.go
  • M src/encoding/json/jsontext/decode_test.go
  • M src/encoding/json/jsontext/encode.go
  • M src/encoding/json/jsontext/encode_test.go
Change size: S
Delta: 4 files changed, 18 insertions(+), 4 deletions(-)
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I7ec702a12a2b2f1ae00b93787bc74bf41cfd0bc0
Gerrit-Change-Number: 795960
Gerrit-PatchSet: 1
Gerrit-Owner: Ilya Gellert <gellert...@gmail.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

Emmanuel Odeke (Gerrit)

unread,
Jun 30, 2026, 7:37:41 PM (5 hours ago) Jun 30
to Ilya Gellert, goph...@pubsubhelper.golang.org, Russ Cox, Joseph Tsai, Brad Fitzpatrick, Daniel Martí, Gopher Robot, golang-co...@googlegroups.com
Attention needed from Ilya Gellert and Russ Cox

Emmanuel Odeke voted Commit-Queue+1

Commit-Queue+1
Open in Gerrit

Related details

Attention is currently required from:
  • Ilya Gellert
  • Russ Cox
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I7ec702a12a2b2f1ae00b93787bc74bf41cfd0bc0
Gerrit-Change-Number: 795960
Gerrit-PatchSet: 1
Gerrit-Owner: Ilya Gellert <gellert...@gmail.com>
Gerrit-Reviewer: Emmanuel Odeke <emma...@orijtech.com>
Gerrit-Reviewer: Russ Cox <r...@golang.org>
Gerrit-CC: Brad Fitzpatrick <brad...@golang.org>
Gerrit-CC: Daniel Martí <mv...@mvdan.cc>
Gerrit-CC: Gopher Robot <go...@golang.org>
Gerrit-CC: Joseph Tsai <joe...@digital-static.net>
Gerrit-Attention: Russ Cox <r...@golang.org>
Gerrit-Attention: Ilya Gellert <gellert...@gmail.com>
Gerrit-Comment-Date: Tue, 30 Jun 2026 23:37:36 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
unsatisfied_requirement
satisfied_requirement
open
diffy
Reply all
Reply to author
Forward
0 new messages