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

cmd/compile: can not decrement atomic uint64(0) #39553

Sasasu opened this issue Jun 12, 2020 · 2 comments

cmd/compile: can not decrement atomic uint64(0) #39553

Sasasu opened this issue Jun 12, 2020 · 2 comments


Copy link

@Sasasu Sasasu commented Jun 12, 2020

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

$ go version
go version go1.14.4 linux/amd64

Does this issue reproduce with the latest release?

package main

import "sync/atomic"

func main() {
	var foo uint64 = 1
	atomic.AddUint64(&foo, ^(0))

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
> on goplayground

What did you do?

atomic.AddUint64(&foo, ^(0))

according to this


Lines 103 to 106 in 509ee70

// AddUint64 atomically adds delta to *addr and returns the new value.
// To subtract a signed positive constant value c from x, do AddUint64(&x, ^uint64(c-1)).
// In particular, to decrement x, do AddUint64(&x, ^uint64(0)).
func AddUint64(addr *uint64, delta uint64) (new uint64)

What did you expect to see?

compile success, decrement uint64(0) got undefined behavior.

What did you see instead?

constant -1 overflows uint64

@Sasasu Sasasu changed the title cmd/compile: can not decrement atomic uint64 cmd/compile: can not decrement atomic uint64(0) Jun 12, 2020
Copy link

@DisposaBoy DisposaBoy commented Jun 12, 2020

I see you fixed the original issue by switching to atomic.AddUint64.
I think the new error is guarding against what's usually a bug since you can't store a negative number in an unsigned integer.

Copy link

@davecheney davecheney commented Jun 12, 2020

As the comment says ^uint64(0) is what you need to use.

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

No branches or pull requests

4 participants