]> Cypherpunks repositories - gostls13.git/commit
encoding/json/v2: report EOF for top-level values in UnmarshalDecode
authorJoe Tsai <joetsai@digital-static.net>
Sat, 11 Oct 2025 00:56:04 +0000 (17:56 -0700)
committerJoseph Tsai <joetsai@digital-static.net>
Mon, 13 Oct 2025 17:44:23 +0000 (10:44 -0700)
commit0e64ee1286c092eca95b3ffcc5917d34f43d4c0f
treeb5d2e8be0c0b4eb951503f74374299d0f5ddfd36
parent6bcd97d9f4386528aa85eb3cc27da0ed902de870
encoding/json/v2: report EOF for top-level values in UnmarshalDecode

The fully streaming UnmarshalJSONFrom method and UnmarshalFromFunc
introduce an edge case where they can encounter EOF in the stream,
where it should be reported upstream as EOF rather than
ErrUnexpectedEOF or be wrapped within a SemanticError.

This is not possible with other unmarshal methods since the
"json" package would read the appropriate JSON value
before calling the custom method or function.

To avoid custom unmarshal methods from encountering EOF,
check whether the stream is already at EOF for top-level values
before calling the custom method.

Also, when wrapping EOF within a SemanticError, convert it
to ErrUnexpectedEOF to better indicate that this is unexpected.

Fixes #75802

Change-Id: I001396734b7e95b5337f77b71326284974ee730a
Reviewed-on: https://go-review.googlesource.com/c/go/+/710877
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
Reviewed-by: Michael Pratt <mpratt@google.com>
Reviewed-by: Carlos Amedee <carlos@golang.org>
src/encoding/json/jsontext/decode.go
src/encoding/json/v2/arshal.go
src/encoding/json/v2/arshal_funcs.go
src/encoding/json/v2/arshal_methods.go
src/encoding/json/v2/arshal_test.go
src/encoding/json/v2/errors.go