aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/cmd/compile/internal/gc/const.go11
-rw-r--r--src/cmd/compile/internal/gc/walk.go6
-rw-r--r--test/fixedbugs/issue27143.go17
3 files changed, 34 insertions, 0 deletions
diff --git a/src/cmd/compile/internal/gc/const.go b/src/cmd/compile/internal/gc/const.go
index dcc16b6dec..32af1fb3fa 100644
--- a/src/cmd/compile/internal/gc/const.go
+++ b/src/cmd/compile/internal/gc/const.go
@@ -122,6 +122,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 ca3025bbaa..79134067c6 100644
--- a/src/cmd/compile/internal/gc/walk.go
+++ b/src/cmd/compile/internal/gc/walk.go
@@ -3561,6 +3561,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.
diff --git a/test/fixedbugs/issue27143.go b/test/fixedbugs/issue27143.go
new file mode 100644
index 0000000000..009ec9f6c2
--- /dev/null
+++ b/test/fixedbugs/issue27143.go
@@ -0,0 +1,17 @@
+// compile
+
+// Copyright 2018 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Issue 27143: cmd/compile: erroneous application of walkinrange
+// optimization for const over 2**63
+
+package p
+
+var c uint64
+
+var b1 bool = 0x7fffffffffffffff < c && c < 0x8000000000000000
+var b2 bool = c < 0x8000000000000000 && 0x7fffffffffffffff < c
+var b3 bool = 0x8000000000000000 < c && c < 0x8000000000000001
+var b4 bool = c < 0x8000000000000001 && 0x8000000000000000 < c