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

Negative uint rewinds to max #42806

Closed
ivanjaros opened this issue Nov 24, 2020 · 2 comments
Closed

Negative uint rewinds to max #42806

ivanjaros opened this issue Nov 24, 2020 · 2 comments

Comments

@ivanjaros
Copy link

What version of Go are you using (go version)?

go version go1.15 windows/amd64

When uint is decreased into negative value, it does not return constant .... overflows int error but instead it will return 18446744073709551615. So there is no overflow guard for unsigned integers.

This works as expected:

fmt.Println(1<<64)

returns error: constant 18446744073709551616 overflows int

this does not:

	var u uint
	fmt.Println(u)
	u--
	fmt.Println(u)

prints
0
18446744073709551615
@martisch
Copy link
Contributor

martisch commented Nov 24, 2020

Overflow at runtime is allowed https://golang.org/ref/spec#Integer_overflow.

Constants are checked for representability by the type at compile time. https://golang.org/ref/spec#Representability

@AlexRouSg
Copy link
Contributor

Might want to see #19624 and #30209 for proposals to change this.

@golang golang locked and limited conversation to collaborators Nov 24, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants