Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
tillulen opened this issue Aug 2, 2017 · 4 comments
Closed

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

tillulen opened this issue Aug 2, 2017 · 4 comments
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@tillulen
Copy link

tillulen commented Aug 2, 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:

...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.

@ALTree ALTree added this to the Go1.10 milestone Aug 3, 2017
@ALTree ALTree added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Aug 3, 2017
@tillulen
Copy link
Author

tillulen commented Aug 3, 2017

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.

@rsc
Copy link
Contributor

rsc commented Aug 7, 2017

The docs seem clear. Given that, let's fix the bug.

@rsc rsc added the NeedsFix The path to resolution is known, but the work has not been done. label Aug 7, 2017
@gopherbot gopherbot removed the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Aug 7, 2017
@griesemer
Copy link
Contributor

Assigned to @martisch per discussion with him.

@gopherbot
Copy link

Change https://golang.org/cl/55134 mentions this issue: strconv: fix ParseUint return value on range overflow

@golang golang locked and limited conversation to collaborators Aug 13, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

6 participants