go/test/fixedbugs/issue27143.go
Alberto Donizetti 42cc4ca30a cmd/compile: prevent overflow in walkinrange
In the compiler frontend, walkinrange indiscriminately calls Int64()
on const CTINT nodes, even though Int64's return value is undefined
for anything over 2⁶³ (in practise, it'll return a negative number).

This causes the introduction of bad constants during rewrites of
unsigned expressions, which make the compiler reject valid Go
programs.

This change introduces a preliminary check that Int64() is safe to
call on the consts on hand. If it isn't, walkinrange exits without
doing any rewrite.

Fixes #27143

Change-Id: I2017073cae65468a521ff3262d4ea8ab0d7098d9
Reviewed-on: https://go-review.googlesource.com/130735
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com>
2018-08-26 21:52:27 +00:00

18 lines
553 B
Go

// 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