diff options
| author | cuiweixie <cuiweixie@gmail.com> | 2022-08-08 16:07:05 +0000 |
|---|---|---|
| committer | Keith Randall <khr@google.com> | 2022-08-08 16:37:49 +0000 |
| commit | 52d0667e6ba69059422a138dd6589f3f697fc0db (patch) | |
| tree | 991590efca972b342d0dbf565a0afa0bd0274190 /src/runtime | |
| parent | 1519729c6a1f05735fdc7a6db38dc83838783eee (diff) | |
| download | go-52d0667e6ba69059422a138dd6589f3f697fc0db.tar.xz | |
cmd/compile,runtime: panic when unsafe.Slice param is nil and > 0
Fixes #54092
Change-Id: Ib917922ed36ee5410e5515f812737203c44f46ae
GitHub-Last-Rev: dfd0c3883cf8b10479d9c5b389baa1a04c52dd34
GitHub-Pull-Request: golang/go#54107
Reviewed-on: https://go-review.googlesource.com/c/go/+/419755
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Diffstat (limited to 'src/runtime')
| -rw-r--r-- | src/runtime/crash_test.go | 8 | ||||
| -rw-r--r-- | src/runtime/slice.go | 6 | ||||
| -rw-r--r-- | src/runtime/testdata/testprog/unsafe.go | 12 |
3 files changed, 26 insertions, 0 deletions
diff --git a/src/runtime/crash_test.go b/src/runtime/crash_test.go index 01d7cbeb29..02604595ac 100644 --- a/src/runtime/crash_test.go +++ b/src/runtime/crash_test.go @@ -844,3 +844,11 @@ func TestPanicWhilePanicking(t *testing.T) { } } } + +func TestPanicOnUnsafeSlice(t *testing.T) { + output := runTestProg(t, "testprog", "panicOnNilAndEleSizeIsZero") + want := "panic: runtime error: unsafe.Slice: ptr is nil and len is not zero" + if !strings.Contains(output, want) { + t.Errorf("output does not contain %q:\n%s", want, output) + } +}
\ No newline at end of file diff --git a/src/runtime/slice.go b/src/runtime/slice.go index 2413a46d6a..8a0ce49fad 100644 --- a/src/runtime/slice.go +++ b/src/runtime/slice.go @@ -129,6 +129,12 @@ func unsafeslice(et *_type, ptr unsafe.Pointer, len int) { panicunsafeslicelen() } + if et.size == 0 { + if ptr == nil && len > 0 { + panicunsafeslicenilptr() + } + } + mem, overflow := math.MulUintptr(et.size, uintptr(len)) if overflow || mem > -uintptr(ptr) { if ptr == nil { diff --git a/src/runtime/testdata/testprog/unsafe.go b/src/runtime/testdata/testprog/unsafe.go new file mode 100644 index 0000000000..d6dddf22c2 --- /dev/null +++ b/src/runtime/testdata/testprog/unsafe.go @@ -0,0 +1,12 @@ +package main + +import "unsafe" + +func init() { + register("panicOnNilAndEleSizeIsZero", panicOnNilAndEleSizeIsZero) +} + +func panicOnNilAndEleSizeIsZero() { + var p *struct{} + _ = unsafe.Slice(p, 5) +}
\ No newline at end of file |
