Skip to content

Return error for non-finite float32 in Marshal, matching float64 - #597

Open
binggao1230 wants to merge 1 commit into
goccy:masterfrom
binggao1230:fix-marshal-float32-inf-nan
Open

Return error for non-finite float32 in Marshal, matching float64#597
binggao1230 wants to merge 1 commit into
goccy:masterfrom
binggao1230:fix-marshal-float32-inf-nan

Conversation

@binggao1230

Copy link
Copy Markdown

json.Marshal(float32(math.Inf(1))) returns the bytes +Inf with a nil error, which is invalid JSON. encoding/json and go-json's own float64 opcodes return *UnsupportedValueError for non-finite floats (RFC 8259 numbers can't represent Inf/NaN); the float32 opcodes never carried the guard.

b, err := json.Marshal(float32(math.Inf(1))) // b == "+Inf", err == nil; json.Valid(b) == false

The Inf/NaN check lives in the VM opcode template (internal/cmd/generator/vm.go.tmpl): every float64 opcode has it, no float32 opcode did. This mirrors it onto the float32 opcode family and regenerates the four vm variants, so bare/pointer/struct/slice/map float32 values are covered across plain, indent, color and color+indent output.

Tests assert float32 Inf/-Inf/NaN now return *UnsupportedValueError with no output, matching encoding/json and the existing float64 behaviour, while finite float32 still marshals unchanged.

Marshaling a float32 Inf/NaN emitted the literal bytes +Inf/-Inf/NaN with a
nil error, which is invalid JSON. encoding/json and the existing float64
opcodes return *UnsupportedValueError instead; the float32 opcodes never got
the guard. Mirror the float64 Inf/NaN check onto every float32 opcode in the
VM template and regenerate the four vm variants.
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