Skip to content

Commit 933e34a

Browse files
committed
cmd/compile: treat slice pointers as non-nil
var a []int = ... p := &a[0] _ = *p We don't need to nil check on the 3rd line. If the bounds check on the 2nd line passes, we know p is non-nil. We rely on the fact that any cap>0 slice has a non-nil pointer as its pointer to the backing array. This is true for all safely-constructed slices, and I don't see any reason why someone would violate this rule using unsafe. R=go1.13 Fixes #30366 Change-Id: I3ed764fcb72cfe1fbf963d8c1a82e24e3b6dead7 Reviewed-on: https://go-review.googlesource.com/c/163740 Run-TryBot: Keith Randall <[email protected]> Reviewed-by: Josh Bleecher Snyder <[email protected]>
1 parent 57976fe commit 933e34a

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ func nilcheckelim(f *Func) {
4949
// value, or a value constructed from an offset of a
5050
// non-nil ptr (OpAddPtr) implies it is non-nil
5151
// We also assume unsafe pointer arithmetic generates non-nil pointers. See #27180.
52-
if v.Op == OpAddr || v.Op == OpLocalAddr || v.Op == OpAddPtr || v.Op == OpOffPtr || v.Op == OpAdd32 || v.Op == OpAdd64 || v.Op == OpSub32 || v.Op == OpSub64 {
52+
// We assume that SlicePtr is non-nil because we do a bounds check
53+
// before the slice access (and all cap>0 slices have a non-nil ptr). See #30366.
54+
if v.Op == OpAddr || v.Op == OpLocalAddr || v.Op == OpAddPtr || v.Op == OpOffPtr || v.Op == OpAdd32 || v.Op == OpAdd64 || v.Op == OpSub32 || v.Op == OpSub64 || v.Op == OpSlicePtr {
5355
nonNilValues[v.ID] = true
5456
}
5557
}

test/codegen/slices.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,13 @@ func SliceExtensionInt64(s []int, l64 int64) []int {
6161
// 386:-`.*runtime\.memclr`
6262
return append(s, make([]int, l64)...)
6363
}
64+
65+
// ---------------------- //
66+
// Nil check of &s[0] //
67+
// ---------------------- //
68+
// See issue 30366
69+
func SliceNilCheck(s []int) {
70+
p := &s[0]
71+
// amd64:-`TESTB`
72+
_ = *p
73+
}

0 commit comments

Comments
 (0)