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: constant expression overflow doesn't report an error #42732

Open
griesemer opened this issue Nov 19, 2020 · 3 comments
Open

cmd/compile: constant expression overflow doesn't report an error #42732

griesemer opened this issue Nov 19, 2020 · 3 comments
Assignees
Milestone

Comments

@griesemer
Copy link
Contributor

@griesemer griesemer commented Nov 19, 2020

https://play.golang.org/p/Ro-SJWdCh35

The compiler reports an error if a constant literal is too large to represent, but it doesn't if we exceed that value in constant arithmetic.

@griesemer griesemer added this to the Go1.17 milestone Nov 19, 2020
@griesemer griesemer self-assigned this Nov 19, 2020
@mdempsky
Copy link
Member

@mdempsky mdempsky commented Nov 19, 2020

It looks like we incorrectly allow infinity as a constant, but we correctly error about any attempts to turn it into a typed value.

I think my CLs to switch cmd/compile to use go/constant should fix this.

@mdempsky mdempsky self-assigned this Nov 25, 2020
@mdempsky
Copy link
Member

@mdempsky mdempsky commented Nov 25, 2020

At tip (of dev.regabi) we now ICE instead, because of the failure to report an error message.

Relatedly, I realized I missed that XOR can also cause overflow, so this currently ICEs as well while trying to report the overflow error:

const MaxInt512 = (1<<256 - 1) * (1<<256 + 1)
const _ = ^MaxInt512 // computes -1 << 512
@gopherbot
Copy link

@gopherbot gopherbot commented Nov 25, 2020

Change https://golang.org/cl/272928 mentions this issue: [dev.regabi] cmd/compile: fix reporting of overflow

gopherbot pushed a commit that referenced this issue Nov 25, 2020
In the previous CL, I had incorrectly removed one of the error
messages from issue20232.go, because I thought go/constant was just
handling it. But actually the compiler was panicking in nodlit,
because it didn't handle constant.Unknown. So this CL makes it leave
n.Type == nil for unknown constant.Values.

While here, also address #42732 by making sure to report an error
message when origConst is called with an unknown constant.Value (as
can happen when multiplying two floating-point constants overflows).

Finally, add OXOR and OBITNOT to the list of operations to report
errors about, since they're also constant expressions that can produce
a constant with a greater bit length than their operands.

Fixes #42732.

Change-Id: I4a538fbae9b3ac4c553d7de5625dc0c87d9acce3
Reviewed-on: https://go-review.googlesource.com/c/go/+/272928
Trust: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.