Skip to content

strconv: ParseUint() always returns MaxUint64 on overflow (ErrRange) #21278

@tillulen

Description

@tillulen

The documentation for ParseUint() is terse: “ParseUint is like ParseInt but for unsigned numbers.”

The documentation for ParseInt() suggests that when the input string represents a number that is too big to fit in bitSize bits, ParseInt returns the most extreme value that still fits in bitSize bits:

...if the value corresponding to s cannot be represented by a signed integer of the given size, err.Err = ErrRange and the returned value is the maximum magnitude integer of the appropriate bitSize and sign.

One would expect that ParseUint behaves in a similar way and that the following call would return 255 and ErrRange:

u, err := strconv.ParseUint("256", 10, 8)
fmt.Println(u, err)

ParseUint in Go 1.8 actually disregards the provided bitSize and always returns MaxUint64 along with ErrRange. The above prints

18446744073709551615 strconv.ParseUint: parsing "256": value out of range

Please find a runnable example on the Go Play Space or the Go Playground.

For consistency, it’s better to fix ParseUint to match the docs and the behavior of ParseInt. For backward compatibility, it is safer to leave ParseUint as is and update its documentation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions