summaryrefslogtreecommitdiff
path: root/libgo
diff options
context:
space:
mode:
authorIan Lance Taylor <iant@golang.org>2021-07-30 17:19:42 -0700
committerIan Lance Taylor <iant@golang.org>2021-08-02 15:27:08 -0700
commit7459bfa8a37a4fbd6ed5153bff76f49d372b4ace (patch)
treee0c96ae718c359f5a026b93a2c3cf31eb4587fca /libgo
parent06d0437d4a5faca2b695918cbe1d54a61935c98b (diff)
compiler, runtime: allow slice to array pointer conversion
Panic if the slice is too short. For golang/go#395 Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/338630
Diffstat (limited to 'libgo')
-rw-r--r--libgo/go/runtime/error.go2
-rw-r--r--libgo/go/runtime/panic.go7
2 files changed, 9 insertions, 0 deletions
diff --git a/libgo/go/runtime/error.go b/libgo/go/runtime/error.go
index 80655342f13..a8c82bbf8e8 100644
--- a/libgo/go/runtime/error.go
+++ b/libgo/go/runtime/error.go
@@ -175,6 +175,7 @@ const (
boundsSlice3B // s[?:x:y], 0 <= x <= y failed (but boundsSlice3A didn't happen)
boundsSlice3C // s[x:y:?], 0 <= x <= y failed (but boundsSlice3A/B didn't happen)
+ boundsConvert // (*[x]T)(s), 0 <= x <= len(s) failed
// Note: in the above, len(s) and cap(s) are stored in y
)
@@ -190,6 +191,7 @@ var boundsErrorFmts = [...]string{
boundsSlice3Acap: "slice bounds out of range [::%x] with capacity %y",
boundsSlice3B: "slice bounds out of range [:%x:%y]",
boundsSlice3C: "slice bounds out of range [%x:%y:]",
+ boundsConvert: "cannot convert slice with length %y to pointer to array with length %x",
}
// boundsNegErrorFmts are overriding formats if x is negative. In this case there's no need to report y.
diff --git a/libgo/go/runtime/panic.go b/libgo/go/runtime/panic.go
index 11396b4123a..a4b9a83d776 100644
--- a/libgo/go/runtime/panic.go
+++ b/libgo/go/runtime/panic.go
@@ -38,6 +38,7 @@ import (
//go:linkname goPanicSlice3BU
//go:linkname goPanicSlice3C
//go:linkname goPanicSlice3CU
+//go:linkname goPanicSliceConvert
//go:linkname panicshift
//go:linkname panicdivide
//go:linkname panicmem
@@ -175,6 +176,12 @@ func goPanicSlice3CU(x uint, y int) {
panic(boundsError{x: int64(x), signed: false, y: y, code: boundsSlice3C})
}
+// failures in the conversion (*[x]T)s, 0 <= x <= y, x == cap(s)
+func goPanicSliceConvert(x int, y int) {
+ panicCheck1(getcallerpc(), "slice length too short to convert to pointer to array")
+ panic(boundsError{x: int64(x), signed: true, y: y, code: boundsConvert})
+}
+
var shiftError = error(errorString("negative shift amount"))
func panicshift() {