Skip to content

Commit bc98e35

Browse files
committed
cmd/compile: avoid memmove -> SSA move rewrite when size is negative
We should panic in this situation. Rewriting to a SSA op just leads to a compiler panic. Fixes #36259 Change-Id: I6e0bccbed7dd0fdac7ebae76b98a211947947386 Reviewed-on: https://go-review.googlesource.com/c/go/+/212405 Run-TryBot: Keith Randall <khr@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com>
1 parent e092fc3 commit bc98e35

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

src/cmd/compile/internal/ssa/gen/generic.rules

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1938,6 +1938,7 @@
19381938

19391939
// Inline small or disjoint runtime.memmove calls with constant length.
19401940
(StaticCall {sym} s1:(Store _ (Const(64|32) [sz]) s2:(Store _ src s3:(Store {t} _ dst mem))))
1941+
&& sz >= 0
19411942
&& isSameSym(sym,"runtime.memmove")
19421943
&& t.(*types.Type).IsPtr() // avoids TUINTPTR, see issue 30061
19431944
&& s1.Uses == 1 && s2.Uses == 1 && s3.Uses == 1

src/cmd/compile/internal/ssa/rewritegeneric.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixedbugs/issue36259.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// compile
2+
3+
// Copyright 2019 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+
func rotate(s []int, m int) {
10+
l := len(s)
11+
m = m % l
12+
buf := make([]int, m)
13+
14+
copy(buf, s)
15+
copy(s, s[m:])
16+
copy(s[l-m:], buf)
17+
}
18+
19+
func main() {
20+
a0 := [...]int{1,2,3,4,5}
21+
println(a0[0])
22+
23+
rotate(a0[:], 1)
24+
println(a0[0])
25+
26+
rotate(a0[:], -3)
27+
println(a0[0])
28+
}

0 commit comments

Comments
 (0)