Skip to content

helpers: MarshalToWriter: return jw.Error before writing#440

Open
omkhar wants to merge 1 commit into
mailru:masterfrom
omkhar:omkhar/marshaltowriter-honor-error
Open

helpers: MarshalToWriter: return jw.Error before writing#440
omkhar wants to merge 1 commit into
mailru:masterfrom
omkhar:omkhar/marshaltowriter-honor-error

Conversation

@omkhar

@omkhar omkhar commented Jun 27, 2026

Copy link
Copy Markdown

Problem

MarshalToWriter writes the buffer to the io.Writer and returns a nil error even when encoding set jw.Error mid-stream. When a value sets w.Error (today: a +Inf/-Inf/NaN float, rejected since #421 / issue #134), the writer has already buffered a truncated/invalid document; DumpTo flushes those raw bytes to the destination and returns only its own (nil) error. The caller gets invalid/truncated JSON on the wire with no error.

The sibling MarshalToHTTPResponseWriter already gates on jw.Error before touching the writer; MarshalToWriter is the one wrapper that does not.

Fix

Mirror the sibling: return jw.Error before DumpTo, so no partial document is written and the caller is told.

  • helpers.go: +3 lines.
  • No-op for valid input (jw.Error is nil on success); strictly aligns MarshalToWriter's error contract with MarshalToHTTPResponseWriter.
  • go test ./... passes.

Related: #134, #421 (the NaN/Inf guard that this path was never updated to honor).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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