Skip to content

math/big: nil *Int appears to not roundtrip with MarshalText and UnmarshalText #60489

@mvdan

Description

@mvdan

https://go.dev/play/p/uV8Vr7trqBv

https://pkg.go.dev/encoding#TextUnmarshaler says:

UnmarshalText must be able to decode the form generated by MarshalText.

MarshalText clearly supports a nil *big.Int value, marshaling it as <nil>, but UnmarshalText rejects it. I think this goes against the TextUnmarshaler docs.

I'm not arguing that marshaling and unmarshaling a nil big int makes any sense, for what it's worth. Just that I always assumed, like the docs say, that roundtripping a value via marshal and then unmarshal was a safe thing to do. If marshaling allows for a nil pointer, unmarshal should support it too - although I'm not sure what it could possibly do, given that it can't set a *big.Int receiver to nil. Perhaps set it to its zero value?

Note that MarshalJSON and UnmarshalJSON don't have this problem. They both support null, and the unmarshal is simply a no-op. Presumably we could do the same in UnmarshalText.

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions