mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
cmd/compile,runtime: change unsafe.Slice((*T)(nil), 0) to return []T(nil)
This CL removes the unconditional OCHECKNIL check added in walkUnsafeSlice by instead passing it as a pointer to runtime.unsafeslice, and hiding the check behind a `len == 0` check. While here, this CL also implements checkptr functionality for unsafe.Slice and disallows use of unsafe.Slice with //go:notinheap types. Updates #46742. Change-Id: I743a445ac124304a4d7322a7fe089c4a21b9a655 Reviewed-on: https://go-review.googlesource.com/c/go/+/331070 Run-TryBot: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Go Bot <gobot@golang.org> Trust: Matthew Dempsky <mdempsky@google.com> Reviewed-by: Keith Randall <khr@golang.org>
This commit is contained in:
parent
1519271a93
commit
4bb0847b08
9 changed files with 83 additions and 27 deletions
|
|
@ -112,19 +112,37 @@ func makeslice64(et *_type, len64, cap64 int64) unsafe.Pointer {
|
|||
return makeslice(et, len, cap)
|
||||
}
|
||||
|
||||
func unsafeslice(et *_type, len int) {
|
||||
func unsafeslice(et *_type, ptr unsafe.Pointer, len int) {
|
||||
if len == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
if ptr == nil {
|
||||
panic(errorString("unsafe.Slice: ptr is nil and len is not zero"))
|
||||
}
|
||||
|
||||
mem, overflow := math.MulUintptr(et.size, uintptr(len))
|
||||
if overflow || mem > maxAlloc || len < 0 {
|
||||
panicunsafeslicelen()
|
||||
}
|
||||
}
|
||||
|
||||
func unsafeslice64(et *_type, len64 int64) {
|
||||
func unsafeslice64(et *_type, ptr unsafe.Pointer, len64 int64) {
|
||||
len := int(len64)
|
||||
if int64(len) != len64 {
|
||||
panicunsafeslicelen()
|
||||
}
|
||||
unsafeslice(et, len)
|
||||
unsafeslice(et, ptr, len)
|
||||
}
|
||||
|
||||
func unsafeslicecheckptr(et *_type, ptr unsafe.Pointer, len64 int64) {
|
||||
unsafeslice64(et, ptr, len64)
|
||||
|
||||
// Check that underlying array doesn't straddle multiple heap objects.
|
||||
// unsafeslice64 has already checked for overflow.
|
||||
if checkptrStraddles(ptr, uintptr(len64)*et.size) {
|
||||
throw("checkptr: unsafe.Slice result straddles multiple allocations")
|
||||
}
|
||||
}
|
||||
|
||||
func panicunsafeslicelen() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue