You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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.
The text was updated successfully, but these errors were encountered:
Going over the release notes of the latest Go releases, I noticed that there were quite a few fixes that could potentially break user code dependent on the incorrect behavior. So I’m in favor of fixing ParseUint, for what it’s worth.
The docs seem clear. Given that, let's fix the bug.
rsc
added
the
NeedsFix
The path to resolution is known, but the work has not been done.
label
Aug 7, 2017
gopherbot
removed
the
NeedsDecision
Feedback is required from experts, contributors, and/or the community before a change can be made.
label
Aug 7, 2017
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:
One would expect that ParseUint behaves in a similar way and that the following call would return 255 and ErrRange:
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.
The text was updated successfully, but these errors were encountered: