Skip to content

Commit 99475df

Browse files
committed
cmd/internal/gc: avoid spurious div-zero errors
Set overflowing integer constants to 1 rather than 0 to avoid spurious div-zero errors in subsequent constant expressions. Also: Exclude new test case from go/types test since it's running too long (go/types doesn't have an upper constant size limit at the moment). Fixes #7746. Change-Id: I3768488ad9909a3cf995247b81ee78a8eb5a1e41 Reviewed-on: https://go-review.googlesource.com/9165 Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Russ Cox <rsc@golang.org>
1 parent c4fe503 commit 99475df

File tree

4 files changed

+137
-3
lines changed

4 files changed

+137
-3
lines changed

src/cmd/internal/gc/const.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ func evconst(n *Node) {
774774
ODIV<<16 | CTRUNE:
775775
if mpcmpfixc(rv.U.Xval, 0) == 0 {
776776
Yyerror("division by zero")
777-
Mpmovecfix(v.U.Xval, 1)
777+
mpsetovf(v.U.Xval)
778778
break
779779
}
780780

@@ -784,7 +784,7 @@ func evconst(n *Node) {
784784
OMOD<<16 | CTRUNE:
785785
if mpcmpfixc(rv.U.Xval, 0) == 0 {
786786
Yyerror("division by zero")
787-
Mpmovecfix(v.U.Xval, 1)
787+
mpsetovf(v.U.Xval)
788788
break
789789
}
790790

src/cmd/internal/gc/mparith2.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
/// implements fix arithmetic
1414

1515
func mpsetovf(a *Mpint) {
16-
a.Val.SetUint64(0)
16+
a.Val.SetUint64(1) // avoid spurious div-zero errors
1717
a.Ovf = true
1818
}
1919

src/go/types/stdlib_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ func TestStdFixed(t *testing.T) {
146146
"bug459.go", // possibly incorrect test - see issue 6703 (pending spec clarification)
147147
"issue3924.go", // possibly incorrect test - see issue 6671 (pending spec clarification)
148148
"issue6889.go", // gc-specific test
149+
"issue7746.go", // large constants - consumes too much memory
149150
)
150151
}
151152

test/fixedbugs/issue7746.go

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
// errorcheck
2+
3+
// Copyright 2015 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package main
8+
9+
const (
10+
c0 = 1 << 100
11+
c1 = c0 * c0
12+
c2 = c1 * c1
13+
c3 = c2 * c2 // ERROR "overflow"
14+
c4 = c3 * c3
15+
c5 = c4 * c4
16+
c6 = c5 * c5
17+
c7 = c6 * c6
18+
c8 = c7 * c7
19+
c9 = c8 * c8
20+
c10 = c9 * c9
21+
c11 = c10 * c10
22+
c12 = c11 * c11
23+
c13 = c12 * c12
24+
c14 = c13 * c13
25+
c15 = c14 * c14
26+
c16 = c15 * c15
27+
c17 = c16 * c16
28+
c18 = c17 * c17
29+
c19 = c18 * c18
30+
c20 = c19 * c19
31+
c21 = c20 * c20
32+
c22 = c21 * c21
33+
c23 = c22 * c22
34+
c24 = c23 * c23
35+
c25 = c24 * c24
36+
c26 = c25 * c25
37+
c27 = c26 * c26
38+
c28 = c27 * c27
39+
c29 = c28 * c28
40+
c30 = c29 * c29
41+
c31 = c30 * c30
42+
c32 = c31 * c31
43+
c33 = c32 * c32
44+
c34 = c33 * c33
45+
c35 = c34 * c34
46+
c36 = c35 * c35
47+
c37 = c36 * c36
48+
c38 = c37 * c37
49+
c39 = c38 * c38
50+
c40 = c39 * c39
51+
c41 = c40 * c40
52+
c42 = c41 * c41
53+
c43 = c42 * c42
54+
c44 = c43 * c43
55+
c45 = c44 * c44
56+
c46 = c45 * c45
57+
c47 = c46 * c46
58+
c48 = c47 * c47
59+
c49 = c48 * c48
60+
c50 = c49 * c49
61+
c51 = c50 * c50
62+
c52 = c51 * c51
63+
c53 = c52 * c52
64+
c54 = c53 * c53
65+
c55 = c54 * c54
66+
c56 = c55 * c55
67+
c57 = c56 * c56
68+
c58 = c57 * c57
69+
c59 = c58 * c58
70+
c60 = c59 * c59
71+
c61 = c60 * c60
72+
c62 = c61 * c61
73+
c63 = c62 * c62
74+
c64 = c63 * c63
75+
c65 = c64 * c64
76+
c66 = c65 * c65
77+
c67 = c66 * c66
78+
c68 = c67 * c67
79+
c69 = c68 * c68
80+
c70 = c69 * c69
81+
c71 = c70 * c70
82+
c72 = c71 * c71
83+
c73 = c72 * c72
84+
c74 = c73 * c73
85+
c75 = c74 * c74
86+
c76 = c75 * c75
87+
c77 = c76 * c76
88+
c78 = c77 * c77
89+
c79 = c78 * c78
90+
c80 = c79 * c79
91+
c81 = c80 * c80
92+
c82 = c81 * c81
93+
c83 = c82 * c82
94+
c84 = c83 * c83
95+
c85 = c84 * c84
96+
c86 = c85 * c85
97+
c87 = c86 * c86
98+
c88 = c87 * c87
99+
c89 = c88 * c88
100+
c90 = c89 * c89
101+
c91 = c90 * c90
102+
c92 = c91 * c91
103+
c93 = c92 * c92
104+
c94 = c93 * c93
105+
c95 = c94 * c94
106+
c96 = c95 * c95
107+
c97 = c96 * c96
108+
c98 = c97 * c97
109+
c99 = c98 * c98
110+
c100 = c99 * c99
111+
)
112+
113+
func main() {
114+
println(c1 / c1)
115+
println(c2 / c2)
116+
println(c3 / c3)
117+
println(c4 / c4)
118+
println(c5 / c5)
119+
println(c6 / c6)
120+
println(c7 / c7)
121+
println(c8 / c8)
122+
println(c9 / c9)
123+
println(c10 / c10)
124+
println(c20 / c20)
125+
println(c30 / c30)
126+
println(c40 / c40)
127+
println(c50 / c50)
128+
println(c60 / c60)
129+
println(c70 / c70)
130+
println(c80 / c80)
131+
println(c90 / c90)
132+
println(c100 / c100)
133+
}

0 commit comments

Comments
 (0)