aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/cmd/compile/internal/gc/const.go11
-rw-r--r--src/cmd/compile/internal/gc/walk.go6
2 files changed, 17 insertions, 0 deletions
diff --git a/src/cmd/compile/internal/gc/const.go b/src/cmd/compile/internal/gc/const.go
index ceb124e31e..1403a2be11 100644
--- a/src/cmd/compile/internal/gc/const.go
+++ b/src/cmd/compile/internal/gc/const.go
@@ -121,6 +121,17 @@ func (n *Node) Int64() int64 {
return n.Val().U.(*Mpint).Int64()
}
+// CanInt64 reports whether it is safe to call Int64() on n.
+func (n *Node) CanInt64() bool {
+ if !Isconst(n, CTINT) {
+ return false
+ }
+
+ // if the value inside n cannot be represented as an int64, the
+ // return value of Int64 is undefined
+ return n.Val().U.(*Mpint).CmpInt64(n.Int64()) == 0
+}
+
// Bool returns n as a bool.
// n must be a boolean constant.
func (n *Node) Bool() bool {
diff --git a/src/cmd/compile/internal/gc/walk.go b/src/cmd/compile/internal/gc/walk.go
index bd936fb70a..2993e08fc2 100644
--- a/src/cmd/compile/internal/gc/walk.go
+++ b/src/cmd/compile/internal/gc/walk.go
@@ -3694,6 +3694,12 @@ func walkinrange(n *Node, init *Nodes) *Node {
return n
}
+ // Ensure that Int64() does not overflow on a and c (it'll happen
+ // for any const above 2**63; see issue #27143).
+ if !a.CanInt64() || !c.CanInt64() {
+ return n
+ }
+
if opl == OLT {
// We have a < b && ...
// We need a ≤ b && ... to safely use unsigned comparison tricks.