Skip to content

Guard nil pointers in MarshalIndent / MarshalText sibling paths - #588

Open
mahirhir wants to merge 1 commit into
goccy:masterfrom
mahirhir:fix/marshal-nil-sibling-guards
Open

Guard nil pointers in MarshalIndent / MarshalText sibling paths#588
mahirhir wants to merge 1 commit into
goccy:masterfrom
mahirhir:fix/marshal-nil-sibling-guards

Conversation

@mahirhir

Copy link
Copy Markdown

Merged PR #524 added a nil-pointer guard to AppendMarshalJSON, but the same indirection-without-nil-check exists in three sibling helpers in internal/encoder/encoder.go: AppendMarshalJSONIndent, AppendMarshalText, AppendMarshalTextIndent. So json.MarshalIndent([]*time.Time{nil}) and slices of nil *T implementing TextMarshaler with a value receiver panic instead of emitting null, diverging from stdlib encoding/json.

The fix ports the same guard #524 added into the three sibling paths. Verified both ways with go test: old panics on all three, fixed emits null/indented; the existing TestNilMarshal (nil-safe-receiver) still passes, full suite green, 0 regression.

…ode paths

The nil-pointer guard added in goccy#524 (for []*time.Time via AppendMarshalJSON)
was missing from the three sibling encode helpers: AppendMarshalJSONIndent,
AppendMarshalText and AppendMarshalTextIndent. Encoding a nil pointer whose
Marshaler/TextMarshaler method has a value/deref receiver (e.g.
json.MarshalIndent([]*time.Time{nil}), or a slice of nil *T implementing
TextMarshaler) panicked instead of emitting null, diverging from encoding/json.

Apply the same `rv.Kind() == reflect.Ptr && rv.IsNil()` guard to all three
siblings and add slice tests covering the JSON-indent and text/text-indent paths.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant