Skip to content

Commit 60e6afb

Browse files
cuonglmgopherbot
authored andcommitted
cmd/compile: do not report division by error during typecheck
types2 have already errored about any spec-required overflows, and division by zero. CL 469595 unintentionally fixed typecheck not to error about overflows, but zero division is still be checked during tcArith. This causes unsafe operations with variable size failed to compile, instead of raising runtime error. Fixes #60601 Change-Id: I7bea2821099556835c920713397f7c5d8a4025ac Reviewed-on: https://go-review.googlesource.com/c/go/+/501735 Auto-Submit: Cuong Manh Le <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Keith Randall <[email protected]> Run-TryBot: Cuong Manh Le <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Cherry Mui <[email protected]>
1 parent f6e0dcc commit 60e6afb

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

src/cmd/compile/internal/typecheck/expr.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,6 @@ func tcArith(n ir.Node, op ir.Op, l, r ir.Node) (ir.Node, ir.Node, *types.Type)
184184
}
185185
}
186186

187-
if (op == ir.ODIV || op == ir.OMOD) && ir.IsConst(r, constant.Int) {
188-
if constant.Sign(r.Val()) == 0 {
189-
base.Errorf("division by zero")
190-
return l, r, nil
191-
}
192-
}
193-
194187
return l, r, t
195188
}
196189

test/fixedbugs/issue60601.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// run
2+
3+
// Copyright 2023 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+
import (
10+
"strings"
11+
"unsafe"
12+
)
13+
14+
func shift[T any]() int64 {
15+
return 1 << unsafe.Sizeof(*new(T))
16+
}
17+
18+
func div[T any]() uintptr {
19+
return 1 / unsafe.Sizeof(*new(T))
20+
}
21+
22+
func add[T any]() int64 {
23+
return 1<<63 - 1 + int64(unsafe.Sizeof(*new(T)))
24+
}
25+
26+
func main() {
27+
shift[[62]byte]()
28+
shift[[63]byte]()
29+
shift[[64]byte]()
30+
shift[[100]byte]()
31+
shift[[1e6]byte]()
32+
33+
add[[1]byte]()
34+
shouldPanic("divide by zero", func() { div[[0]byte]() })
35+
}
36+
37+
func shouldPanic(str string, f func()) {
38+
defer func() {
39+
err := recover()
40+
if err == nil {
41+
panic("did not panic")
42+
}
43+
s := err.(error).Error()
44+
if !strings.Contains(s, str) {
45+
panic("got panic " + s + ", want " + str)
46+
}
47+
}()
48+
49+
f()
50+
}

0 commit comments

Comments
 (0)