From 61bc17f04e232a62204b78b68e839db7029338d7 Mon Sep 17 00:00:00 2001 From: Keith Randall Date: Tue, 28 Mar 2023 10:19:21 -0700 Subject: cmd/compile: don't assume pointer of a slice is non-nil unsafe.SliceData can return pointers which are nil. That function gets lowered to the SSA OpSlicePtr, which the compiler assumes is non-nil. This used to be the case as OpSlicePtr was only used in situations where the bounds check already passed. But with unsafe.SliceData that is no longer the case. There are situations where we know it is nil. Use Bounded() to indicate that. I looked through all the uses of OSPTR and added SetBounded where it made sense. Most OSPTR results are passed directly to runtime calls (e.g. memmove), so even if we know they are non-nil that info isn't helpful. Fixes #59293 Change-Id: I437a15330db48e0082acfb1f89caf8c56723fc51 Reviewed-on: https://go-review.googlesource.com/c/go/+/479896 Reviewed-by: Matthew Dempsky Reviewed-by: Keith Randall TryBot-Result: Gopher Robot Run-TryBot: Keith Randall --- test/fixedbugs/issue59293.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 test/fixedbugs/issue59293.go (limited to 'test/fixedbugs') diff --git a/test/fixedbugs/issue59293.go b/test/fixedbugs/issue59293.go new file mode 100644 index 0000000000..1f05fe9a7a --- /dev/null +++ b/test/fixedbugs/issue59293.go @@ -0,0 +1,28 @@ +// run + +// Copyright 2023 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package main + +import "unsafe" + +//go:noinline +func f(x []byte) bool { + return unsafe.SliceData(x) != nil +} + +//go:noinline +func g(x string) bool { + return unsafe.StringData(x) != nil +} + +func main() { + if f(nil) { + panic("bad f") + } + if g("") { + panic("bad g") + } +} -- cgit v1.2.1